最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

mybatis-plus分頁(yè)插件失效探究解決

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

網(wǎng)上推薦

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

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

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

分頁(yè)插件無(wú)效原因

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

圖1

圖2

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

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

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

解決方案

具體應(yīng)該怎么做呢?像下面這樣,在構(gòu)建SqlSessionFactory時(shí),需要在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è)置自動(dòng)提交
        sqlSessionFactory.openSession(true);
        return sqlSessionFactory;
}

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

//設(shè)置自動(dòng)提交
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í)行者實(shí)例
      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);
    }
    //對(duì)上面的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;
  }

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

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

接下來(lái),PaginationInnerInterceptor.beforeQuery()生成分頁(yè)sql;

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

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

相關(guān)文章

  • Java事務(wù)管理學(xué)習(xí)之JDBC詳解

    Java事務(wù)管理學(xué)習(xí)之JDBC詳解

    這篇文章主要介紹了Java事務(wù)管理學(xué)習(xí)之JDBC的相關(guān)資料,文中介紹的非常詳細(xì),相信對(duì)大家具有一定的參考價(jià)值,需要的朋友們下面來(lái)一起看看吧。
    2017-03-03
  • 一文詳解SpringBoot如何使用pageHelper做分頁(yè)處理

    一文詳解SpringBoot如何使用pageHelper做分頁(yè)處理

    分頁(yè)是常見大型項(xiàng)目都需要的一個(gè)功能,PageHelper是一個(gè)非常流行的MyBatis分頁(yè)插件,下面就跟隨小編一起來(lái)了解下SpringBoot是如何使用pageHelper做分頁(yè)處理的吧
    2025-03-03
  • java獲取IP和IP的歸屬地的方法實(shí)踐

    java獲取IP和IP的歸屬地的方法實(shí)踐

    在Java中獲取IP地址通常指的是獲取本地機(jī)器的IP地址或者通過某種方式獲取的遠(yuǎn)程IP地址,本文就來(lái)詳細(xì)的介紹一下,感興趣的可以了解一下
    2024-05-05
  • Java單例模式實(shí)現(xiàn)靜態(tài)內(nèi)部類方法示例

    Java單例模式實(shí)現(xiàn)靜態(tài)內(nèi)部類方法示例

    這篇文章主要介紹了Java單例模式實(shí)現(xiàn)靜態(tài)內(nèi)部類方法示例,涉及構(gòu)造函數(shù)私有化等相關(guān)內(nèi)容,需要的朋友可以了解下。
    2017-09-09
  • Spring中BeanFactoryPostProcessors是如何執(zhí)行的

    Spring中BeanFactoryPostProcessors是如何執(zhí)行的

    BeanFactoryPostProcessor是Spring容器提供的一個(gè)擴(kuò)展機(jī)制,它允許開發(fā)者在Bean的實(shí)例化和初始化之前對(duì)BeanDefinition進(jìn)行修改和處理,這篇文章主要介紹了你知道Spring中BeanFactoryPostProcessors是如何執(zhí)行的嗎,需要的朋友可以參考下
    2023-11-11
  • java使用for循環(huán)輸出楊輝三角

    java使用for循環(huán)輸出楊輝三角

    楊輝三角形由數(shù)字排列,可以把它看做一個(gè)數(shù)字表,其基本特性是兩側(cè)數(shù)值均為1,其他位置的數(shù)值是其正上方的數(shù)字與左上角數(shù)值之和,下面是java使用for循環(huán)輸出包括10行在內(nèi)的楊輝三角形
    2014-02-02
  • Java實(shí)現(xiàn)預(yù)覽與打印功能詳解

    Java實(shí)現(xiàn)預(yù)覽與打印功能詳解

    在?Java?中,打印功能主要依賴?java.awt.print?包,該包提供了與打印相關(guān)的一些關(guān)鍵類,比如?PrinterJob?和?PageFormat,它們構(gòu)成了?Java?打印框架的核心,接下來(lái)我們將一步步實(shí)現(xiàn)一個(gè)簡(jiǎn)單的預(yù)覽和打印功能,需要的朋友可以參考下
    2025-07-07
  • 帶你了解Java數(shù)據(jù)結(jié)構(gòu)和算法之二叉樹

    帶你了解Java數(shù)據(jù)結(jié)構(gòu)和算法之二叉樹

    這篇文章主要為大家介紹了Java數(shù)據(jù)結(jié)構(gòu)和算法之二叉樹,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下,希望能夠給你帶來(lái)幫助
    2022-01-01
  • java使用jdbc鏈接Oracle示例類分享

    java使用jdbc鏈接Oracle示例類分享

    本文為大家提供一個(gè)java使用jdbc鏈接Oracle的示例類,大家參考使用吧
    2014-01-01
  • Java常用排序算法及性能測(cè)試集合

    Java常用排序算法及性能測(cè)試集合

    周末天氣不好,在家無(wú)事,把常用排序算法理了一遍,收獲不小,特寫文章紀(jì)念。這些算法在學(xué)校的時(shí)候?qū)W過一遍,很多原理都忘記了
    2013-06-06

最新評(píng)論

阿克陶县| 芷江| 利川市| 五峰| 青海省| 香河县| 洪洞县| 黄大仙区| 乌恰县| 沙田区| 巴南区| 炉霍县| 调兵山市| 苍南县| 肥东县| 汉中市| 库车县| 阆中市| 景宁| 衡东县| 门源| 繁昌县| 惠州市| 论坛| 桃源县| 福贡县| 怀仁县| 福建省| 济南市| 尤溪县| 育儿| 孟津县| 阜新市| 广灵县| 定远县| 新沂市| 曲沃县| 岚皋县| 禄劝| 吉安县| 北川|