Mybatis-plus自定义SQL注入器查询@TableLogic逻辑删除后的数据详解
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);
}总结
到此这篇关于Mybatis-plus自定义SQL注入器查询@TableLogic逻辑删除后的数据的文章就介绍到这了,更多相关Mybatis-plus查询@TableLogic逻辑删除后数据内容请搜索脚本之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持脚本之家!
相关文章
基于SpringBoot与Mybatis实现SpringMVC Web项目
这篇文章主要介绍了基于SpringBoot与Mybatis实现SpringMVC Web项目的相关资料,需要的朋友可以参考下2017-04-04
Spring Security密码解析器PasswordEncoder自定义登录逻辑
这篇文章主要为大家介绍了Spring Security密码解析器PasswordEncoder自定义登录逻辑示例详解,有需要的朋友可以借鉴参考下,希望能够有所帮助,祝大家多多进步,早日升职加薪2022-08-08
java substring(a)与substring(a,b)的使用说明
这篇文章主要介绍了java substring(a)与substring(a,b)的使用说明,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧2020-10-10
feign开启日志Logger.Level feignLoggerLevel()中Level爆红的解决
这篇文章主要介绍了feign开启日志Logger.Level feignLoggerLevel()中Level爆红的解决方案,具有很好的参考价值,希望对大家有所帮助。如有错误或未考虑完全的地方,望不吝赐教2022-06-06
Spring Boot 中使用 Mybatis Plus的操作方法
本文介绍了如何在 Spring Boot 项目中集成 Mybatis Plus,Spring Boot 与 MyBatis Plus 的集成非常简单,通过自动配置和简洁的 API,可以大大减少开发中常见的数据库操作代码,需要的朋友参考下吧2024-12-12


最新评论