mybatis plus使用雪花算法

编程入门 行业动态 更新时间:2024-10-13 04:25:44

mybatis plus使用雪花<a href=https://www.elefans.com/category/jswz/34/1770096.html style=算法"/>

mybatis plus使用雪花算法

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

com.baomidou

mybatis-plus-boot-starter

3.1.0

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

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();

}

}

更多推荐

mybatis plus使用雪花算法

本文发布于:2024-02-13 18:59:57,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1760439.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:算法   雪花   mybatis

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!