Mybatisplus怎么自定义SQL注入器查询@TableLogic逻辑删除后的数据

数据库   发布日期:2023年08月09日   浏览次数:867

这篇文章主要介绍了Mybatisplus怎么自定义SQL注入器查询@TableLogic逻辑删除后的数据的相关知识,内容详细易懂,操作简单快捷,具有一定借鉴价值,相信大家阅读完这篇Mybatisplus怎么自定义SQL注入器查询@TableLogic逻辑删除后的数据文章都会有所收获,下面我们一起来看看吧。

    1 需求

    Mybatis-plus使用@TableLogic注解进行逻辑删除数据后,在某些场景下,又需要查询该数据时,又不想写SQL。

    2 解决方案

    自定义Mybatis-plus的SQL注入器一劳永逸的解决该问题

    3 方案:

    3.1 方案1,继承 AbstractMethod拼接SQL语句

    1. public class SelectIgnoreLogicDeleteByMap extends AbstractMethod {
    2. @Override
    3. public MappedStatement injectMappedStatement(Class<?> mapperClass, Class<?> modelClass, TableInfo tableInfo) {
    4. String sqlBase= "<script>SELECT %s FROM %s %s
    5. </script>";
    6. String sqlScript = this.sqlWhereByMap(tableInfo);
    7. String sql = String.format(sqlBase, this.sqlSelectColumns(tableInfo, false), tableInfo.getTableName(), sqlScript);
    8. SqlSource sqlSource = this.languageDriver.createSqlSource(this.configuration, sql, Map.class);
    9. return this.addSelectMappedStatementForTable(mapperClass, "selectIgnoreLogicDeleteByMap", sqlSource, tableInfo);
    10. }
    11. /**
    12. * 拼接where条件根据map参数
    13. *
    14. * @param table 表
    15. * @return sql
    16. */
    17. protected String sqlWhereByMap(TableInfo table) {
    18. String sqlScript;
    19. sqlScript = SqlScriptUtils.convertChoose("v == null", " ${k} IS NULL ", " ${k} = #{v} ");
    20. sqlScript = SqlScriptUtils.convertForeach(sqlScript, "cm", "k", "v", "AND");
    21. sqlScript = SqlScriptUtils.convertWhere(sqlScript);
    22. sqlScript = SqlScriptUtils.convertIf(sqlScript, String.format("%s != null and !%s", "cm", "cm.isEmpty"), true);
    23. return sqlScript;
    24. }
    25. }

    3.2. 方案2,继承 AbstractMethod拼接SQL语句

    1. public class SelectIgnoreLogicDelete extends AbstractMethod {
    2. @Override
    3. public MappedStatement injectMappedStatement(Class<?> mapperClass, Class<?> modelClass, TableInfo tableInfo) {
    4. String sqlBase = "<script>%s SELECT %s FROM %s %s %s %s
    5. </script>";
    6. String sql = String.format(sqlBase, this.sqlFirst(), this.sqlSelectColumns(tableInfo, true), tableInfo.getTableName(), this.sqlWhereEntityWrapper(true, tableInfo), this.sqlOrderBy(tableInfo), this.sqlComment());
    7. SqlSource sqlSource = this.languageDriver.createSqlSource(this.configuration, sql, modelClass);
    8. return this.addSelectMappedStatementForTable(mapperClass, "selectIgnoreLogicDelete", sqlSource, tableInfo);
    9. }
    10. /**
    11. * 拼接where条件
    12. *
    13. * @param newLine 新行
    14. * @param table 表
    15. * @return sql
    16. */
    17. protected String sqlWhereEntityWrapper(boolean newLine, TableInfo table) {
    18. String sqlScript = table.getAllSqlWhere(false, true, "ew.entity.");
    19. sqlScript = SqlScriptUtils.convertIf(sqlScript, String.format("%s != null", "ew.entity"), true);
    20. sqlScript = sqlScript + "
    21. ";
    22. sqlScript = sqlScript + SqlScriptUtils.convertIf(String.format(SqlScriptUtils.convertIf(" AND", String.format("%s and %s", "ew.nonEmptyOfEntity", "ew.nonEmptyOfNormal"), false) + " ${%s}", "ew.sqlSegment"), String.format("%s != null and %s != '' and %s", "ew.sqlSegment", "ew.sqlSegment", "ew.nonEmptyOfWhere"), true);
    23. sqlScript = SqlScriptUtils.convertWhere(sqlScript) + "
    24. ";
    25. sqlScript = sqlScript + SqlScriptUtils.convertIf(String.format(" ${%s}", "ew.sqlSegment"), String.format("%s != null and %s != '' and %s", "ew.sqlSegment", "ew.sqlSegment", "ew.emptyOfWhere"), true);
    26. sqlScript = SqlScriptUtils.convertIf(sqlScript, String.format("%s != null", "ew"), true);
    27. return newLine ? "
    28. " + sqlScript : sqlScript;
    29. }

    4. 自定义SQL注入器,注册上述自定义的方法

    1. public class CustomSqlInjector extends DefaultSqlInjector {
    2. @Override
    3. public List<AbstractMethod> getMethodList(Class<?> mapperClass, TableInfo tableInfo) {
    4. List<AbstractMethod> methodList = super.getMethodList(mapperClass, tableInfo);
    5. methodList.add(new SelectIgnoreLogicDeleteByMap());
    6. methodList.add(new SelectIgnoreLogicDelete());
    7. return methodList;
    8. }
    9. }

    5. 自定义基础mapper,声明注册的方法

    1. public interface CustomBaseMapper<T> extends BaseMapper<T> {
    2. /**
    3. * 根据map条件查询数据,并忽略逻辑删除
    4. *
    5. * @param columnMap 查询条件
    6. * @return 结果信息
    7. */
    8. List<T> selectIgnoreLogicDeleteByMap(@Param("cm") Map<String, Object> columnMap);
    9. /**
    10. * 根据条件查询数据,并忽略逻辑删除
    11. *
    12. * @param queryWrapper 查询条件
    13. * @return 结果信息
    14. */
    15. List<T> selectIgnoreLogicDelete(@Param("ew") Wrapper<T> queryWrapper);
    16. }

    6. 使用声明的方法

    6.1 业务mapper继承自定义的CustomBaseMapper

    1. @Mapper
    2. public interface UserMapper extends CustomBaseMapper<User> {
    3. }

    6.2 调用方法selectIgnoreLogicDelete

    1. @Override
    2. public List<User> getIgnoreDeleteById(Long userId) {
    3. LambdaQueryWrapper<User> queryWrapper = new LambdaQueryWrapper<>();
    4. queryWrapper.eq(User::getId,userId);
    5. return this.baseMapper.selectIgnoreLogicDelete(queryWrapper);
    6. }

    6.3 调用方法selectIgnoreLogicDeleteByMap

    1. @Override
    2. public List<User> getIgnoreDeleteById(Long userId) {
    3. Map<String, Object> columnMap = new HashMap<>(2);
    4. columnMap.put("id", userId);
    5. return this.baseMapper.selectIgnoreLogicDeleteByMap(columnMap);
    6. }

    以上就是Mybatisplus怎么自定义SQL注入器查询@TableLogic逻辑删除后的数据的详细内容,更多关于Mybatisplus怎么自定义SQL注入器查询@TableLogic逻辑删除后的数据的资料请关注九品源码其它相关文章!