MyBatisPlus高级操作(自定义ID生成器、自定义填充、逻辑删除、3.x注册乐观锁,分页插件)

自定义ID生成器

编写ID生成器

import com.baomidou.mybatisplus.core.incrementer.IdentifierGenerator;

import java.util.Random;

public class CustomIdGenerator implements IdentifierGenerator {

    private final char[] arrays = {
        '0','1','2','3','4','5','6','7','8','9','a','b','c','d','e','f','g','h','i','j','k','l','m','n','o','p','q','r','s','t','u','v','w','x','y','z'
    };

    private final Random random = new Random();

    @Override
    public Number nextId(Object entity) {

        long ops =  0L;
        for (int i = 0; i < 18; i++) {
            int tmp = random.nextInt(9)+1;
            ops = ops * 10 + tmp;
        }
        return ops;
    }

    @Override
    public String nextUUID(Object entity) {
        StringBuffer buffer = new StringBuffer();
        String name = entity.getClass().getSimpleName().toLowerCase();
        buffer.append(name).append("-");
        for (int i = 0; i < 9; i++) {
            buffer.append(arrays[random.nextInt(26) + 10]);
            buffer.append(arrays[random.nextInt(10)]);
        }
        return buffer.toString();
    }
}

注入spring容器

@Configuration
public class MybatisPlusConfig {

    // 自定义ID 生成器
    @Bean
    public IdentifierGenerator identifierGenerator() {
        return new CustomIdGenerator();
    }
}

 

自定义填充

 自定义对时间的填充

import com.baomidou.mybatisplus.core.handlers.MetaObjectHandler;
import org.apache.ibatis.reflection.MetaObject;

import java.util.Date;

public class MyBatisPlusMetaObjectHandler implements MetaObjectHandler {

    @Override
    public void insertFill(MetaObject metaObject) {
        this.setFieldValByName("gmt_create",new Date(),metaObject);
        this.setFieldValByName("gmt_update",new Date(),metaObject);
    }

    @Override
    public void updateFill(MetaObject metaObject) {
        this.setFieldValByName("gmt_update",new Date(),metaObject);
    }
}

注入Spring容器

@Configuration
public class MybatisPlusConfig {


    // 自定义字段填充
    @Bean
    public MetaObjectHandler metaObjectHandler() {
        return new MyBatisPlusMetaObjectHandler();
    }

}

逻辑删除

配置文件

mybatis-plus.global-config.db-config.logic-delete-value=1
mybatis-plus.global-config.db-config.logic-not-delete-value=0

删除为1,没删除为0

添加字段并且加@TableLogic注解

@TableLogic
private Integer deleted;

注册乐观锁,分页插件

以user表为例子,添加 version字段,并且 有@Version注解

@Data
@TableName("yy_user")
public class User implements Serializable {


    @TableId(type = IdType.ASSIGN_UUID)
    private String id;

    private String name;
    private String password;

    @TableField(fill = FieldFill.INSERT)
    private Date gmt_create;

    @TableField(fill = FieldFill.INSERT_UPDATE)
    private Date gmt_update;

    @Version
    private Integer version;

    @TableLogic
    private Integer deleted;
}

注册插件,2.x版本直接将OptimisticLockerInterceptor添加到Spring容器

@Configuration
public class MybatisPlusConfig {

    // 插件 注册
    @Bean
    public MybatisPlusInterceptor mybatisPlusInterceptor() {
        MybatisPlusInterceptor interceptor = new MybatisPlusInterceptor();
        interceptor.addInnerInterceptor(new OptimisticLockerInnerInterceptor());
        interceptor.addInnerInterceptor(new PaginationInnerInterceptor());
        return interceptor;
    }
}

 

本文章参考了 @狂胜说Java的哔哩哔哩视频: https://www.bilibili.com/video/BV17E411N7KN

©️2020 CSDN 皮肤主题: 数字20 设计师:CSDN官方博客 返回首页