亚洲乱码中文字幕综合,中国熟女仑乱hd,亚洲精品乱拍国产一区二区三区,一本大道卡一卡二卡三乱码全集资源,又粗又黄又硬又爽的免费视频

mybatis-plus分頁插件失效探究解決

 更新時間:2023年07月06日 09:35:00   作者:子瞻  
這篇文章主要為大家介紹了mybatis-plus分頁插件失效探究解決,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪

網(wǎng)上推薦

網(wǎng)上基本上都是推薦配置如下:

@Bean
    public MybatisPlusInterceptor mybatisPlusInterceptor() {
        MybatisPlusInterceptor mybatisPlusInterceptor = new MybatisPlusInterceptor();
        mybatisPlusInterceptor.addInnerInterceptor(new PaginationInnerInterceptor(DbType.MYSQL));
        return mybatisPlusInterceptor;
    }

但是,僅僅這么做,就能達(dá)到我們的預(yù)期嗎?

分頁插件無效原因

其結(jié)局就是分頁插件沒有效果,原因是為什么呢???

圖1

圖2

通過對比上面兩張圖可以發(fā)現(xiàn),圖一DefaultSqlSession.selectList()底層調(diào)用Plugin.invoke();圖二DefaultSqlSession.selectList()底層調(diào)用CachingExecutor.query()。

其中,圖一是分頁插件生效的調(diào)用鏈,圖二是分頁插件失效的調(diào)用鏈。

也就是說,分頁插件失效的原因是,mybatis-plusPlugin類沒有為分頁插件攔截器生成Executor代理。

解決方案

具體應(yīng)該怎么做呢?像下面這樣,在構(gòu)建SqlSessionFactory時,需要在MybatisSqlSessionFactoryBean顯示設(shè)置Plugin。

@Bean(name = "defaultSqlSessionFactory")
    public SqlSessionFactory defaultSqlSessionFactory(){
        MybatisSqlSessionFactoryBean bean = new MybatisSqlSessionFactoryBean();
        bean.setDataSource(dataSource);
        //設(shè)置攔截器
        bean.setPlugins(mybatisPlusInterceptor);
        SqlSessionFactory sqlSessionFactory = bean.getObject();
        //設(shè)置自動提交
        sqlSessionFactory.openSession(true);
        return sqlSessionFactory;
}

那么,為分頁插件生成代理類是在什么時機(jī)生成呢?先公布答案:

//設(shè)置自動提交
sqlSessionFactory.openSession(true);

調(diào)用鏈如下

圖3

咱再看細(xì)節(jié):DefaultSqlSessionFactory.openSessionFromDataSource()詳情:

private SqlSession openSessionFromDataSource(ExecutorType execType, TransactionIsolationLevel level, boolean autoCommit) {
    Transaction tx = null;
    try {
      final Environment environment = configuration.getEnvironment();
      final TransactionFactory transactionFactory = getTransactionFactoryFromEnvironment(environment);
      tx = transactionFactory.newTransaction(environment.getDataSource(), level, autoCommit);
      //這步很關(guān)鍵,創(chuàng)建執(zhí)行者實例
      final Executor executor = configuration.newExecutor(tx, execType);
      return new DefaultSqlSession(configuration, executor, autoCommit);
    } catch (Exception e) {
      closeTransaction(tx); // may have fetched a connection so lets call close()
      throw ExceptionFactory.wrapException("Error opening session.  Cause: " + e, e);
    } finally {
      ErrorContext.instance().reset();
    }
  }

Configuration.newExecutor()詳情:

public Executor newExecutor(Transaction transaction, ExecutorType executorType) {
    executorType = executorType == null ? defaultExecutorType : executorType;
    executorType = executorType == null ? ExecutorType.SIMPLE : executorType;
    Executor executor;
    if (ExecutorType.BATCH == executorType) {
      executor = new BatchExecutor(this, transaction);
    } else if (ExecutorType.REUSE == executorType) {
      executor = new ReuseExecutor(this, transaction);
    } else {
      executor = new SimpleExecutor(this, transaction);
    }
    if (cacheEnabled) {
      executor = new CachingExecutor(executor);
    }
    //對上面的executor進(jìn)行代理(目的是把插件和執(zhí)行器封裝為代理類)
    executor = (Executor) interceptorChain.pluginAll(executor);
    return executor;
  }

MybatisPlusInterceptor.pluginAll();

public Object pluginAll(Object target) {
    for (Interceptor interceptor : interceptors) {
      target = interceptor.plugin(target);
    }
    return target;
  }

通過上面的重點code展示,我們大致了解了部分重要節(jié)點中分頁插件代理類生成的邏輯。接下來我們繼續(xù)了解具體分頁插件工作的效果。

圖4

public boolean willDoQuery(){
        if (countMs != null) {
            countSql = countMs.getBoundSql(parameter);
        } else {
            countMs = buildAutoCountMappedStatement(ms);
            //生成查詢count SQL
            String countSqlStr = autoCountSql(page, boundSql.getSql());
            PluginUtils.MPBoundSql mpBoundSql = PluginUtils.mpBoundSql(boundSql);
            //構(gòu)建BoundSql
            countSql = new BoundSql(countMs.getConfiguration(), countSqlStr, mpBoundSql.parameterMappings(), parameter);
            PluginUtils.setAdditionalParameter(countSql, mpBoundSql.additionalParameters());
        }
        //查詢 count 數(shù)值
        List<Object> result = executor.query(countMs, parameter, rowBounds, resultHandler, cacheKey, countSql);
}

接下來,PaginationInnerInterceptor.beforeQuery()生成分頁sql;

最終MybatisPlusInterceptor.intercept()里面的executor.query()執(zhí)行分頁sql。

以上就是mybatis-plus分頁插件失效探究解決的詳細(xì)內(nèi)容,更多關(guān)于mybatis plus分頁插件失效的資料請關(guān)注腳本之家其它相關(guān)文章!

相關(guān)文章

  • Java代碼中如何去掉煩人的“!=null”

    Java代碼中如何去掉煩人的“!=null”

    這篇文章主要介紹了Java代碼中去掉煩人的“!=null”,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2020-05-05
  • 深入理解JVM之Java對象的創(chuàng)建、內(nèi)存布局、訪問定位詳解

    深入理解JVM之Java對象的創(chuàng)建、內(nèi)存布局、訪問定位詳解

    這篇文章主要介紹了深入理解JVM之Java對象的創(chuàng)建、內(nèi)存布局、訪問定位,結(jié)合實例形式詳細(xì)分析了Java對象的創(chuàng)建、內(nèi)存布局、訪問定位相關(guān)概念、原理、操作技巧與注意事項,需要的朋友可以參考下
    2019-09-09
  • JAVA?IDEA項目打包為jar包的步驟詳解

    JAVA?IDEA項目打包為jar包的步驟詳解

    在Java開發(fā)中我們通常會將我們的項目打包成可執(zhí)行的Jar包,以便于在其他環(huán)境中部署和運行,下面這篇文章主要給大家介紹了關(guān)于JAVA?IDEA項目打包為jar包的相關(guān)資料,需要的朋友可以參考下
    2024-08-08
  • Kotlin-Coroutines中的async與await異步協(xié)程管理

    Kotlin-Coroutines中的async與await異步協(xié)程管理

    這篇文章主要為大家介紹了Kotlin-Coroutines中的async與await異步協(xié)程管理,提升程序性能解析,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪
    2023-10-10
  • Java中的封裝、繼承和多態(tài),你真的都懂了嗎

    Java中的封裝、繼承和多態(tài),你真的都懂了嗎

    Java中的封裝、繼承和多態(tài)知識點是學(xué)習(xí)java必備的基礎(chǔ)知識,看似簡單,真正理解起來還是有一定難度的,今天小編再次通過實例代碼給大家講解java 封裝繼承多態(tài)知識,感興趣的朋友一起學(xué)習(xí)下吧
    2021-05-05
  • SpringBoot之RestTemplate在URL中轉(zhuǎn)義字符的問題

    SpringBoot之RestTemplate在URL中轉(zhuǎn)義字符的問題

    這篇文章主要介紹了SpringBoot之RestTemplate在URL中轉(zhuǎn)義字符的問題,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教
    2023-06-06
  • 在SpringBoot 中從application.yml中獲取自定義常量方式

    在SpringBoot 中從application.yml中獲取自定義常量方式

    這篇文章主要介紹了在SpringBoot 中從application.yml中獲取自定義常量方式,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2020-04-04
  • java 多線程死鎖詳解及簡單實例

    java 多線程死鎖詳解及簡單實例

    這篇文章主要介紹了java 多線程死鎖詳解及簡單實例的相關(guān)資料,需要的朋友可以參考下
    2017-01-01
  • java boolean占用內(nèi)存大小說明

    java boolean占用內(nèi)存大小說明

    這篇文章主要介紹了java boolean占用內(nèi)存大小,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教
    2021-06-06
  • Java程序員常犯的五個錯誤

    Java程序員常犯的五個錯誤

    這篇文章總結(jié)以前經(jīng)驗針對java編程的一些習(xí)慣,給出一些關(guān)于java編程的建議: 當(dāng)你開始成為一個程序員的時候,在編程的時候很容易陷入下面所述的一些壞習(xí)慣,下面把Java程序員常犯的五個錯誤整理如下,需要的朋友可以參考下
    2015-07-07

最新評論