Mybatis-plus自定義SQL注入器查詢@TableLogic邏輯刪除后的數(shù)據(jù)詳解
1 需求
Mybatis-plus使用@TableLogic注解進(jìn)行邏輯刪除數(shù)據(jù)后,在某些場(chǎng)景下,又需要查詢?cè)摂?shù)據(jù)時(shí),又不想寫SQL。
2 解決方案
自定義Mybatis-plus的SQL注入器一勞永逸的解決該問(wèn)題
3 方案:
3.1 方案1,繼承 AbstractMethod拼接SQL語(yǔ)句
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條件根據(jù)map參數(shù) * * @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語(yǔ)句
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注入器,注冊(cè)上述自定義的方法
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. 自定義基礎(chǔ)mapper,聲明注冊(cè)的方法
public interface CustomBaseMapper<T> extends BaseMapper<T> { /** * 根據(jù)map條件查詢數(shù)據(jù),并忽略邏輯刪除 * * @param columnMap 查詢條件 * @return 結(jié)果信息 */ List<T> selectIgnoreLogicDeleteByMap(@Param("cm") Map<String, Object> columnMap); /** * 根據(jù)條件查詢數(shù)據(jù),并忽略邏輯刪除 * * @param queryWrapper 查詢條件 * @return 結(jié)果信息 */ List<T> selectIgnoreLogicDelete(@Param("ew") Wrapper<T> queryWrapper); }
6. 使用聲明的方法
6.1 業(yè)務(wù)mapper繼承自定義的CustomBaseMapper
@Mapper public interface UserMapper extends CustomBaseMapper<User> { }
6.2 調(diào)用方法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 調(diào)用方法selectIgnoreLogicDeleteByMap
@Override public List<User> getIgnoreDeleteById(Long userId) { Map<String, Object> columnMap = new HashMap<>(2); columnMap.put("id", userId); return this.baseMapper.selectIgnoreLogicDeleteByMap(columnMap); }
總結(jié)
到此這篇關(guān)于Mybatis-plus自定義SQL注入器查詢@TableLogic邏輯刪除后的數(shù)據(jù)的文章就介紹到這了,更多相關(guān)Mybatis-plus查詢@TableLogic邏輯刪除后數(shù)據(jù)內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
springboot jpa 延遲加載問(wèn)題的2種解決
這篇文章主要介紹了springboot jpa 延遲加載問(wèn)題的2種解決方案,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2021-07-07基于Spring boot @Value 注解注入屬性值的操作方法
這篇文章主要介紹了結(jié)合SpEL使用@Value-基于配置文件或非配置的文件的值注入-Spring Boot的相關(guān)知識(shí),本文通過(guò)實(shí)例代碼給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2020-07-07Spring MVC InitBinder驗(yàn)證方法
這篇文章主要介紹了Spring MVC InitBinder驗(yàn)證方法,小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2018-03-03通過(guò)Java實(shí)現(xiàn)中文分詞與文本關(guān)鍵詞提取
這篇文章主要為大家詳細(xì)介紹了如何利用Java實(shí)現(xiàn)中文分詞以及文本關(guān)鍵詞提取功能,文中的示例代碼講解詳細(xì),感興趣的小伙伴可以跟隨小編一起學(xué)習(xí)學(xué)習(xí)2023-06-06解決Mybatis中foreach嵌套使用if標(biāo)簽對(duì)象取值的問(wèn)題
這篇文章主要介紹了解決Mybatis中foreach嵌套使用if標(biāo)簽對(duì)象取值的問(wèn)題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-02-02Spring單元測(cè)試類ApplicationTests錯(cuò)誤的解決
這篇文章主要介紹了Spring單元測(cè)試類ApplicationTests錯(cuò)誤的解決,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-01-01