zoukankan      html  css  js  c++  java
  • SpringBoot集成Mybatis-puls

    1、引入依赖

    <dependency>
        <groupId>com.baomidou</groupId>
        <artifactId>mybatis-plus-boot-starter</artifactId>
        <version>3.3.1.tmp</version>
    </dependency>

    2.配置

    application.properties方式:
    #配置数据源
    spring.datasource.url=jdbc:mysql://localhost:3306/school
    spring.datasource.driverClassName=com.mysql.jdbc.Driver
    spring.datasource.username=root
    spring.datasource.password=123456
    spring.datasource.type=org.apache.commons.dbcp2.BasicDataSource
    
    
    #mybatis-plus 配置
    #设置id类型,详见说明
    mybatis-plus.global-config.db-config.id-type: auto
    #设置策略,详见说明
    mybatis-plus.global-config.db-config.field-strategy: not_empty   
    #开启驼峰下划线转换
    mybatis-plus.global-config.db-config.column-underline: true 
    #全局逻辑删除字段值 3.3.0开始支持,详见说明
    mybatis-plus.global-config.db-config.logic-delete-field: flag
    #设置逻辑删除已删除值
    mybatis-plus.global-config.db-config.logic-delete-value: 0
    #设置逻辑删除未删除值
    mybatis-plus.global-config.db-config.logic-not-delete-value: 1
    #设置数据库类型(设置数据库方言)
    mybatis-plus.global-config.db-config.db-type: mysql
    #配置热加载,Mapper更改后无需重启
    mybatis-plus.global-config.refresh=true
    
    #返回Map的时候,将Map内的Key转换为驼峰的命名表达式
    configuration.map-underscore-to-camel-case: true
    #开启缓存
    configuration.cache-enabled: false
    application.yml方式:
    #配置数据源
    spring:
        datasource:
          url: jdbc:mysql://localhost:3306/school
          driverClassName: com.mysql.jdbc.Driver
          username: root
          password: 123456
          #配置连接池
          type: org.apache.commons.dbcp2.BasicDataSource
    #mybatis-plus 配置
    mybatis-plus:
      global-config:
        db-config:
          id-type: auto                #设置id类型,详见说明
          field-strategy: not_empty    #设置策略,详见说明
          column-underline: true       #启动驼峰下划线转换
          #逻辑删除配置
          logic-delete-field: flag  #全局逻辑删除字段值 3.3.0开始支持,详见说明
          logic-delete-value: 0  # 逻辑已删除值(默认为 0)
          logic-not-delete-value: 1 # 逻辑未删除值(默认为 1)
          db-type: mysql            #数据库类型(设置数据库方言)
        refresh: false
      configuration:
        map-underscore-to-camel-case: true  #返回Map的时候,将Map内的Key转换为驼峰的命名表达式
        cache-enabled: false                #开启缓存

    3.在SpringBoot启动类加入@MapperScan设置mapper包

    @SpringBootApplication
    @MapperScan("com.baomidou.mybatisplus.samples.quickstart.mapper")
    public class Application {
    
        public static void main(String[] args) {
            SpringApplication.run(Application.class, args);
        }
    
    }
     

    说明:

    id类型:(可在实体类用注解@TableId(value="id",type=IdType.AUTO) 声明类型)
    AUTO->`0`("数据库ID自增")
    INPUT->`1`(用户输入ID")
    ID_WORKER->`2`("全局唯一ID")
    UUID->`3`("全局唯一ID")
    NONE-> 4 ("不需要ID")
     
    策略:
    not_null,默认策略,也就是忽略null的字段,不忽略""
    not-empty 为null或为空字符串的忽略,就是如果设置值为null,“”,不会插入数据库
     
    逻辑删除:
    实体类字段上加上@TableLogic注解
    @TableLogic private Integer deleted;
    如果实体类上有 @TableLogic 则以实体上的为准,忽略全局。
     
    作者:ki16
    本文版权归作者和博客园共有,欢迎转载,但未经作者同意必须在文章页面给出原文连接,否则保留追究法律责任的权利。
  • 相关阅读:
    关于GridView Master-Detail 不支持明细属性为IEnumerable、IList问题
    GridControl摘录
    DevExpress中chartControl中实现统计图功能
    DevExpress中GridControl列转义的实现方法
    DevExpress实现根据行,列索引来获取RepositoryItem的方法
    修改yum源为阿里云的
    使用sendmail来发邮件
    nginx默认访问目录时显示403错误
    同时调整lv分区的大小(减少一个,增加另一个)
    ORA-00257错误的解决办法
  • 原文地址:https://www.cnblogs.com/gaojinshun/p/14557431.html
Copyright © 2011-2022 走看看