Mybatis-Plus雪花id的使用以及参数配置详解

1.引入Mybatis-Plus依赖(3.1.1版本目前有些问题,建议使用3.1.0版本)

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

  

2.在application.yml配置文件中增加如下配置项

mybatis-plus:
  #mapper-locations: classpath:mybatis/**/*Mapper.xml
  # 在classpath前添加星号可以使项目热加载成功
# 自定义xml sql文件需要配置这个 mapper-locations: classpath*:mybatis/**/*Mapper.xml #实体扫描,多个package用逗号或者分号分隔 typeAliasesPackage: com.nis.project global-config: #主键类型 0:"数据库ID自增", 1:"用户输入ID",2:"全局唯一ID (数字类型唯一ID)", 3:"全局唯一ID UUID"; id-type: 3 #机器 ID 部分(影响雪花ID) workerId: 1 #数据标识 ID 部分(影响雪花ID)(workerId 和 datacenterId 一起配置才能重新初始化 Sequence) datacenterId: 18 #字段策略 0:"忽略判断",1:"非 NULL 判断"),2:"非空判断" field-strategy: 2 #驼峰下划线转换 db-column-underline: true #刷新mapper 调试神器 refresh-mapper: true #数据库大写下划线转换 #capital-mode: true #序列接口实现类配置 #key-generator: com.baomidou.springboot.xxx #逻辑删除配置(下面3个配置) logic-delete-value: 0 logic-not-delete-value: 1 #自定义SQL注入器 #sql-injector: com.baomidou.mybatisplus.mapper.LogicSqlInjector #自定义填充策略接口实现 #meta-object-handler: com.baomidou.springboot.xxx configuration: map-underscore-to-camel-case: true cache-enabled: false # 这个配置会将执行的sql打印出来,在开发或测试的时候可以用 log-impl: org.apache.ibatis.logging.stdout.StdOutImpl

  

3.原有的mapper接口增加继承BaseMapper接口

public interface UserMapper extends BaseMapper<User>

  

4.实体类增加注解

在User实体类上添加@TableId的注解用来标识实体类的主键,以便插件在生成主键雪花Id或者其他id模式的时候找到哪个是主键;这种id是局部id,优先级:局部>全局。

@TableId
    @JsonFormat(shape = JsonFormat.Shape.STRING)
    private Long userId;

  

5.分页配置

5.1 添加mybatis的一个配置类(不添加,则分页数据中的page参数会异常)

@Configuration
@MapperScan({"com.nis.project.*.mapper"}) //也可以放到spring boot的启动类
public class MybatisPlusConfig {

   
    @Bean
    public PaginationInterceptor paginationInterceptor() {
        return new PaginationInterceptor();
    }
}

  以上资料转载,点我

猜你喜欢

转载自www.cnblogs.com/chenghening/p/12793280.html
今日推荐