mybatis-plus中做逻辑删除是 只是简单的修改逻辑删除的字段, 如果想要在删除的同时添加删除人,和删除时间该如何实现呢?对于填充数据 我们可以使用 mybatis-plus的自动填充功能
但是不是所有的删除都会触发自动填充功能
在mybatis-plus老版本中 删除是不能触发自动填充的
需要使用官方的Sql 注入器LogicDeleteByIdWithFill 而且调用方法为deleteByIdWithFill
在这里不具体赘述了
@Component
public class MySqlInjector extends DefaultSqlInjector {
@Override
public List getMethodList(Class> mapperClass, TableInfo tableInfo) {
List methods = super.getMethodList(mapperClass, tableInfo);
methods.add(new LogicDeleteByIdWithFill());
return methods;
}
}
在新版本中 deleteById 默认实现了自动填充,不需要用户再手动添加sql注入器
如上图 mybatis源码中我们可以看出, 我们在使用deleteById(Entity et) 时,就可以触发自动填充
可是这也不能适用所有场景,deleteById 虽然可以实现填充的效果,但是where筛选只会根据id进行删除.
如果某些业务里面需要添加其他删除条件时无法实现了.所以我们要自己写一个sql注入器
第一步 编写注入器,这里面注意MAPPER_METHOD 是mapper中定义的方法名 ,名称随意保持一致就行
public class LogicDeleteWithFill extends AbstractMethod {
private static final String MAPPER_METHOD = "deleteWithFill";
private static final long serialVersionUID = -402373469518227818L;
@Override
public MappedStatement injectMappedStatement(Class> mapperClass, Class> modelClass, TableInfo tableInfo) {
String sql;
SqlMethod sqlMethod = SqlMethod.LOGIC_DELETE;
if (tableInfo.isWithLogicDelete()) {
List 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, false);
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 getMethodList(Class> mapperClass, TableInfo tableInfo) {
List methods = super.getMethodList(mapperClass, tableInfo);
methods.add(new LogicDeleteWithFill());
return methods;
}
}
第三步 在mapper中添加方法
只要在你的mapper中添加这个方法即可 不需要为该方法添加xml方法
注意这里的方法名要和sql注入器中的MAPPER_METHOD 保持一致
实体类类型与所在mapper保持一致(该例中的Dictionary)
完成这一步 就可以正常使用了
public interface DictionaryMapper extends baseMapper{ int deleteWithFill(@Param(Constants.ENTITY) Dictionary param, @Param(Constants.WRAPPER) Wrapper wrapper); }
测试
sql打印
mapper 接口优化 (可有可无) 为了方便使用 可以创建自己的service serviceImpl mapper
这样就不需要在每个mapper接口中添加 deleteWithFill 方法了
public interface MybaseMapperservice 接口extends baseMapper { int deleteWithFill(@Param(Constants.ENTITY) T param, @Param(Constants.WRAPPER) Wrapper wrapper); }
public interface MyServiceserviceImpl 实现类extends IService { boolean batchDelete(List idList, T t); boolean deleteWithFill(T t, Wrapper wrapper); }
public class MyServiceImpl实体父类, T extends baseEntity> extends ServiceImpl implements MyService { @Override @Transactional(rollbackFor = Exception.class) public boolean batchDelete(List idList, T t) { return deleteWithFill(t, new LambdaQueryWrapper<>(t).in(T::getId, idList)); } @Override @Transactional(rollbackFor = Exception.class) public boolean deleteWithFill(T t, Wrapper wrapper) { return SqlHelper.retBool(baseMapper.deleteWithFill(t, wrapper)); } }
@Data
@AllArgsConstructor
@NoArgsConstructor
public class baseEntity implements Serializable {
private static final long serialVersionUID = -3925407267780334782L;
private Long id;
@JsonInclude(Include.NON_NULL)
@TableField(fill = FieldFill.INSERT)
private Long createId;
@JsonInclude(Include.NON_NULL)
@TableField(fill = FieldFill.INSERT)
private LocalDateTime createTime;
@JsonIgnore
@TableField(fill = FieldFill.INSERT_UPDATE, select = false)
private Long updateId;
@JsonIgnore
@TableField(fill = FieldFill.INSERT_UPDATE, select = false)
private LocalDateTime updateTime;
@JsonIgnore
private Boolean deleted;
}
业务模块继承自定义的即可



