国产 无码 综合区,色欲AV无码国产永久播放,无码天堂亚洲国产AV,国产日韩欧美女同一区二区

自定義Mybatis-plus插件(限制最大查詢數(shù)量)

這篇具有很好參考價值的文章主要介紹了自定義Mybatis-plus插件(限制最大查詢數(shù)量)。希望對大家有所幫助。如果存在錯誤或未考慮完全的地方,請大家不吝賜教,您也可以點擊"舉報違法"按鈕提交疑問。

自定義Mybatis-plus插件(限制最大查詢數(shù)量)

需求背景

? 一次查詢?nèi)绻Y(jié)果返回太多(1萬或更多),往往會導(dǎo)致系統(tǒng)性能下降,有時更會內(nèi)存不足,影響系統(tǒng)穩(wěn)定性,故需要做限制。

解決思路

1.經(jīng)分析最后決定,應(yīng)限制一次查詢返回的最大結(jié)果數(shù)量不應(yīng)該超出1萬,對于一次返回結(jié)果大于限制的時候應(yīng)該拋出異常,而不應(yīng)該截取(limit 10000)最大結(jié)果(結(jié)果需求不匹配)。

2.利用mybatis攔截器技術(shù),統(tǒng)一攔截sql,并真對大結(jié)果的查詢先做一次count查詢。

步驟一

1.1 定義攔截器PreCheckBigQueryInnerInterceptor

public class PreCheckBigQueryInnerInterceptor implements InnerInterceptor {}
1.2 重寫willDoQuery方法
 public boolean willDoQuery(Executor executor, MappedStatement ms, Object parameter, RowBounds rowBounds, ResultHandler resultHandler, BoundSql boundSql) throws SQLException {
        // 解析sql
        Statement stmt = CCJSqlParserUtil.parse(boundSql.getSql());
        if (stmt instanceof Select) {
            PlainSelect selectStmt = (PlainSelect) ((Select) stmt).getSelectBody();
            if (Objects.nonNull(selectStmt.getLimit())) {
                //包含limit查詢
                return true;
            }
            for (SelectItem selectItem : selectStmt.getSelectItems()) {
                //計數(shù)查詢 count();
                SelectExpressionItem selectExpressionItem = (SelectExpressionItem) selectItem;
                if (selectExpressionItem.getExpression() instanceof Function) {
                    //包含function查詢
                    return true;
                }
            }
            Long aLong = doQueryCount(executor, ms, parameter, rowBounds, resultHandler, boundSql);
            if (aLong == 0L) {
                return false;
            }
            if (aLong > 20) {
                throw new RuntimeException("單個查詢結(jié)果大于20條!!!");
            }
        }
        return true;
    }
1.3 代碼解析
1.3.1 利用CCJSqlParserUtil解析sql,并判斷sql類型,只對Select的SQL攔擊.
1.3.2 對于已有l(wèi)imit的sql查詢,直接放行.
1.3.3 對于包含function查詢(例如count(1)計算,max()...),直接放行.
1.3.4 否則判斷為大結(jié)果查詢,執(zhí)行(doQueryCount)與查詢數(shù)量.
1.3.5 對于大于指定數(shù)量的結(jié)果,拋出異常.
1.4 定義doQueryCount方法
private Long doQueryCount(Executor executor, MappedStatement ms, Object parameter, RowBounds rowBounds, ResultHandler resultHandler, BoundSql boundSql) throws SQLException {
        MappedStatement countMs = buildAutoCountMappedStatement(ms);
        String countSqlStr = autoCountSql(true, boundSql.getSql());
        PluginUtils.MPBoundSql mpBoundSql = PluginUtils.mpBoundSql(boundSql);
        BoundSql countSql = new BoundSql(countMs.getConfiguration(), countSqlStr, mpBoundSql.parameterMappings(), parameter);
        PluginUtils.setAdditionalParameter(countSql, mpBoundSql.additionalParameters());
        CacheKey cacheKey = executor.createCacheKey(countMs, parameter, rowBounds, countSql);
        Object result = executor.query(countMs, parameter, rowBounds, resultHandler, cacheKey, countSql).get(0);
        System.out.println(result);
        return (result == null ? 0L : Long.parseLong(result.toString()));
    }
代碼解讀:參考PaginationInnerInterceptor(mybatis-plus)分頁插件
1.4.1:構(gòu)造MappedStatement對象buildAutoCountMappedStatement(ms),MappedStatement相當(dāng)于一個存儲 SQL 語句、輸入?yún)?shù)和輸出結(jié)果映射等信息的封裝體,它對應(yīng)一條 SQL 語句,并包含了該 SQL 語句執(zhí)行所需的所有信息。如下代碼
<mapper namespace="com.example.UserMapper">
   <select id="selectAllUsers" resultType="com.example.User">
       SELECT * FROM user
   </select>
</mapper>

注意:必須重新構(gòu)造,不能直接使用入?yún)⒅械膍s

1.4.2:autoCountSql(true, boundSql.getSql()) 定義并優(yōu)化計數(shù)查詢語句
String.format("SELECT COUNT(1) FROM (%s) TOTAL", originalSql);
1.4.3: 執(zhí)行查詢executor.query

步驟二

1.1 注冊攔截器PreCheckBigQueryInnerInterceptor

@Bean
public MybatisPlusInterceptor mybatisPlusInterceptor() {
    MybatisPlusInterceptor interceptor = new MybatisPlusInterceptor();
    interceptor.addInnerInterceptor(new PaginationInnerInterceptor(DbType.MYSQL));//分頁插件(Mybatis-plus)
    interceptor.addInnerInterceptor(new BlockAttackInnerInterceptor());//防止全表更新(Mybatis-plus)
    interceptor.addInnerInterceptor(new PreCheckBigQueryInnerInterceptor());//防止全表查詢(自定義插件)
    return interceptor;
}

知識小結(jié):

  1. MybatisPlusInterceptor
public class MybatisPlusInterceptor implements Interceptor {
    @Setter
    private List<InnerInterceptor> interceptors = new ArrayList<>();
}

? 他是基于mybatis的Interceptor接口做的攔截器,上文中我們 注冊攔截器PreCheckBigQueryInnerInterceptor的攔截器其實添加到MybatisPlusInterceptor.interceptors集合中。

  1. 為啥重寫willDoQuery見代碼而不是beforeQuery
 public Object intercept(Invocation invocation) throws Throwable {
       ......
                for (InnerInterceptor query : interceptors) {
                    if (!query.willDoQuery(executor, ms, parameter, rowBounds, resultHandler, boundSql)) {
                        return Collections.emptyList();
                    }
                    query.beforeQuery(executor, ms, parameter, rowBounds, resultHandler, boundSql);
                }
     ......
        return invocation.proceed();
 }

2.1 willDoQuery先于beforeQuery方法,且一定會執(zhí)行文章來源地址http://www.zghlxwxcb.cn/news/detail-418378.html

到了這里,關(guān)于自定義Mybatis-plus插件(限制最大查詢數(shù)量)的文章就介紹完了。如果您還想了解更多內(nèi)容,請在右上角搜索TOY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!

本文來自互聯(lián)網(wǎng)用戶投稿,該文觀點僅代表作者本人,不代表本站立場。本站僅提供信息存儲空間服務(wù),不擁有所有權(quán),不承擔(dān)相關(guān)法律責(zé)任。如若轉(zhuǎn)載,請注明出處: 如若內(nèi)容造成侵權(quán)/違法違規(guī)/事實不符,請點擊違法舉報進行投訴反饋,一經(jīng)查實,立即刪除!

領(lǐng)支付寶紅包贊助服務(wù)器費用

相關(guān)文章

  • mybatis-plus之排序查詢

    mybatis-plus之排序查詢

    一、orderByAsc 排序:ORDER BY 字段, ... ASC 例:? orderByAsc(\\\"id\\\", \\\"name\\\") --- order by id ASC,name ASC #二、orderByDesc 排序:ORDER BY 字段, ... DESC 例:? orderByDesc(\\\"id\\\", \\\"name\\\") --- order by id DESC,name DESC #三、orderBy 排序:ORDER BY 字段, ... 例:? orderBy(true, true, \\\"id\\\", \\\"name\\\") --- order by id ASC,name ASC ?倒敘排序 ?

    2024年02月15日
    瀏覽(28)
  • MyBatis-Plus條件查詢問題解決

    MyBatis-Plus條件查詢問題解決

    問題描述 系統(tǒng)中有用戶注冊的功能,但是會出現(xiàn)重復(fù)注冊的現(xiàn)象,代碼中有做過重復(fù)校驗,但是沒有生效。 問題解決 首先排查數(shù)據(jù)生成時間點不同,相差時間有長有短,不是用戶同時多次點擊的原因,應(yīng)該是用戶這邊不同時間重復(fù)多次注冊導(dǎo)致的,但是程序中防止重復(fù)校驗

    2024年02月16日
    瀏覽(26)
  • spring boot集成mybatis-plus——Mybatis Plus 查詢數(shù)據(jù)(圖文講解)

    spring boot集成mybatis-plus——Mybatis Plus 查詢數(shù)據(jù)(圖文講解)

    ?更新時間 2023-01-03 16:07:12 大家好,我是小哈。 本小節(jié)中,我們將學(xué)習(xí)如何通過 Mybatis Plus 查詢數(shù)據(jù)庫表中的數(shù)據(jù)。 在前面小節(jié)中,我們已經(jīng)定義好了一個用于測試的用戶表, 執(zhí)行腳本如下: 定義一個名為? User ?實體類: 不明白 Mybatis Plus 實體類注解的小伙伴,可參考前面

    2024年02月02日
    瀏覽(23)
  • mybatis-plus實現(xiàn)分頁查詢

    mybatis-plus實現(xiàn)分頁查詢

    分頁查詢使用的方法是: IPage:用來構(gòu)建分頁查詢條件 Wrapper:用來構(gòu)建條件查詢的條件,目前我們沒有可直接傳為Null IPage:返回值,你會發(fā)現(xiàn)構(gòu)建分頁條件和方法的返回值都是IPage IPage是一個接口,我們需要找到它的實現(xiàn)類來構(gòu)建它,具體的實現(xiàn)類,可以進入到IPage類中按ctrl+

    2023年04月08日
    瀏覽(23)
  • MyBatis-Plus(三.Wrapper條件查詢)

    MyBatis-Plus(三.Wrapper條件查詢)

    Wrapper是Mybatis-plus中特有的 條件封裝接口 也就是把 查詢的條件 封裝到Wrapper實現(xiàn)類中 它的各個實現(xiàn)類有什么作用呢, 我覺得直接 顧名思義 吧? QueryWrapper(刪,查) 因為參數(shù)一般都從前端傳來的數(shù)據(jù)中得到, 所以必須用條件封裝的第一個參數(shù)確認它不為null UpdateWrapper(改) LambdaQuer

    2024年02月04日
    瀏覽(23)
  • SpringBoot(整合MyBatis + MyBatis-Plus + MyBatisX插件使用)

    SpringBoot(整合MyBatis + MyBatis-Plus + MyBatisX插件使用)

    1.需求分析 2.數(shù)據(jù)庫表設(shè)計 3.數(shù)據(jù)庫環(huán)境配置 1.新建maven項目 2.pom.xml 引入依賴 3.application.yml 配置數(shù)據(jù)源 數(shù)據(jù)庫名 用戶名 密碼 驅(qū)動是mysql8的(因為上面使用了版本仲裁) 4.Application.java 編寫啟動類 5.測試 6.配置類切換druid數(shù)據(jù)源 7.測試數(shù)據(jù)源是否成功切換 4.Mybatis基礎(chǔ)配置 1

    2024年03月20日
    瀏覽(32)
  • MyBatis-Plus 實戰(zhàn)教程四 idea插件

    MyBatis-Plus 實戰(zhàn)教程四 idea插件

    MybatisPlus提供了很多的插件功能,進一步拓展其功能。目前已有的插件有: PaginationInnerInterceptor:自動分頁 TenantLineInnerInterceptor:多租戶 DynamicTableNameInnerInterceptor:動態(tài)表名 OptimisticLockerInnerInterceptor:樂觀鎖 IllegalSQLInnerInterceptor:sql 性能規(guī)范 BlockAttackInnerInterceptor:防止全表更

    2024年02月06日
    瀏覽(15)
  • springboot使用Mybatis-plus分頁插件

    在? pom.xml ? 文件中添加 MyBatis Plus 和分頁插件的依賴: 注意替換? {mybatis-plus-version} ?為對應(yīng)的版本號。 在 Spring Boot 的配置文件? application.yml ? 中添加分頁插件的配置參數(shù): 注意代碼中的注釋,其中 ? PaginationInterceptor ?表示使用 MyBatis Plus 提供的分頁插件。 在接口層使用

    2024年02月07日
    瀏覽(21)
  • Mybatis-Plus高級查詢LambdaQueryWrapper&QueryWrapper

    目錄 前言 Wrapper 查詢構(gòu)造器 查詢條件 前期準(zhǔn)備 查詢條件 allEq eq ne gt ge lt le between,notBetween like,notLike likeLeft likeRight isNull 空值查詢 isNotNull 非空值查詢 in notIn inSql、notInSql groupBy orderBy、orderByAsc、orderByDesc or、and 解決方法 last exists、notExists 總結(jié) 附加MySQL語句執(zhí)行順序 我剛剛畢

    2024年02月04日
    瀏覽(23)
  • MyBatis-Plus分頁查詢(快速上手運用)

    MyBatis-Plus分頁查詢(快速上手運用)

    Mybatis-Plus知識點[MyBatis+MyBatis-Plus的基礎(chǔ)運用]_心態(tài)還需努力呀的博客-CSDN博客?? Mybatis-Plus+SpringBoot結(jié)合運用_心態(tài)還需努力呀的博客-CSDN博客 MyBaits-Plus中@TableField和@TableId用法_心態(tài)還需努力呀的博客-CSDN博客 MyBatis-Plus中的更新操作(通過id更新和條件更新)_心態(tài)還需努力呀的博

    2024年02月16日
    瀏覽(22)

覺得文章有用就打賞一下文章作者

支付寶掃一掃打賞

博客贊助

微信掃一掃打賞

請作者喝杯咖啡吧~博客贊助

支付寶掃一掃領(lǐng)取紅包,優(yōu)惠每天領(lǐng)

二維碼1

領(lǐng)取紅包

二維碼2

領(lǐng)紅包