1. 程式人生 > 其它 >mybatis-plus使用心得

mybatis-plus使用心得

  mybatis-plus是一款基於mybatis的持久層框架,在mybatis上只做增強不做改變。基本使用流程:

  1. 匯入依賴座標:
    <dependency>
    <groupId>com.baomidou</groupId>
    <artifactId>mybatis-plus-boot-starter</artifactId>
    <version>Latest Version</version>
    </dependency>
  2. 在yml檔案中配置使用的資料來源。
  3. mybatisplus分別在dao層和service層提供了基礎的介面,提供了基礎的增刪改查實現。只需要繼承BaseMapper和Iservice介面。
  4. 在mybatis-plus中集成了分頁外掛,需要在配置類中將外掛配置成bean加入容器中。
    @Bean
        public MybatisPlusInterceptor mybatisPlusInterceptor(){
            MybatisPlusInterceptor mybatisPlusInterceptor = new MybatisPlusInterceptor();
            mybatisPlusInterceptor.addInnerInterceptor(new PaginationInnerInterceptor(DbType.MYSQL));
            
    return mybatisPlusInterceptor; }
  5. 整合日誌配置,在yml檔案中配置:
    mybatis-plus:
      configuration:
        log-impl: org.apache.ibatis.logging.stdout.StdOutImpl
  6. id生成:mybatis-plus自帶主鍵生成策略,使用註解
    @TableId(type = IdType.AUTO)
    private Long id;
  7. 自動填充,一般資料庫中都有修改時間,新增時間等欄位,mybatis-plus可以通過配置生成策略自動生成。程式碼中在實體類上需要自動填充的欄位使用註解標註。
    @TableField(value = "gmt_create",fill = FieldFill.INSERT)
    
    private Date gmtCreate; @TableField(value = "gmt_modified",fill = FieldFill.INSERT_UPDATE) private Date gmtModified;

    需要將自動填充策略作為bean新增到容器中,自定義填充欄位的生成策略。

    @Slf4j
    @Component
    public class MyMetaObjectHandler implements MetaObjectHandler {
        @Override
        public void insertFill(MetaObject metaObject) {
            log.info("start insert fill....");
            this.setFieldValByName("gmtCreate",new Date(),metaObject);
            this.setFieldValByName("gmtModified",new Date(),metaObject);
        }
    
        @Override
        public void updateFill(MetaObject metaObject) {
            log.info("start update fill....");
            this.setFieldValByName("gmtModified",new Date(),metaObject);
        }
    }
  8. 邏輯刪除欄位處理,實際開發中資料庫的欄位一般不會進行物理刪除,通常使用邏輯刪除修改標記位。mybatis-plus中使用註解@Tablelogic實現,資料庫增加邏輯刪除欄位,實體類上標註
    @TableLogic(value = "1",delval = "0") //邏輯刪除
        private Integer deleted;

    在yml檔案中指明邏輯刪除標記位:

    mybatis-plus:
      global-config:
        db-config:
          logic-delete-field: deleted # 邏輯刪除的欄位
          logic-delete-value: 1 # 1代表刪除
          logic-not-delete-value: 0 # 0代表未刪除
  9. 樂觀鎖/悲觀鎖 mybatis-plus中使用@Version欄位實現樂觀鎖,資料庫新增version欄位,實體類處理:
    @Version
        private Integer version;

    將樂觀鎖元件註冊到IOC中

    @Bean
        public OptimisticLockerInnerInterceptor optimisticLockerInnerInterceptor(){
            return new OptimisticLockerInnerInterceptor();
        }
  10. mybatis-plus處理列舉欄位,使用自定義的列舉類表示,可以使用@EnumValue註解加在要儲存進資料庫中的欄位上,mp將會自動對映。使用@JsonValue加在列舉類中要返回給前端的屬性的get方法上,返回時將會自動對映,如SexEnum中返回前端的就是(“男人”/“女人”).
    public enum SexEnum {
        MAN(1, "男人"),
        WOMAN(0, "女人");
    
        @EnumValue
        private int code;
    
        private String value;
    
    
        SexEnum(int code, String value) {
            this.code = code;
            this.value = value;
        }
    
    
        public int getCode() {
            return code;
        }
    
        @JsonValue
        public String getValue() {
            return value;
        }
    }

    yml檔案中配置

    mybatis-plus:
      type-enums-package: com.codeyoung.json.domain # 指定列舉類所在的包
      configuration:
        default-enum-type-handler: org.apache.ibatis.type.EnumOrdinalTypeHandler # 指定預設列舉類處理器
  11. mybatis-plus處理json欄位的資料,如果資料庫中欄位為json格式的字串,可以使用@TableFiled(Typehandler=JacksonTypeHandler.class)實現javatype到jdbctype的互轉,可以將實體類物件轉為json字串存入到表中。
    @Data
    public class Foo {
        private Long id;
        private String bar;
        private Bar barObj;
        private Date createTime;
    }
    
    @Data
    public class Bar {
        private String name;
        private Integer quz;
        private Date timestamp;
    }