Mybatis-plus自定義SQL注入器查詢@TableLogic邏輯刪除后的數(shù)據(jù)詳解
更新時間:2023年03月09日 10:54:38 作者:Hi,all
這篇文章主要給大家介紹了關于Mybatis-plus自定義SQL注入器查詢@TableLogic邏輯刪除后的數(shù)據(jù)的相關資料,文中通過實例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友可以參考下
1 需求
Mybatis-plus使用@TableLogic注解進行邏輯刪除數(shù)據(jù)后,在某些場景下,又需要查詢該數(shù)據(jù)時,又不想寫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條件根據(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語句
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> { /** * 根據(jù)map條件查詢數(shù)據(jù),并忽略邏輯刪除 * * @param columnMap 查詢條件 * @return 結果信息 */ List<T> selectIgnoreLogicDeleteByMap(@Param("cm") Map<String, Object> columnMap); /** * 根據(jù)條件查詢數(shù)據(jù),并忽略邏輯刪除 * * @param queryWrapper 查詢條件 * @return 結果信息 */ List<T> selectIgnoreLogicDelete(@Param("ew") Wrapper<T> queryWrapper); }
6. 使用聲明的方法
6.1 業(yè)務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); }
總結
到此這篇關于Mybatis-plus自定義SQL注入器查詢@TableLogic邏輯刪除后的數(shù)據(jù)的文章就介紹到這了,更多相關Mybatis-plus查詢@TableLogic邏輯刪除后數(shù)據(jù)內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關文章希望大家以后多多支持腳本之家!
相關文章
基于Spring boot @Value 注解注入屬性值的操作方法
這篇文章主要介紹了結合SpEL使用@Value-基于配置文件或非配置的文件的值注入-Spring Boot的相關知識,本文通過實例代碼給大家介紹的非常詳細,對大家的學習或工作具有一定的參考借鑒價值,需要的朋友可以參考下2020-07-07解決Mybatis中foreach嵌套使用if標簽對象取值的問題
這篇文章主要介紹了解決Mybatis中foreach嵌套使用if標簽對象取值的問題,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2022-02-02Spring單元測試類ApplicationTests錯誤的解決
這篇文章主要介紹了Spring單元測試類ApplicationTests錯誤的解決,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2022-01-01