先上两个官网链接:
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 删除并自动填充