栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 前沿技术 > 大数据 > 大数据系统

Springboot+Mybatis plus +Thymeleaf实现数据库增删改

Springboot+Mybatis plus +Thymeleaf实现数据库增删改

1、SpringBoot项目创建好了之后,首先引入mysql驱动,mybatis plus,thymeleaf所需的驱动

pom.xml



    4.0.0
    
        org.springframework.boot
        spring-boot-starter-parent
        2.5.5
         
    
    com.example
    demo
    0.0.1-SNAPSHOT
    demo
    Demo project for Spring Boot
    
        1.8
    
    
        
            org.springframework.boot
            spring-boot-devtools
            true
        
        
            org.springframework.boot
            spring-boot-starter
        
        
            org.springframework.boot
            spring-boot-starter-test
            test
        
        
            org.springframework.boot
            spring-boot-starter-web
        
        
            mysql
            mysql-connector-java
            runtime
        
        
            com.alibaba
            druid
            1.1.20
        
        
            com.baomidou
            mybatis-plus-boot-starter
            3.4.0
        
        
            org.springframework.boot
            spring-boot-starter-thymeleaf
        
    

    
        
            
                src/main/java
                
                    ***
                
            
        
        
            
                org.springframework.boot
                spring-boot-maven-plugin
                
                    true
                
            
        
    

 2、配置连接数据库属性,及mybatis plus配置

 application.yml

server:
  port: 8000

spring:
  thymeleaf:
    prefix: classpath:/templates/
  datasource:
    type: com.alibaba.druid.pool.DruidDataSource
    url: jdbc:mysql:///test?characterEncoding=utf-8&serverTimezone=UTC 
    username: root       //数据库账号
    password: lichenxi2   //数据库密码

mybatis-plus:
  mapper-locations: mapper
    @PostMapping("/addAction")
    public String addAction(@RequestParam(name = "name2") String name,
                            @RequestParam(name = "age2") String age,
                            @RequestParam(name = "address2") String address){
        Student stu1 = new Student(0,name,address,age);
        studentService.add(stu1);
        return "redirect:/list";
    }


    @PostMapping("/addAction2")
    @ResponseBody
    public String addAction2(@RequestBody Map req){
        Student stu1 = new Student(0, (String)req.get("name"),
                (String)req.get("address"),
                (String)req.get("age"));
        studentService.add(stu1);
        return "{"result":0,"msg":"ok"}";
    }

    @GetMapping("/update")
    public  String update(@RequestParam(name = "id") Integer id, Model model){
        Student stu1 = studentService.findByID(id.intValue());
        model.addAttribute("student",stu1);
        return "update";
    }
    @PostMapping("/updateAction")
    public String updateAction(Student student){
        studentService.update(student);
        return "redirect:/list";
    }
    @GetMapping("/delete")
    public String delete(@RequestParam(name = "id") Integer id){
        studentService.delete(id.intValue());
        return "redirect:/list";
    }

index.html(主页面)




    
    Title


新增
    
ID姓名年龄地址操作
更新 删除

add.html




    
    
    Title


姓名:
年龄:
地址:

update.html




    
    Title


姓名: 年龄: 地址: ID:

演示

点击更新后的页面:

 

 点击新增后的页面:

就这样了,做的比较简陋。 

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/311217.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号