MybatisPlus版本
<dependency>
<groupId>com.baomidou</groupId>
<artifactId>mybatis-plus-boot-starter</artifactId>
<version>3.4.2</version>
</dependency>
<dependency>
<groupId>com.baomidou</groupId>
<artifactId>mybatis-plus-extension</artifactId>
<version>3.4.2</version>
</dependency>
创建LogicBatchDeleteWithFill
public class LogicBatchDeleteWithFill extends AbstractMethod {
private static final String MAPPER_METHOD = "batchDeleteWithFill";
@Override
public MappedStatement injectMappedStatement(Class<?> mapperClass, Class<?> modelClass, TableInfo tableInfo) {
String sql;
SqlMethod sqlMethod = SqlMethod.LOGIC_DELETE;
if (tableInfo.isWithLogicDelete()) {
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, false);
sql = String.format(sqlMethod.getSql(), tableInfo.getTableName(), sqlSet," ",
sqlWhereEntityWrapper(true, tableInfo));
} else {
sql = String.format(sqlMethod.getSql(), tableInfo.getTableName(), sqlLogicSet(tableInfo), "",
sqlWhereEntityWrapper(true, tableInfo));
}
SqlSource sqlSource = languageDriver.createSqlSource(configuration, sql, modelClass);
return this.addUpdateMappedStatement(mapperClass, modelClass, MAPPER_METHOD, sqlSource);
} else {
sqlMethod = SqlMethod.DELETE;
sql = String.format(sqlMethod.getSql(), tableInfo.getTableName()," ", sqlWhereEntityWrapper(true, tableInfo));
SqlSource sqlSource = languageDriver.createSqlSource(configuration, sql, modelClass);
return this.addDeleteMappedStatement(mapperClass, MAPPER_METHOD, sqlSource);
}
}
}
创建LogicDeleteSqlInjector
public class LogicDeleteSqlInjector extends DefaultSqlInjector {
@Override
public List<AbstractMethod> getMethodList(Class<?> mapperClass) {
List<AbstractMethod> methodList = super.getMethodList(mapperClass);
methodList.add(new LogicDeleteByIdWithFill());
methodList.add(new LogicBatchDeleteWithFill());
methodList.add(new AlwaysUpdateSomeColumnById());
return methodList;
}
}
创建MyBaseMapper
public interface MyBaseMapper<T> extends BaseMapper<T> {
/**
* 根据id逻辑删除并填充字段
* @param entity
* @return
*/
int deleteByIdWithFill(T entity );
/**
* 批量逻辑删除并填充字段
* @param entity
* @param wrapper
* @return
*/
int batchDeleteWithFill(@Param(Constants.ENTITY) T entity, @Param(Constants.WRAPPER) Wrapper<T> wrapper);
}
配置
public class MybatisPlusConfig {
/** 省略代码 **/
/**
* 逻辑删除
* @return
*/
@Bean
public LogicDeleteSqlInjector logicDeleteSqlInjector() {
return new LogicDeleteSqlInjector();
}
}
继承MyBaseMapper
public interface SysOperLogMapper extends MyBaseMapper<SysOperLog> {
}
至此扩展完成,在调用mapper中时,可以同时使用扩展方法及mybatis-plus自带的方法。
需要扩展service的下面链接自取:
mybatis-plus扩展IServicehttps://blog.csdn.net/qq_43040552/article/details/129276408