SpringBoot Mybatis-Plus 对数据库的操作

一、新建项目

1、新建项目。

SpringBoot Mybatis-Plus 对数据库的操作_第1张图片

2、父模块pom.xml文件的配置。



    
        org.springframework.boot
        spring-boot-starter-parent
        2.1.3.RELEASE
    
    4.0.0

    com.zxj
    zxj-reptile
    1.0
    pom

    
        zxj-reptile-api
        zxj-reptile-manage
    

    
        UTF-8
        1.8
        2.1.9
        5.1.47
        1.2.37
        2.9.2
    

    
        
        
            org.springframework.boot
            spring-boot-starter
        
        
            org.springframework.boot
            spring-boot-starter-web
        
        
        
            com.baomidou
            mybatis-plus-boot-starter
            ${mybatis-plus-boot-starter.version}
        
        
            mysql
            mysql-connector-java
            ${mysql.version}
            compile
        
        
        
            com.alibaba
            fastjson
            ${com.alibaba.version}
        
    

    
        
            
            dev
            
                dev
            
            
                true
            
        
        
            
            test
            
                test
            
        
        
            
            pro
            
                pro
            
        
    

3、zxj-reptile-manage子模块pom.xml的配置。



    
        zxj-reptile
        com.zxj
        1.0
    
    4.0.0

    zxj-reptile-manage
    jar

    
    

4、zxj-reptile-api子模块pom.xml的配置。



    4.0.0
    
        com.zxj
        zxj-reptile
        1.0
    
    zxj-reptile-api
    jar

    
        
        
            com.zxj
            zxj-reptile-manage
            ${project.parent.version}
        
        
        
            io.springfox
            springfox-swagger2
            ${springfox-swagger.version}
        
        
            io.springfox
            springfox-swagger-ui
            ${springfox-swagger.version}
        
    

    
        
            
                org.springframework.boot
                spring-boot-maven-plugin
            
        
    

5、zxj-reptile-api子模块下的application.yml文件的配置

server:
  port: 9001

spring:
  profiles:
    active: dev

  datasource:
    url: jdbc:mysql://localhost:3306/zxj_sql?useUnicode=true&characterEncoding=utf-8
    username: root
    password: 123456
    driverClassName: com.mysql.jdbc.Driver
    dbcp2:
      test-on-borrow: false  #从连接池借用连接时,是否测试该连接
      test-on-create: false   #创建连接时,是否测试连接
      test-on-return: false  #连接归还到连接池时是否测试该连接
      test-while-idle: false #当连接空闲时,是否执行连接测试.
      validation-query: SELECT 1               #获取连接时连接校验的sql查询语句
      time-between-eviction-runs-millis: 20000 #连接池调整的时间间隔
      min-evictable-idle-time-millis: 20000    #空闲连接最少空闲多久后可被清除

#mybatis
mybatis-plus:
  mapper-locations: classpath*:/mapper/**/*.xml
  type-aliases-package: com.zxj.reptile.module.*.entity
  global-config:
    # 数据库相关配置
    db-config:
      #主键类型  0:"数据库ID自增", 1:"用户输入ID",2:"全局唯一ID (数字类型唯一ID)", 3:"全局唯一ID UUID";
      id-type: 3
      #字段策略 0:"忽略判断",1:"非 NULL 判断"),2:"非空判断"
      field-strategy: 1
      #驼峰下划线转换
      column-underline: true
      #逻辑删除配置
      logic-delete-value: 0
      logic-not-delete-value: 1
      db-type: mysql
    #刷新mapper 调试神器
    refresh: true
  # 原生配置
  configuration:
    map-underscore-to-camel-case: true
    cache-enabled: false
    #mybatis-plus的日志开关
#    log-impl: org.apache.ibatis.logging.stdout.StdOutImpl

二、数据库代码的自动生成。

    这个可以参考我的其他文章  Mybatis-plus 自动生成数据库相关代码

三、Swagger的使用。

   这个可以参考我的其他文章  Java swagger2的使用

1、可以用一个公共的返回类型,作为所有的接口返回值类型。

public class AjaxJson {
    private T data;
    private String msg;
    private int code;

    public AjaxJson() {
        setSuccess("成功");
    }

    public T getData() {
        return data;
    }

    public void setData(T data) {
        this.data = data;
    }

    public String getMsg() {
        return msg;
    }

    public void setMsg(String msg) {
        this.msg = msg;
    }

    public int getCode() {
        return code;
    }

    public void setCode(int code) {
        this.code = code;
    }

    public void setSuccess(String msg) {
        setMsg(msg);
        this.code = 200;
    }

    public void setError(String msg) {
        setMsg(msg);
        this.code = 500;
    }
}

2、Api接口。主要做了对数据库的查询和插入。

package com.zxj.reptile.module.demo;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.zxj.reptile.module.AjaxJson;
import com.zxj.reptile.module.demo.entity.Demo;
import com.zxj.reptile.module.demo.service.IDemoService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@Api(value = "demo", tags = {"测试demo"})
@RestController
@RequestMapping("demo")
public class DemoApi {
    @Autowired
    private IDemoService demoService;

    @ApiOperation(value = "添加内容", notes = "添加内容")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "content", value = "内容",  dataType = "String", paramType = "query")
    })
    @RequestMapping(value = "/insert", method = RequestMethod.PUT)
    public AjaxJson addDemo(@RequestParam(value = "content") String content) {
        AjaxJson ajaxJson = new AjaxJson<>();
        try {
            Demo entity = new Demo();
            entity.setContent(content);
            demoService.insert(entity);
            ajaxJson.setData(entity);
        } catch (Exception e) {
            e.printStackTrace();
            ajaxJson.setError("失败: " + e.getMessage());
        }
        return ajaxJson;
    }

    @ApiOperation(value = "获取列表信息", notes = "获取列表信息")
    @RequestMapping(value = "/list", method = RequestMethod.GET)
    public AjaxJson listDemo() {
        AjaxJson ajaxJson = new AjaxJson<>();
        try {
            List entityList = demoService.selectList(new EntityWrapper<>());
            ajaxJson.setData(entityList);
        } catch (Exception e) {
            e.printStackTrace();
            ajaxJson.setError("失败: " + e.getMessage());
        }
        return ajaxJson;
    }
}

四、测试。 

1、数据库展示。

SpringBoot Mybatis-Plus 对数据库的操作_第2张图片

2、测试查询。

SpringBoot Mybatis-Plus 对数据库的操作_第3张图片

3、测试插入。

SpringBoot Mybatis-Plus 对数据库的操作_第4张图片

4、查看数据的变化。

SpringBoot Mybatis-Plus 对数据库的操作_第5张图片

 

 

有不解的地方,可以在下方留言评论。

你可能感兴趣的:(数据库,增删改查,Mybatis-Plus,Java后端,SpringBoot,SpringBoot,数据库)