这篇文章主要介绍了mybatisplus怎么自定义sql注入器查询@TableLogic逻辑删除后的数据的相关知识,内容详细易懂,操作简单快捷,具有一定借鉴价值,相信大家阅读完这篇Mybatisplus怎么自定义SQL注入器查询@Table
这篇文章主要介绍了mybatisplus怎么自定义sql注入器查询@TableLogic逻辑删除后的数据的相关知识,内容详细易懂,操作简单快捷,具有一定借鉴价值,相信大家阅读完这篇Mybatisplus怎么自定义SQL注入器查询@TableLogic逻辑删除后的数据文章都会有所收获,下面我们一起来看看吧。
Mybatis-plus使用@TableLogic注解进行逻辑删除数据后,在某些场景下,又需要查询该数据时,又不想写SQL。
自定义Mybatis-plus的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); } 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; }}
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); } 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; }
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; }}
public interface CustomBaseMapper<T> extends BaseMapper<T> { List<T> selectIgnoreLogicDeleteByMap(@Param("cm") Map<String, Object> columnMap); List<T> selectIgnoreLogicDelete(@Param("ew") Wrapper<T> queryWrapper);}
@Mapperpublic interface UserMapper extends CustomBaseMapper<User> {}
@Override public List<User> getIgnoreDeleteById(Long userId) { LambdaQueryWrapper<User> queryWrapper = new LambdaQueryWrapper<>(); queryWrapper.eq(User::getId,userId); return this.baseMapper.selectIgnoreLogicDelete(queryWrapper); }
@Override public List<User> getIgnoreDeleteById(Long userId) {Map<String, Object> columnMap = new HashMap<>(2); columnMap.put("id", userId); return this.baseMapper.selectIgnoreLogicDeleteByMap(columnMap); }
关于“Mybatisplus怎么自定义SQL注入器查询@TableLogic逻辑删除后的数据”这篇文章的内容就介绍到这里,感谢各位的阅读!相信大家对“Mybatisplus怎么自定义SQL注入器查询@TableLogic逻辑删除后的数据”知识都有一定的了解,大家如果还想学习更多知识,欢迎关注编程网精选频道。
--结束END--
本文标题: Mybatisplus怎么自定义SQL注入器查询@TableLogic逻辑删除后的数据
本文链接: https://www.lsjlt.com/news/351186.html(转载时请注明来源链接)
有问题或投稿请发送至: 邮箱/279061341@qq.com QQ/279061341
下载Word文档到电脑,方便收藏和打印~
2024-05-03
2024-05-03
2024-05-03
2024-05-03
2024-05-03
2024-05-03
2024-05-03
2024-05-03
2024-05-03
2024-05-03
回答
回答
回答
回答
回答
回答
回答
回答
回答
回答
0