Mybatis-Plus插件配置
2022.01.25已更新至最新版本 -> 3.5.1
-
yml配置
1 # Mybatis-Plus 2 mybatis-plus: 3 # 配置mapper的扫描,找到所有的mapper.xml映射文件 4 mapper-locations: com.xxx.project.biz.*.mapper.*Mapper.xml,com.xxx.project.biz.*.*.mapper.*Mapper.xml 5 #实体扫描 6 typeAliasesPackage: com.xxx.project.biz.*.entity,com.xxx.project.biz.*.*.entity 7 global-config: 8 # 数据库相关配置 9 db-config: 10 #主键类型 NONE:"数据库ID自增", INPUT:"用户输入ID", ID_WORKER:"全局唯一ID (数字类型唯一ID)", UUID:"全局唯一ID UUID", AUTO: MP自动决定; 11 id-type: id_worker 12 #字段策略 IGNORED:"忽略判断", NOT_NULL:"非 NULL 判断", NOT_EMPTY:"非空判断" 13 field-strategy: not_empty 14 #驼峰下划线转换 15 column-underline: true 16 #数据库大写下划线转换 17 capital-mode: true 18 #table-prefix: sys_ 19 #逻辑删除配置 20 logic-delete-value: 1 21 logic-not-delete-value: 0 22 # 数据库类型 23 db-type: mysql 24 #刷新mapper 调试神器 25 refresh: true 26 # 原生配置 27 configuration: 28 map-underscore-to-camel-case: true 29 cache-enabled: false 30 # 打印sql日志 31 log-impl: org.apache.ibatis.logging.stdout.StdOutImpl
-
乐观锁、逻辑删除、物流分页插件配置
1 /** 2 * MybatisPlus 扫描mapper配置 以及 插件配置 3 * @Author: xhq 4 * @Version: 1.0 5 */ 6 @Configuration 7 @MapperScan({"com.xxx.project.biz.*.mapper","com.xxx.project.biz.*.*.mapper"}) 8 public class MybatisPlusConfig { 9 10 /** 11 * 物理分页 插件 12 * @return 13 */ 14 @Bean 15 public PaginationInterceptor paginationInterceptor() { 16 return new PaginationInterceptor(); 17 } 18 19 /** 20 * 乐观锁 插件 21 * @return 22 */ 23 @Bean 24 public OptimisticLockerInterceptor optimisticLockerInterceptor() { 25 return new OptimisticLockerInterceptor(); 26 } 27 28 /** 29 * 逻辑删除 30 * @return 31 */ 32 @Bean 33 public ISqlInjector sqlInjector() { 34 return new LogicSqlInjector(); 35 } 36 }
1 /** 2 * Entity基类 3 * 4 * @author xhq 5 */ 6 public class BaseEntity implements Serializable { 7 8 private static final long serialVersionUID = 1L; 9 10 /** 主键id @JSONField该注解是解决Long类型太长传值前端精度丢失*/ 11 @JSONField(serializeUsing= ToStringSerializer.class) 12 private Long id; 13 14 /** 创建时间 插入自动填充 */ 15 @TableField(fill = FieldFill.INSERT) 16 private Date createTime; 17 18 /** 更新时间 插入和更新自动填充 */ 19 @TableField(fill = FieldFill.INSERT_UPDATE) 20 private Date updateTime; 21 22 /** 备注 */ 23 private String remark; 24 25 /** 逻辑删除 0:正常 1:删除 */ 26 @TableLogic 27 private Integer deleted; 28 29 /** 乐观锁 */ 30 @Version 31 private Integer version; 32 33 getter and setter... ... 34 }
-
自动填充字段配置
1 /** 2 * mybatis-plus 自定义自动填充字段处理器 3 * @Author: xhq 4 * @Version: 1.0 5 */ 6 @Component 7 public class MyMetaObjectHandler implements MetaObjectHandler { 8 9 @Override 10 public void insertFill(MetaObject metaObject) { 11 this.setInsertFieldValByName("createTime", new Date(), metaObject); 12 this.setInsertFieldValByName("updateTime", new Date(), metaObject); 13 } 14 15 @Override 16 public void updateFill(MetaObject metaObject) { 17 this.setUpdateFieldValByName("updateTime", new Date(), metaObject); 18 } 19 }