MyBatis_Plus 3.1.2 删除填充

先上两个官网链接:
MyBatis_Plus自动填充功能
MyBatis_Plus 逻辑删除-常见问题
这个是最新版本的说明,老版本不支持逻辑删除自动填充,问题已经在3.5.0版本之后逻辑删除自动填充更新支持了,之前的老版本会存在删除不填充的问题。

3.1.2版本是这样的

@Component
public class BaseModelFillHandler implements MetaObjectHandler {

    @Override
    public void insertFill(MetaObject metaObject) {
        String operator = ExecutionContext.getOperator();
        if (operator == null) {
            operator = "";
        }
        this.setInsertFieldValByName("createBy", operator, metaObject);
        this.setInsertFieldValByName("lastUpdateBy", operator, metaObject);
        Date now = new Date();
        this.setInsertFieldValByName("createTime", now, metaObject);
        this.setInsertFieldValByName("lastUpdateTime", now, metaObject);
    }

    @Override
    public void updateFill(MetaObject metaObject) {
        String operator = ExecutionContext.getOperator();
        if (operator == null) {
            operator = "";
        }
        this.setUpdateFieldValByName("lastUpdateBy", operator, metaObject);
        this.setUpdateFieldValByName("lastUpdateTime", new Date(), metaObject);
    }
 }

处理方法:
1.自己写wrapper拼接update语句做删除,
2.使用sql注入器将LogicDeleteByIdWithFill注入MyBatis_Plus|SQL注入

代码如下 :
注入器

public class LogicDeleteByIdWithFill extends AbstractMethod {

    private static final String MAPPER_METHOD = "deleteWithFill";//这个命名要跟Mapper方法名一致

    @Override
    public MappedStatement injectMappedStatement(Class<?> mapperClass, Class<?> modelClass, TableInfo tableInfo) {
        String sql;
        SqlMethod sqlMethod = SqlMethod.LOGIC_DELETE;
        if (tableInfo.isLogicDelete()) {
            List<TableFieldInfo> fieldInfos = tableInfo.getFieldList().stream()
                    .filter(i -> i.getFieldFill() == FieldFill.UPDATE || i.getFieldFill() == FieldFill.INSERT_UPDATE)
                    .collect(toList());
            if (CollectionUtils.isNotEmpty(fieldInfos)) {
                String sqlSet = "SET " + fieldInfos.stream().map(i -> i.getSqlSet(ENTITY_DOT)).collect(joining(EMPTY))
                        + tableInfo.getLogicDeleteSql(false, true);
                sql = String.format(sqlMethod.getSql(), tableInfo.getTableName(), sqlSet,
                        sqlWhereEntityWrapper(true, tableInfo), sqlComment());
            } else {
                sql = String.format(sqlMethod.getSql(), tableInfo.getTableName(), sqlLogicSet(tableInfo),
                        sqlWhereEntityWrapper(true, tableInfo), sqlComment());
            }
            SqlSource sqlSource = languageDriver.createSqlSource(configuration, sql, modelClass);
            return addUpdateMappedStatement(mapperClass, modelClass, MAPPER_METHOD, sqlSource);
        } else {
            sqlMethod = SqlMethod.DELETE;
            sql = String.format(sqlMethod.getSql(), tableInfo.getTableName(), sqlWhereEntityWrapper(true, tableInfo), sqlComment());
            SqlSource sqlSource = languageDriver.createSqlSource(configuration, sql, modelClass);
            return addDeleteMappedStatement(mapperClass, MAPPER_METHOD, sqlSource);
        }
    }
}

注册

@Component
public class MySqlInjector extends DefaultSqlInjector {

    @Override
    public List<AbstractMethod> getMethodList(Class<?> mapperClass) {
        List<AbstractMethod> methodList = super.getMethodList(mapperClass);
        methodList.add(new LogicDeleteByIdWithFill());
        return methodList;
    }
}

mapper方法

public interface MyBaseMapper<T> extends BaseMapper<T> {
	
    int deleteWithFill(@Param(Constants.ENTITY) T entity, @Param(Constants.WRAPPER) Wrapper<T> queryWrapper);
}

参考文档:
mybatis-plus 逻辑删除无法做自动填充的问题
【mybatis-plus】mybatis-plus 删除并自动填充

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值