SpringBoot ---- Mybatis Plus plug-in

Autofill

Notes entity fields

public class User {
  
  @TableField(fill = FieldFill.INSERT)
  private Date createTime;
  @TableField(fill = FieldFill.INSERT_UPDATE)
  private Date updateTime;
}

Custom implementation class

@Component
public class MyMetaObjectHandler implements MetaObjectHandler {

  	//
    @Override
    public void insertFill(MetaObject metaObject) {
        this.strictInsertFill(metaObject, "createTime", LocalDateTime.class, LocalDateTime.now()); // 起始版本 3.3.0(推荐使用)
        this.fillStrategy(metaObject, "createTime", LocalDateTime.now()); // 也可以使用(3.3.0 该方法有bug请升级到之后的版本如`3.3.1.8-SNAPSHOT`)
        /* 上面选其一使用,下面的已过时(注意 strictInsertFill 有多个方法,详细查看源码) */
        //this.setFieldValByName("operator", "Jerry", metaObject);
        //this.setInsertFieldValByName("operator", "Jerry", metaObject);
    }

    @Override
    public void updateFill(MetaObject metaObject) {
        log.info("start update fill ....");
        this.strictUpdateFill(metaObject, "updateTime", LocalDateTime.class, LocalDateTime.now()); // 起始版本 3.3.0(推荐使用)
        this.fillStrategy(metaObject, "updateTime", LocalDateTime.now()); // 也可以使用(3.3.0 该方法有bug请升级到之后的版本如`3.3.1.8-SNAPSHOT`)
        /* 上面选其一使用,下面的已过时(注意 strictUpdateFill 有多个方法,详细查看源码) */
        //this.setFieldValByName("operator", "Tom", metaObject);
        //this.setUpdateFieldValByName("operator", "Tom", metaObject);
    }
}

Optimistic locking plug

You need a database table to join versionthe field

Notes entity fields @Version

@Version
private Integer version

Write configuration class

@Configuration
@MapperScan("")  // 可以写在这里或者启动类上面
public class MybatisPlusConfig {
  
  	// 乐观锁插件
    @Bean
    public OptimisticLockerInterceptor optimisticLockerInterceptor() {
        return new OptimisticLockerInterceptor();
    }
}

Pagination plug-in

Write configuration class

@Configuration
@MapperScan("")  // 可以写在这里或者启动类上面
public class MybatisPlusConfig {
  
  	// 分页插件
    @Bean
    public PaginationInterceptor paginationInterceptor() {
        PaginationInterceptor paginationInterceptor = new PaginationInterceptor();
        // 设置请求的页面大于最大页后操作, true调回到首页,false 继续请求  默认false
        // paginationInterceptor.setOverflow(false);
        // 设置最大单页限制数量,默认 500 条,-1 不受限制
        // paginationInterceptor.setLimit(500);
        // 开启 count 的 join 优化,只针对部分 left join
        // paginationInterceptor.setCountSqlParser(new JsqlParserCountOptimize(true));
        return paginationInterceptor;
    }
}

Paging query

public Test{
  
    @Test
    void testPage() {
        // 创建 Page 对象,传入 当前页 和 每页记录数
   			Page<User> page = new Page<>(1,5);
        // 分页查询
      	userMapper.selectPage(page, null);
      	
        /**
         * 分页数据
         * page.getCurrent()  ---- 当前页
         * page.getPages()    ---- 总页数
         * page.getSize()     ---- 每页记录数
         * page.getTotal()    ---- 总记录数
         * page.getRecords()  ---- List 集合
         * page.hasNext()     ---- 下一页
         * page.hasPrevious() ---- 上一页
         */
    }
}

Tombstone

Need to be added in the data deletedfield

Notes entity fields @TableLogic

@TableLogic
private Integer deleted;

Write configuration class

3.1.1 begin this step is no longer required

// 
@Configuration
public class MyBatisPlusConfiguration {

    @Bean
    public ISqlInjector sqlInjector() {
        return new LogicSqlInjector();
    }
}

Performance Analysis plug-in

Write configuration class

Recommend that only developers to use, not recommended online

@Configuration
@MapperScan("com.baomidou.cloud.service.*.mapper*")
public class MybatisPlusConfig {

    /**
     * SQL执行效率插件
     */
    @Bean
    @Profile({"dev","test"})// 设置 dev test 环境开启
    public PerformanceInterceptor performanceInterceptor() {
        // 使用默认配置
        // return new PerformanceInterceptor();
        
        // 自定义配置
        PerformanceInterceptor performanceInterceptor = new PerformanceInterceptor();
        performanceInterceptor.setMaxTime(100);// 单位:ms,超过则不执行
        performanceInterceptor.setFormat(true);  // SQL 格式化
        return performanceInterceptor;
    }
}

Set dev environment

Can create different profiles for each environment application-dev.properties, application-test.properties,application-prod.properties

#环境设置:dev、test、prod
spring.profiles.active=dev

Conditions constructor

For more details refer to the official documentation

Guess you like

Origin www.cnblogs.com/qq188380780/p/12595982.html