Mybatis-plus自定义SQL注入器查询@TableLogic逻辑删除后的数据详解

作者:Hi,all 时间:2023-04-09 22:36:45 

1  需求

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

2  解决方案

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

3  方案:

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

public class SelectIgnoreLogicDeleteByMap extends AbstractMethod {

@Override
   public MappedStatement injectMappedStatement(Class<?> mapperClass, Class<?> modelClass, TableInfo tableInfo) {

String sqlBase= "<script>SELECT %s FROM %s %s\n</script>";
       String sqlScript = this.sqlWhereByMap(tableInfo);
       String sql = String.format(sqlBase, this.sqlSelectColumns(tableInfo, false), tableInfo.getTableName(), sqlScript);
       SqlSource sqlSource = this.languageDriver.createSqlSource(this.configuration, sql, Map.class);
       return this.addSelectMappedStatementForTable(mapperClass, "selectIgnoreLogicDeleteByMap", sqlSource, tableInfo);
   }

/**
    * 拼接where条件根据map参数
    *
    * @param table 表
    * @return sql
    */
   protected String sqlWhereByMap(TableInfo table) {
       String sqlScript;
       sqlScript = SqlScriptUtils.convertChoose("v == null", " ${k} IS NULL ", " ${k} = #{v} ");
       sqlScript = SqlScriptUtils.convertForeach(sqlScript, "cm", "k", "v", "AND");
       sqlScript = SqlScriptUtils.convertWhere(sqlScript);
       sqlScript = SqlScriptUtils.convertIf(sqlScript, String.format("%s != null and !%s", "cm", "cm.isEmpty"), true);
       return sqlScript;
   }
}

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

public class SelectIgnoreLogicDelete extends AbstractMethod {

@Override
   public MappedStatement injectMappedStatement(Class<?> mapperClass, Class<?> modelClass, TableInfo tableInfo) {

String sqlBase = "<script>%s SELECT %s FROM %s %s %s %s\n</script>";

String sql = String.format(sqlBase, this.sqlFirst(), this.sqlSelectColumns(tableInfo, true), tableInfo.getTableName(), this.sqlWhereEntityWrapper(true, tableInfo), this.sqlOrderBy(tableInfo), this.sqlComment());
       SqlSource sqlSource = this.languageDriver.createSqlSource(this.configuration, sql, modelClass);
       return this.addSelectMappedStatementForTable(mapperClass, "selectIgnoreLogicDelete", sqlSource, tableInfo);
   }

/**
    * 拼接where条件
    *
    * @param newLine 新行
    * @param table   表
    * @return sql
    */
   protected String sqlWhereEntityWrapper(boolean newLine, TableInfo table) {
       String sqlScript = table.getAllSqlWhere(false, true, "ew.entity.");
       sqlScript = SqlScriptUtils.convertIf(sqlScript, String.format("%s != null", "ew.entity"), true);
       sqlScript = sqlScript + "\n";
       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);
       sqlScript = SqlScriptUtils.convertWhere(sqlScript) + "\n";
       sqlScript = sqlScript + SqlScriptUtils.convertIf(String.format(" ${%s}", "ew.sqlSegment"), String.format("%s != null and %s != '' and %s", "ew.sqlSegment", "ew.sqlSegment", "ew.emptyOfWhere"), true);
       sqlScript = SqlScriptUtils.convertIf(sqlScript, String.format("%s != null", "ew"), true);
       return newLine ? "\n" + sqlScript : sqlScript;
   }

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

public class CustomSqlInjector extends DefaultSqlInjector {

@Override
   public List<AbstractMethod> getMethodList(Class<?> mapperClass, TableInfo tableInfo) {
       List<AbstractMethod> methodList = super.getMethodList(mapperClass, tableInfo);
       methodList.add(new SelectIgnoreLogicDeleteByMap());
       methodList.add(new SelectIgnoreLogicDelete());
       return methodList;
   }
}

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

public interface CustomBaseMapper<T> extends BaseMapper<T> {

/**
    * 根据map条件查询数据,并忽略逻辑删除
    *
    * @param columnMap 查询条件
    * @return 结果信息
    */
   List<T> selectIgnoreLogicDeleteByMap(@Param("cm") Map<String, Object> columnMap);

/**
    * 根据条件查询数据,并忽略逻辑删除
    *
    * @param queryWrapper 查询条件
    * @return 结果信息
    */
   List<T> selectIgnoreLogicDelete(@Param("ew") Wrapper<T> queryWrapper);
}

6. 使用声明的方法

6.1  业务mapper继承自定义的CustomBaseMapper

@Mapper
public interface UserMapper extends CustomBaseMapper<User> {
}

6.2 调用方法selectIgnoreLogicDelete

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

6.3 调用方法selectIgnoreLogicDeleteByMap

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

来源:https://blog.csdn.net/jinchunzhao123/article/details/127988188

标签:mybatis-plus,@tableLogic,逻辑删除
0
投稿

猜你喜欢

  • java异常处理执行顺序详解try catch finally

    2022-10-01 04:10:10
  • Maven中的SnapShot版本和正式版本的区别

    2023-11-03 16:31:46
  • java中List分页的几种方法介绍

    2022-03-01 12:04:28
  • C#的FileInfo类实现文件操作实例

    2021-06-30 06:39:44
  • IntelliJ IDEA2020.1版本更新pom文件自动导包的方法

    2023-01-13 17:44:01
  • Android 中倒计时验证两种常用方式实例详解

    2022-08-29 04:44:41
  • .Net WInform开发笔记(二)Winform程序运行结构图及TCP协议在Winform中的应用

    2021-08-08 23:56:44
  • 小白2分钟学会Visual Studio如何将引用包打包到NuGet上

    2022-01-14 10:25:53
  • java如何消除太多的if else判断示例代码

    2023-01-17 21:57:40
  • 基于Java数组实现循环队列的两种方法小结

    2023-06-30 16:09:01
  • C#处理Paint事件的方法

    2022-01-12 02:02:51
  • opencv实现读取视频保存视频

    2021-08-16 15:57:50
  • Java取整与四舍五入

    2021-07-25 01:07:32
  • 如何用java实现分页查询

    2023-09-23 06:50:24
  • 利用C#编写一个Windows服务程序的方法详解

    2022-08-09 20:24:03
  • java selenium 操作浏览器实例

    2023-04-13 17:57:13
  • WPF如何绘制光滑连续贝塞尔曲线示例代码

    2022-06-07 18:31:58
  • C#事件(event)使用方法详解

    2023-12-24 14:35:05
  • Java日期时间类(Date、DateFormat、Calendar)解析

    2022-08-06 18:02:14
  • MyBatis关闭一级缓存的两种方式(分注解和xml两种方式)

    2023-09-02 10:18:59
  • asp之家 软件编程 m.aspxhome.com