Spring Boot(Smart Moon项目)集成Mybatis

Spring Boot(Smart Moon项目)集成Mybatis

一、Spring引入Mybatis

  • 编辑项目根目录的pom.xml,添加以下内容

    
    
         org.mybatis
         mybatis
         3.5.10
    
    
    
         org.mybatis.spring.boot
         mybatis-spring-boot-starter
         2.2.2
    
    

二、配置Mybatis generator自动生成CURD接口基础代码

  • resource目录下配置generator.properties

    jdbc.driverLocation=D:\\SQL\\mysql-connector-8.0.23\\mysql-connector-java-8.0.23.jar
    jdbc.driverClass=com.mysql.cj.jdbc.Driver #需要指定连接驱动路径
    jdbc.connectionURL=jdbc:mysql://localhost:3306/database_name?characterEncoding=utf-8
    jdbc.userId=root
    jdbc.password=your_password
    
    
    pom.xml
    
        
            
                org.springframework.boot
                spring-boot-maven-plugin
            
            
            
                org.mybatis.generator
                mybatis-generator-maven-plugin
                1.3.2
                
                    true
                    true
                
            
        
    
    
    
  • resource目录下配置generatorConfig.xml

    
    
    
    
        
        
    
        
        
    
        
    
            
            
                
                
            
    
            
            
            
    
    
            
            
                
            
    
    
            
            
    
                
                
                
                
                
                
                
                
            
    
            
            
                
            
    
            
            
                
            
    
            
            

    如若:http://mybatis.org/dtd/mybatis-generator-config_1_0.dtd 报红,可以将此链接复制到浏览器打开,下载文件后,存在在自定义路径,然后编辑IEDA的file -> setting -> Schemas and DTDs -> 找到EXternal schemas and DTDs:点击 + 号,

    • URL填写http://mybatis.org/dtd/mybatis-generator-config_1_0.dtd
    • File: 浏览找到文件下载后的存放位置,点击apply -> OK 退出

    Spring Boot(Smart Moon项目)集成Mybatis_第1张图片
    在这里插入图片描述

  • 编辑Run配置

    • 点击工具栏Run -> Edit Configurations…

      Spring Boot(Smart Moon项目)集成Mybatis_第2张图片

    • 点击+号 -> 选择maven,配置如下内容后点击apply -> Ok 退出:

      • Name: mybatis-generator 【可以自定义】
      • Run: mybatis-generator:generate -e

      Spring Boot(Smart Moon项目)集成Mybatis_第3张图片

    • 点击Run选择:mybatis-generator,自动生成配置generatorConfig.xml文件配置中, 注释下的表的对应CURD基础代码

三、编写Controller

  • 项目路径src\main\java\com\demo\smartmoon新建controller包,新建SysUserController【本项目使用】

    
    @RestController
    @RequestMapping(value = "/api/2/user", produces = MediaType.APPLICATION_JSON_VALUE)
    public class SysUserController {
        @Autowired
        private SysUserService sysUserService;
        
        @GetMapping(value = "/findByID")
        public Object findByID(@RequestParam Long id){
            return Response.success(sysUserService.findByID(id));
        }
    
        @ApiOperation(value = "新增用户信息")
        public Object insertSysUser(@RequestBody SysUser sysUser){
            return Response.success(sysUserService.insertSysUser(sysUser));
        }
    }
    
    • 测试效果:

      Spring Boot(Smart Moon项目)集成Mybatis_第4张图片

四、对接Knife4j

  • application.yml配置文件开启knife4j

    knife4j:
      # 开启增强配置
      enable: true
    
  • controller新增注解

    
    @RestController
    @RequestMapping(value = "/api/2/user", produces = MediaType.APPLICATION_JSON_VALUE)
    @EnableOpenApi
    @Api(value = "信息管理", tags="账户管理")
    public class SysUserController {
        @Autowired
        private SysUserService sysUserService;
    
        @ApiOperation(value = "根据ID查询账户列表")
        @GetMapping(value = "/findByID")
        @ApiOperationSupport(author = "xuanyu.li")
        public Object findByID(@RequestParam Long id){
            System.out.println(id);
            return Response.success(sysUserService.findByID(id));
        }
    
        @ApiOperation(value = "新增用户信息")
        @PostMapping (value = "/insertSysUser")
        @ApiOperationSupport(author = "xuanyu.li")
        public Object insertSysUser(@RequestBody SysUser sysUser){
            return Response.success(sysUserService.insertSysUser(sysUser));
        }
    }
    
  • 实体类model新增注解【如果想要在knife4j的swagger models看见】

    
    @Data
    @ApiModel(value = "SysUser",description = "用户实体对象")
    public class SysUser {
        @ApiModelProperty(value = "主键id", name = "id")
        private Long id;
    
        @ApiModelProperty(value = "name", name = "name")
        private String name;
    
        @ApiModelProperty(value = "nickName", name = "nickName")
        private String nickName;
    
        @ApiModelProperty(value = "avater", name = "avater")
        private String avater;
    
        @ApiModelProperty(value = "salt", name = "salt")
        private String salt;
    
        @ApiModelProperty(value = "password", name = "password")
        private String password;
    
        @ApiModelProperty(value = "email", name = "email")
        private String email;
    
        @ApiModelProperty(value = "mobile", name = "mobile")
        private String mobile;
    
        @ApiModelProperty(value = "status", name = "status")
        private Byte status;
    
        @ApiModelProperty(value = "deptId", name = "deptId")
        private Long deptId;
    ......【略,根据自己表确定】
    
  • 效果:

Spring Boot(Smart Moon项目)集成Mybatis_第5张图片
Spring Boot(Smart Moon项目)集成Mybatis_第6张图片

你可能感兴趣的:(Java基础集合,mybatis,spring,boot,java)