Mybatis Plus配置类

  • Post author:
  • Post category:其他




通用实体BaseEntity

@Data
public class BaseEntity implements Serializable {

    @ApiModelProperty(value = "id")
    @TableId(type = IdType.AUTO)
    private Long id;

    @ApiModelProperty(value = "创建时间")
    @TableField("create_time")
    private Date createTime;

    @ApiModelProperty(value = "更新时间")
    @TableField("update_time")
    private Date updateTime;

    @ApiModelProperty(value = "逻辑删除(1:已删除,0:未删除)")
    @TableLogic
    @TableField("is_deleted")
    private Integer isDeleted;

    @ApiModelProperty(value = "其他参数")
    @TableField(exist = false)
    private Map<String,Object> param = new HashMap<>();
}



分页插件配置

@Configuration
public class MybatisPlusConfig {

    /**
     * 配置分页插件
     * @return
     */
    @Bean
    public MybatisPlusInterceptor mybatisPlusInterceptor(){
        MybatisPlusInterceptor mybatisPlusInterceptor = new MybatisPlusInterceptor();
        //分页插件
        mybatisPlusInterceptor.addInnerInterceptor(new PaginationInnerInterceptor(DbType.MYSQL));
        return mybatisPlusInterceptor;
    }
}



时间自动填充

@Slf4j
@Component
public class MymetaObjectHandler implements MetaObjectHandler {
    //使用mp实现添加的时候,这个方法执行
    @Override
    public void insertFill(MetaObject metaObject) {
        this.setFieldValByName("createTime",new Date(),metaObject);
        this.setFieldValByName("updateTime",new Date(),metaObject);
    }
    //使用mp实现修改的时候,使用这个方法执行。
    @Override
    public void updateFill(MetaObject metaObject) {
        //修改的时候只需要改变修改时间。
        this.setFieldValByName("updateTime", new Date(), metaObject);
    }
}



数据源

spring:
  datasource:
    driver-class-name: com.mysql.cj.jdbc.Driver
    username: root
    password: root
    url: jdbc:mysql://localhost:3306/test_mybaitsplus?useUnicode=true&characterEncoding=utf-8&serverTimezone=UTC

mybatis-plus:
#  configuration:
#    log-impl: org.apache.ibatis.logging.stdout.StdOutImpl
  global-config:
    db-config:
      logic-delete-field: deleted
      logic-delete-value: 1
      logic-not-delete-value: 0



版权声明:本文为qq_45297578原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。