MyBaitsPlus逻辑删除时,自动添加删除时间

  • 数据表中添加 delete_time 字段
ALTER TABLE `table_name` 
ADD COLUMN `delete_time` bigint NULL DEFAULT NULL COMMENT '删除时间' 
  • 实体父类Entity中添加映射
 	@TableField(value = "delete_time")
    private Long deleteTime;
  • 重写SQL 注入器 MySqlInjector 替换 默认注入器 DefaultSqlInjector
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());

你可能感兴趣的:(java,MyBaitsPlus,java)