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

简介: Mybatis-plus使用@TableLogic注解进行逻辑删除数据后,在某些场景下,又需要查询该数据时,又不想写SQL。自定义Mybatis-plus的SQL注入器一劳永逸的解决该问题

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

publicclassSelectIgnoreLogicDeleteByMapextendsAbstractMethod {
@OverridepublicMappedStatementinjectMappedStatement(Class<?>mapperClass, Class<?>modelClass, TableInfotableInfo) {
StringsqlBase="<script>SELECT %s FROM %s %s\n</script>";
StringsqlScript=this.sqlWhereByMap(tableInfo);
Stringsql=String.format(sqlBase, this.sqlSelectColumns(tableInfo, false), tableInfo.getTableName(), sqlScript);
SqlSourcesqlSource=this.languageDriver.createSqlSource(this.configuration, sql, Map.class);
returnthis.addSelectMappedStatementForTable(mapperClass, "selectIgnoreLogicDeleteByMap", sqlSource, tableInfo);
    }
/*** 拼接where条件根据map参数** [url=home.php?mod=space&uid=952169]@Param[/url] table 表* [url=home.php?mod=space&uid=155549]@Return[/url] sql*/protectedStringsqlWhereByMap(TableInfotable) {
StringsqlScript;
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);
returnsqlScript;
    }
}

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

publicclassSelectIgnoreLogicDeleteextendsAbstractMethod {
@OverridepublicMappedStatementinjectMappedStatement(Class<?>mapperClass, Class<?>modelClass, TableInfotableInfo) {
StringsqlBase="<script>%s SELECT %s FROM %s %s %s %s\n</script>";
Stringsql=String.format(sqlBase, this.sqlFirst(), this.sqlSelectColumns(tableInfo, true), tableInfo.getTableName(), this.sqlWhereEntityWrapper(true, tableInfo), this.sqlOrderBy(tableInfo), this.sqlComment());
SqlSourcesqlSource=this.languageDriver.createSqlSource(this.configuration, sql, modelClass);
returnthis.addSelectMappedStatementForTable(mapperClass, "selectIgnoreLogicDelete", sqlSource, tableInfo);
    }
/*** 拼接where条件** @param newLine 新行* @param table   表* @return sql*/protectedStringsqlWhereEntityWrapper(booleannewLine, TableInfotable) {
StringsqlScript=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);
returnnewLine?"\n"+sqlScript : sqlScript;
    }

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

publicclassCustomSqlInjectorextendsDefaultSqlInjector {
@OverridepublicList<AbstractMethod>getMethodList(Class<?>mapperClass, TableInfotableInfo) {
List<AbstractMethod>methodList=super.getMethodList(mapperClass, tableInfo);
methodList.add(newSelectIgnoreLogicDeleteByMap());
methodList.add(newSelectIgnoreLogicDelete());
returnmethodList;
    }
}

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

publicinterfaceCustomBaseMapper<T>extendsBaseMapper<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);
}

2.使用声明的方法

业务mapper继承自定义的CustomBaseMapper

@MapperpublicinterfaceUserMapperextendsCustomBaseMapper<User> {
}

调用方法selectIgnoreLogicDelete

@OverridepublicList<User>getIgnoreDeleteById(LonguserId) {
LambdaQueryWrapper<User>queryWrapper=newLambdaQueryWrapper<>();
queryWrapper.eq(User::getId,userId);
returnthis.baseMapper.selectIgnoreLogicDelete(queryWrapper);
}

调用方法selectIgnoreLogicDeleteByMap

@OverridepublicList<User>getIgnoreDeleteById(LonguserId) {
Map<String, Object>columnMap=newHashMap<>(2);
columnMap.put("id", userId);
returnthis.baseMapper.selectIgnoreLogicDeleteByMap(columnMap);
}
目录
相关文章
|
Java 数据库连接 数据库
mybatis查询数据,返回的对象少了一个字段
mybatis查询数据,返回的对象少了一个字段
1514 9
|
SQL XML Java
MyBatis Mapper中使用limit参数的查询问题
总结而言,MyBatis中使用 `limit`参数的查询可以高度定制并且灵活,基于方法签名和XML映射文件的组合来达成多样化的查询需求。通过参数化查询和动态SQL,MyBatis可以有效地处理各种复杂情境下的数据库操作,并且将SQL语句的维护与业务代码的编写相分离,提升代码的可维护性和可阅读性。
931 13
|
SQL Java 数据库
解决Java Spring Boot应用中MyBatis-Plus查询问题的策略。
保持技能更新是侦探的重要素质。定期回顾最佳实践和新技术。比如,定期查看MyBatis-Plus的更新和社区的最佳做法,这样才能不断提升查询效率和性能。
864 1
|
XML Java 数据库连接
Mybatis一对一,一对多关联查询
## MyBatis一对一、一对多关联查询详解 MyBatis是一款优秀的持久层框架,提供了灵活的SQL映射功能,支持复杂的数据库操作。本文将详细介绍MyBatis中一对一和一对多关联查询的实现。 ### 一对一关联查询 一对一关联关系指的是一个表中的一条记录与另一个表中的一条记录相关联。例如,一个用户有一个地址信息。 #### 数据库表设计 假设有两个表:`user`和 `address`。 ``` CREATE TABLE user ( id INT PRIMARY KEY, name VARCHAR(50) ); CREATE TABLE address
637 18
|
SQL Java 数据库连接
【潜意识Java】MyBatis中的动态SQL灵活、高效的数据库查询以及深度总结
本文详细介绍了MyBatis中的动态SQL功能,涵盖其背景、应用场景及实现方式。
2115 6
|
SQL 数据库 开发者
ClkLog埋点分析系统支持自定义SQL 查询
本期主要为大家介绍ClkLog九月上线的新功能-自定义SQL查询。
ClkLog埋点分析系统支持自定义SQL 查询
|
SQL 安全 Java
MyBatis-Plus条件构造器:构建安全、高效的数据库查询
MyBatis-Plus 提供了一套强大的条件构造器(Wrapper),用于构建复杂的数据库查询条件。Wrapper 类允许开发者以链式调用的方式构造查询条件,无需编写繁琐的 SQL 语句,从而提高开发效率并减少 SQL 注入的风险。
856 2
MyBatis-Plus条件构造器:构建安全、高效的数据库查询
|
SQL 缓存 Java
【详细实用のMyBatis教程】获取参数值和结果的各种情况、自定义映射、动态SQL、多级缓存、逆向工程、分页插件
本文详细介绍了MyBatis的各种常见用法MyBatis多级缓存、逆向工程、分页插件 包括获取参数值和结果的各种情况、自定义映射resultMap、动态SQL
【详细实用のMyBatis教程】获取参数值和结果的各种情况、自定义映射、动态SQL、多级缓存、逆向工程、分页插件
|
XML Java 数据库连接
Mybatis实现RBAC权限模型查询
通过对RBAC权限模型的理解和MyBatis的灵活使用,我们可以高效地实现复杂的权限管理功能,为应用程序的安全性和可维护性提供有力支持。
581 5