ALTER TABLE `table_name`
ADD COLUMN `delete_time` bigint NULL DEFAULT NULL COMMENT '删除时间'
@TableField(value = "delete_time")
private Long deleteTime;
public class MySqlInjector extends AbstractSqlInjector {
@Override
public List<AbstractMethod> getMethodList(Class<?> mapperClass) {
return Stream.of(
new Insert(),
new ExDelete(),
new ExDeleteByMap(),
new ExDeleteById(),
new ExDeleteBatchByIds(),
new Update(),
new UpdateById(),
new SelectById(),
new SelectBatchByIds(),
new SelectByMap(),
new SelectOne(),
new SelectCount(),
new SelectMaps(),
new SelectMapsPage(),
new SelectObjs(),
new SelectList(),
new SelectPage(),
//根据ID查询数据,无视逻辑删除字段
new SelectByIdWithDeleted(),
//根据ID删除数据,真实删除
new RemoveRealById()
).collect(toList());
}
}
public class ExDelete extends Delete {
@Override
protected String sqlLogicSet(TableInfo table) {
return LogicDeleteUtils.sqlLogicSet(table);
}
}
public class ExDeleteBatchByIds extends DeleteBatchByIds {
@Override
protected String sqlLogicSet(TableInfo table) {
return LogicDeleteUtils.sqlLogicSet(table);
}
}
public class ExDeleteById extends DeleteById {
@Override
protected String sqlLogicSet(TableInfo table) {
return LogicDeleteUtils.sqlLogicSet(table);
}
}
public class ExDeleteByMap extends DeleteByMap {
@Override
protected String sqlLogicSet(TableInfo table) {
return LogicDeleteUtils.sqlLogicSet(table);
}
}
public class RemoveRealById extends AbstractMethod {
@Override
public MappedStatement injectMappedStatement(Class<?> mapperClass, Class<?> modelClass, TableInfo tableInfo) {
String sql;
ExSqlMethod sqlMethod = ExSqlMethod.REMOVE_REAL_BY_ID;
sql = String.format(sqlMethod.getSql(), tableInfo.getTableName(), tableInfo.getKeyColumn(),
tableInfo.getKeyProperty());
SqlSource sqlSource = languageDriver.createSqlSource(configuration, sql, Object.class);
return this.addDeleteMappedStatement(mapperClass, sqlMethod.getMethod(), sqlSource);
}
}
public class SelectByIdWithDeleted extends AbstractMethod {
@Override
public MappedStatement injectMappedStatement(Class<?> mapperClass, Class<?> modelClass, TableInfo tableInfo) {
ExSqlMethod sqlMethod = ExSqlMethod.SELECT_BY_ID_WITH_DELETED;
String sql = String.format(sqlMethod.getSql(),
sqlSelectColumns(tableInfo, false), tableInfo.getTableName(), tableInfo.getKeyColumn(), tableInfo.getKeyProperty());
SqlSource sqlSource = languageDriver.createSqlSource(configuration, sql, modelClass);
return this.addSelectMappedStatementForTable(mapperClass, sqlMethod.getMethod(), sqlSource, tableInfo);
}
}
public class LogicDeleteUtils {
public static final String DELETE_TIME = "delete_time";
public LogicDeleteUtils() {
}
public static String sqlLogicSet(TableInfo table) {
String set = "SET " + table.getLogicDeleteSql(false, false);
Optional<TableFieldInfo> optional = table.getFieldList().stream()
.filter(v -> v.getColumn().equals(DELETE_TIME) && ClassUtils.isAssignable(v.getPropertyType(), Long.class))
.findFirst();
return optional.map(v -> String.format("%s ,%s=NOW() ", set, DELETE_TIME)).orElse(set);
}
public static String sqlLogicSet(Class<?> mapper, String method) {
return mapper.getName() + StrPool.DOT + method;
}
}
public enum ExSqlMethod {
/**
* 根据 id 查询数据,无视逻辑删除字段
*/
SELECT_BY_ID_WITH_DELETED("selectByIdWithDeleted", "根据 id 查询数据,无视逻辑删除字段", ""),
/**
* 根据ID删除数据,真实删除
*/
REMOVE_REAL_BY_ID("removeRealById", "根据ID删除数据,真实删除", "");
private final String method;
private final String desc;
private final String sql;
ExSqlMethod(String method, String desc, String sql) {
this.method = method;
this.desc = desc;
this.sql = sql;
}
public String getMethod() {
return method;
}
public String getDesc() {
return desc;
}
public String getSql() {
return sql;
}
}
@Bean
@ConditionalOnMissingBean
public ISqlInjector getMySqlInjector() {
return new MySqlInjector();
}
自定义 SQL 注入器失效,可能是因为使用自定义SqlSessionFactory,不会初始化刚开始自定义的 SQL 注入器了,
// 注入sql注入器
globalConfig.setSqlInjector(new MySqlInjector());