整点活,MyBatis-Plus学习笔记(SQL注入器——选装件)

前言

一些MyBatisPlus已经实现好的自定义SQL方法
这个是自己学习时候记得笔记uy要是想详细了解可以去MP官网,上边有更详细的配置流程以及视频教学:MyBatis-Plus

InsertBatchSomeColumn

批量新增数据,自选字段insert
添加步骤:

  1. 自定义Injector中加入该选装件
@Component
public class MySQLInjector extends DefaultSqlInjector {

    @Override
    public List<AbstractMethod> getMethodList(Class<?> mapperClass) {
        // 1. 获取父级创建的方法
        List<AbstractMethod> methodList = super.getMethodList(mapperClass);
        // 2. 增加组件,新增操作时,不会对逻辑删除字段进行设置
        /** InsertBatchSomeColumn有无参和带参两种构造方法
         *	无参构造会将插入记录的所有属性进行设置
         *	带参构造可自定义具体对一条数据的哪些属性进行设置
         */
        methodList.add(new InsertBatchSomeColumn(new Predicate<TableFieldInfo>() {
            @Override
            public boolean test(TableFieldInfo tableFieldInfo) {
                return !tableFieldInfo.isLogicDelete();
                //return !tableFieldInfo.isLogicDelete() && !tableFieldInfo.getColumn().equals("name");
                // 排除多个字段的时候,用&&
            }
        }));
        // 3. 返回
        return methodList;
    }
}
  1. 在Mapper中添加这个方法
    /**
     * 批量插入
     * @param list 插入数据
     * @return 影响记录行数
     */
    int insertBatchSomeColumn(List<Type> list);
  1. 执行操作
    public void insertBatch(){
        Type type = new Type();
        type.setName("abc");
        type.setParentId(2);
        Type type2 = new Type();
        type2.setName("cde");
        type2.setParentId(2);

        List<Type> types = Arrays.asList(type,type2);
        System.out.println("影响行数:"+typeMapper.insertBatchSomeColumn(types));
    }

执行结果如下:(没有对设置的deleted字段进行插入)
整点活,MyBatis-Plus学习笔记(SQL注入器——选装件)_第1张图片

  • 注意

对于以设置默认值的字段,插入时为null,这时候数据库中数据就是 null
只在MySql中做过测试,其他数据库慎用

LogicDeleteByIdWithFill

根据id进行逻辑删除数据,并带自动填充功能
使用场景:当进行逻辑删除时,有些字段也要跟着更改(注销用户积分清零)
添加步骤:

  1. 自定义Injector中加入该选装件
@Component
public class MySQLInjector extends DefaultSqlInjector {

    @Override
    public List<AbstractMethod> getMethodList(Class<?> mapperClass) {
        // 1. 获取父级创建的方法
        List<AbstractMethod> methodList = super.getMethodList(mapperClass);
        // 2. 增加组件,逻辑删除时,可以对其他字段也进行修改
        methodList.add(new LogicDeleteByIdWithFill());
        // 3. 返回
        return methodList;
    }
}
  1. Mapper中添加对应的方法声明
 /**
     * 逻辑删除并填充
     * @param type 操作实体
     * @return 影响行数
     */
    int deleteByIdWithFill(Type type);
  1. 方法调用
   public void logicDeleteFill(){
        Type type = new Type();
        type.setTypeId(19);         // 根据id删除
        type.setName("我不好");    // 修改的字段,改哪个设置哪个就行
        System.out.println("影响行数:"+typeMapper.deleteByIdWithFill(type));
    }
  1. 在对应实体类要进行填充的字段上声明填充时机
    @TableField(fill = FieldFill.UPDATE)
    private String name;	// 设置name属性,在更新时执行填充
  1. 执行
    整点活,MyBatis-Plus学习笔记(SQL注入器——选装件)_第2张图片

AlwaysUpdateSomeColumnById

根据Id更新固定的某些字段,更新时可以设置哪些字段需要更新,哪些字段不需要更新;如果设置需要更新的字段没有设置值,更新为null。
( 这个功能感觉没多大用。。。)
添加步骤:

  1. 自定义Injector中加入该选装件
@Component
public class MySQLInjector extends DefaultSqlInjector {

    @Override
    public List<AbstractMethod> getMethodList(Class<?> mapperClass) {
        // 1. 获取父级创建的方法
        List<AbstractMethod> methodList = super.getMethodList(mapperClass);
        // 2. 增加组件,根据id更新时,固定更新某些字段( 不包含逻辑删除字段 )
        /** AlwaysUpdateSomeColumnById有无参和带参两种构造方法
         *	无参构造回对执行update的记录有设置值的数据进行更新
         *	带参构造可自定义具体对一条数据的哪些属性进行设置(即使设置值也不更新)
         */
        methodList.add(new AlwaysUpdateSomeColumnById(new Predicate<TableFieldInfo>() {
            @Override
            public boolean test(TableFieldInfo tableFieldInfo) {
                // 不会将 name字段的数据放入 update 语句的 setreturn !tableFieldInfo.getColumn().equals("name");
            }
        }));
        // 3. 返回
        return methodList;
    }
}

  1. 将方法添加至对应Mapper中
    /**
     *根据id更新时,固定更新某些字段( 不包含逻辑删除字段 )
     * @param type 更新对象
     * @return 影响行数
     */
    int alwaysUpdateSomeColumnById(@Param(Constants.ENTITY) Type type);
  1. 执行对应方法
    public void alwaysUpdate(){
        Type type = new Type();
        type.setTypeId(20);         // 根据id删除
        type.setName("我不好");    // 修改的字段,改哪个设置哪个就行
        type.setParentId(5);
        System.out.println("影响行数:"+typeMapper.alwaysUpdateSomeColumnById(type));
    }

执行结果如下,虽没有对name进行修改,但是对createTime进行了修改,这是不应该的
整点活,MyBatis-Plus学习笔记(SQL注入器——选装件)_第3张图片

你可能感兴趣的:(整点活,MyBatis-Plus学习笔记(SQL注入器——选装件))