Mybatis-Plus雪花id的使用以及参数配置详解
1.引入Mybatis-Plus依赖(3.1.1版本目前有些问题,建议使用3.1.0版本)
1 2 3 4 5 | <dependency> <groupId>com.baomidou</groupId> <artifactId>mybatis-plus-boot-starter</artifactId> <version> 3.1 . 0 </version> </dependency> |
2.在application.yml配置文件中增加如下配置项
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 | mybatis-plus: #mapper-locations: classpath:mybatis /**/ *Mapper.xml # 在classpath前添加星号可以使项目热加载成功<br> # 自定义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接口
1 | public interface UserMapper extends BaseMapper<User> |
4.实体类增加注解
在User实体类上添加@TableId的注解用来标识实体类的主键,以便插件在生成主键雪花Id或者其他id模式的时候找到哪个是主键;这种id是局部id,优先级:局部>全局。
1 2 3 | @TableId @JsonFormat (shape = JsonFormat.Shape.STRING) private Long userId; |
5.分页配置
5.1 添加mybatis的一个配置类(不添加,则分页数据中的page参数会异常)
1 2 3 4 5 6 7 8 9 10 | @Configuration @MapperScan ({ "com.nis.project.*.mapper" }) //也可以放到spring boot的启动类 public class MybatisPlusConfig { @Bean public PaginationInterceptor paginationInterceptor() { return new PaginationInterceptor(); } } |
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 基于Microsoft.Extensions.AI核心库实现RAG应用
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
· 开发者必知的日志记录最佳实践
· SQL Server 2025 AI相关能力初探
· 震惊!C++程序真的从main开始吗?99%的程序员都答错了
· 单元测试从入门到精通
· 【硬核科普】Trae如何「偷看」你的代码?零基础破解AI编程运行原理
· 上周热点回顾(3.3-3.9)
· winform 绘制太阳,地球,月球 运作规律