溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊(cè)×
其他方式登錄
點(diǎn)擊 登錄注冊(cè) 即表示同意《億速云用戶服務(wù)條款》

Mybatisplus怎么自定義SQL注入器查詢@TableLogic邏輯刪除后的數(shù)據(jù)

發(fā)布時(shí)間:2023-03-09 11:29:35 來(lái)源:億速云 閱讀:198 作者:iii 欄目:開發(fā)技術(shù)

這篇文章主要介紹了Mybatisplus怎么自定義SQL注入器查詢@TableLogic邏輯刪除后的數(shù)據(jù)的相關(guān)知識(shí),內(nèi)容詳細(xì)易懂,操作簡(jiǎn)單快捷,具有一定借鑒價(jià)值,相信大家閱讀完這篇Mybatisplus怎么自定義SQL注入器查詢@TableLogic邏輯刪除后的數(shù)據(jù)文章都會(huì)有所收獲,下面我們一起來(lái)看看吧。

    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);
        }

    關(guān)于“Mybatisplus怎么自定義SQL注入器查詢@TableLogic邏輯刪除后的數(shù)據(jù)”這篇文章的內(nèi)容就介紹到這里,感謝各位的閱讀!相信大家對(duì)“Mybatisplus怎么自定義SQL注入器查詢@TableLogic邏輯刪除后的數(shù)據(jù)”知識(shí)都有一定的了解,大家如果還想學(xué)習(xí)更多知識(shí),歡迎關(guān)注億速云行業(yè)資訊頻道。

    向AI問(wèn)一下細(xì)節(jié)

    免責(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)容。

    AI