重写Mybatis-Plus的BaseMapper的增加、删除、修改操作

重写Mybatis-Plus的BaseMapper的增加、删除、修改操作

Mybatis-plus的deleteById()方法并不会去更新实体类中被标注的修改时间,所以通过重写deleteById()方法逻辑来实现这个功能。

1、创建自定义的Sql注入器

@Component
public class CustomSqlInjector extends DefaultSqlInjector {
 
    @Override
    public List<AbstractMethod> getMethodList(Class<?> mapperClass) {
        // 按照原有的对象进行复制,然后将要自定义的方法对象进行更换
        // 将deleteById方法和DeleteBatchByIds进行更换
        return Stream.of(new Insert(), new Delete(), new DeleteByMap(), new CustomDeleteById(),
                new CustomDeleteBatchByIds(), new Update(), new UpdateById(), new SelectById(),
                new SelectBatchByIds(), new SelectByMap(), new SelectOne(),
                new SelectCount(), new SelectMaps(), new SelectMapsPage(),
                new SelectObjs(), new SelectList(), new SelectPage()).collect(Collectors.toList());
    }
}

为了保证其他操作不受影响,将父类DefaultSqlInjectorgetMethodList()方法复制到自定义的Sql注入器中,然后将需要自定义的方法换成自定义的对象即可

2、创建自定义的删除方法

将原有方法复制到自定义的类中,然后进行修改
public class MyDeleteById extends AbstractMethod {

    @Override
    public MappedStatement injectMappedStatement(Class<?> mapperClass, Class<?> modelClass, TableInfo tableInfo) {
        SqlMethod sqlMethod = SqlMethod.LOGIC_DELETE_BY_ID;
        String sql;
        if (tableInfo.isLogicDelete()) {
            // 获取被标注要自动填充的字段
            List<TableFieldInfo> fieldInfos = tableInfo.getFieldList().stream().filter(TableFieldInfo::isWithUpdateFill).collect(Collectors.toList());
            if (CollectionUtils.isNotEmpty(fieldInfos)) {
                String sqlSet = "SET " + fieldInfos.stream().map(i -> {
                    if (i.getPropertyType() == LocalDateTime.class) {
                        // 当为LocalDateTime类型时,添加当前时间
                        return i.getColumn() + "=now(), ";
                    }else {
                        return null;
                    }
                }).collect(Collectors.joining("")) + tableInfo.getLogicDeleteSql(false, false);
                sql = String.format(sqlMethod.getSql(), tableInfo.getTableName(), sqlSet, tableInfo.getKeyColumn(), tableInfo.getKeyProperty(), tableInfo.getLogicDeleteSql(true, true));
            } else {
                sql = String.format(sqlMethod.getSql(), tableInfo.getTableName(), this.sqlLogicSet(tableInfo), tableInfo.getKeyColumn(), tableInfo.getKeyProperty(), tableInfo.getLogicDeleteSql(true, true));
            }
        } else {
            sqlMethod = SqlMethod.DELETE_BY_ID;
            sql = String.format(sqlMethod.getSql(), tableInfo.getTableName(), tableInfo.getKeyColumn(), tableInfo.getKeyProperty());
        }

        SqlSource sqlSource = this.languageDriver.createSqlSource(this.configuration, sql, modelClass);
        return this.addUpdateMappedStatement(mapperClass, modelClass, this.getMethod(sqlMethod), sqlSource);
    }
}

因为入参只有Id,所以修改时间只能通过now()方法获取,其他需要入参的功能是实现不了的,所以要判断字段类型是否是LocalDateTime.class,是的话才拼接,不是就加个""字符串就行。

这样在调用deleteById()就会同时修改修改时间了。

  • 2
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值