Mybatis-Plus插件配置


 

  • 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 }

     


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM