您好,登錄后才能下訂單哦!
這篇文章主要介紹了Mybatisplus怎么自定義SQL注入器查詢@TableLogic邏輯刪除后的數(shù)據(jù)的相關(guān)知識(shí),內(nèi)容詳細(xì)易懂,操作簡(jiǎn)單快捷,具有一定借鑒價(jià)值,相信大家閱讀完這篇Mybatisplus怎么自定義SQL注入器查詢@TableLogic邏輯刪除后的數(shù)據(jù)文章都會(huì)有所收獲,下面我們一起來(lái)看看吧。
Mybatis-plus使用@TableLogic注解進(jìn)行邏輯刪除數(shù)據(jù)后,在某些場(chǎng)景下,又需要查詢?cè)摂?shù)據(jù)時(shí),又不想寫SQL。
自定義Mybatis-plus的SQL注入器一勞永逸的解決該問(wèn)題
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; } }
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; }
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> { /** * 根據(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); }
@Mapper public 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); }
關(guān)于“Mybatisplus怎么自定義SQL注入器查詢@TableLogic邏輯刪除后的數(shù)據(jù)”這篇文章的內(nèi)容就介紹到這里,感謝各位的閱讀!相信大家對(duì)“Mybatisplus怎么自定義SQL注入器查詢@TableLogic邏輯刪除后的數(shù)據(jù)”知識(shí)都有一定的了解,大家如果還想學(xué)習(xí)更多知識(shí),歡迎關(guān)注億速云行業(yè)資訊頻道。
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。