SpringBoot集成PageHelper時出現“在系統中發現了多個分頁插件,請檢查系統配置!”


近日在項目中使用SpringBoot集成PageHelper后,跑單元測試時出現了“在系統中發現了多個分頁插件,請檢查系統配置!”這個問題。

如下圖所示:


   
   
  
  
          
  1. org.mybatis.spring.MyBatisSystemException: nested exception is org.apache.ibatis.exceptions.PersistenceException:
  2. ### Error querying database. Cause: java.lang.RuntimeException: 在系統中發現了多個分頁插件,請檢查系統配置!
  3. ### Cause: java.lang.RuntimeException: 在系統中發現了多個分頁插件,請檢查系統配置!
  4. at org.mybatis.spring.MyBatisExceptionTranslator.translateExceptionIfPossible(MyBatisExceptionTranslator.java:77)
  5. at org.mybatis.spring.SqlSessionTemplate $SqlSessionInterceptor.invoke(SqlSessionTemplate.java:446)
  6. at com.sun.proxy. $Proxy109.selectList(Unknown Source)
  7. at org.mybatis.spring.SqlSessionTemplate.selectList(SqlSessionTemplate.java:230)
  8. at org.apache.ibatis.binding.MapperMethod.executeForMany(MapperMethod.java:137)
  9. at org.apache.ibatis.binding.MapperMethod.execute(MapperMethod.java:75)
  10. at org.apache.ibatis.binding.MapperProxy.invoke(MapperProxy.java:59)
  11. at com.sun.proxy. $Proxy110.selectAll(Unknown Source)
  12. at com.lianjia.cto.ke.broadband.service.StationInfoService.selectPage(StationInfoService.java:27)
  13. at com.lianjia.cto.ke.broadband.service.StationInfoService$ $FastClassBySpringCGLIB$ $ee2f34a4.invoke(<generated>)
  14. at org.springframework.cglib.proxy.MethodProxy.invoke(MethodProxy.java:204)
  15. at org.springframework.aop.framework.CglibAopProxy $CglibMethodInvocation.invokeJoinpoint(CglibAopProxy.java:738)
  16. at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:157)
  17. at org.springframework.aop.framework.adapter.MethodBeforeAdviceInterceptor.invoke(MethodBeforeAdviceInterceptor.java:52)
  18. at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:168)
  19. at org.springframework.aop.interceptor.ExposeInvocationInterceptor.invoke(ExposeInvocationInterceptor.java:92)
  20. at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:179)
  21. at org.springframework.aop.framework.CglibAopProxy $DynamicAdvisedInterceptor.intercept(CglibAopProxy.java:673)
  22. at com.lianjia.cto.ke.broadband.service.StationInfoService$ $EnhancerBySpringCGLIB$ $1da3a0f3.selectPage(<generated>)
  23. at com.lianjia.cto.ke.broadband.service.StationInfoServiceTest.selectPage(StationInfoServiceTest.java:27)
  24. at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
  25. at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
  26. at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
  27. at java.lang.reflect.Method.invoke(Method.java:498)
  28. at org.junit.runners.model.FrameworkMethod $1.runReflectiveCall(FrameworkMethod.java:50)
  29. at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)
  30. at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:47)
  31. at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:17)
  32. at org.springframework.test.context.junit4.statements.RunBeforeTestMethodCallbacks.evaluate(RunBeforeTestMethodCallbacks.java:75)
  33. at org.springframework.test.context.junit4.statements.RunAfterTestMethodCallbacks.evaluate(RunAfterTestMethodCallbacks.java:86)
  34. at org.springframework.test.context.junit4.statements.SpringRepeat.evaluate(SpringRepeat.java:84)
  35. at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:325)
  36. at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.runChild(SpringJUnit4ClassRunner.java:252)
  37. at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.runChild(SpringJUnit4ClassRunner.java:94)
  38. at org.junit.runners.ParentRunner $3.run(ParentRunner.java:290)
  39. at org.junit.runners.ParentRunner $1.schedule(ParentRunner.java:71)
  40. at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288)
  41. at org.junit.runners.ParentRunner.access $000(ParentRunner.java:58)
  42. at org.junit.runners.ParentRunner $2.evaluate(ParentRunner.java:268)
  43. at org.springframework.test.context.junit4.statements.RunBeforeTestClassCallbacks.evaluate(RunBeforeTestClassCallbacks.java:61)
  44. at org.springframework.test.context.junit4.statements.RunAfterTestClassCallbacks.evaluate(RunAfterTestClassCallbacks.java:70)
  45. at org.junit.runners.ParentRunner.run(ParentRunner.java:363)
  46. at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.run(SpringJUnit4ClassRunner.java:191)
  47. at org.junit.runner.JUnitCore.run(JUnitCore.java:137)
  48. at com.intellij.junit4.JUnit4IdeaTestRunner.startRunnerWithArgs(JUnit4IdeaTestRunner.java:68)
  49. at com.intellij.rt.execution.junit.IdeaTestRunner $Repeater.startRunnerWithArgs(IdeaTestRunner.java:47)
  50. at com.intellij.rt.execution.junit.JUnitStarter.prepareStreamsAndStart(JUnitStarter.java:242)
  51. at com.intellij.rt.execution.junit.JUnitStarter.main(JUnitStarter.java:70)
  52. Caused by: org.apache.ibatis.exceptions.PersistenceException:
  53. ### Error querying database. Cause: java.lang.RuntimeException: 在系統中發現了多個分頁插件,請檢查系統配置!
  54. ### Cause: java.lang.RuntimeException: 在系統中發現了多個分頁插件,請檢查系統配置!
  55. at org.apache.ibatis.exceptions.ExceptionFactory.wrapException(ExceptionFactory.java:30)
  56. at org.apache.ibatis.session.defaults.DefaultSqlSession.selectList(DefaultSqlSession.java:150)
  57. at org.apache.ibatis.session.defaults.DefaultSqlSession.selectList(DefaultSqlSession.java:141)
  58. at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
  59. at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
  60. at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
  61. at java.lang.reflect.Method.invoke(Method.java:498)
  62. at org.mybatis.spring.SqlSessionTemplate $SqlSessionInterceptor.invoke(SqlSessionTemplate.java:433)
  63. ... 46 more
  64. Caused by: java.lang.RuntimeException: 在系統中發現了多個分頁插件,請檢查系統配置!
  65. at com.github.pagehelper.PageHelper.skip(PageHelper.java:55)
  66. at com.github.pagehelper.PageInterceptor.intercept(PageInterceptor.java:92)
  67. at org.apache.ibatis.plugin.Plugin.invoke(Plugin.java:61)
  68. at com.sun.proxy. $Proxy123.query(Unknown Source)
  69. at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
  70. at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
  71. at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
  72. at java.lang.reflect.Method.invoke(Method.java:498)
  73. at org.apache.ibatis.plugin.Plugin.invoke(Plugin.java:63)
  74. at com.sun.proxy. $Proxy123.query(Unknown Source)
  75. at com.github.pagehelper.PageInterceptor.executeAutoCount(PageInterceptor.java:201)
  76. at com.github.pagehelper.PageInterceptor.intercept(PageInterceptor.java:113)
  77. at org.apache.ibatis.plugin.Plugin.invoke(Plugin.java:61)
  78. at com.sun.proxy. $Proxy123.query(Unknown Source)
  79. at org.apache.ibatis.session.defaults.DefaultSqlSession.selectList(DefaultSqlSession.java:148)
  80. ... 52 more

先上結果:maven的pagehelper-spring-boot-starter這個依賴,提供了自動配置分頁插件的功能,所以有兩種方法解決這個問題

第一種,SpringBoot啟動類的注解上排除這個自動配置

@SpringBootApplication(exclude = PageHelperAutoConfiguration.class)

  
  
 
 
         

第二種,在構建SqlSessionFactory時,不要再去手動添加分頁的攔截器,在application.yml中進行配置pagehelper的屬性,在SpringBoot啟動時會將配置自動注入生成攔截器


   
   
  
  
          
  1. # PageHelper配置
  2. pagehelper:
  3. offsetAsPageNum: true
  4. rowBoundsWithCount: true
  5. reasonable: true
  6. returnPageInfo: true
  7. params: count=countSql

 


以下是解決問題的思路:

看到這個報錯后,查看了一下配置,發現並沒有配置多個PageHelper。於是查看了一下報錯部分的源碼,問題出現在一個skip方法中,如下圖所示:


   
   
  
  
          
  1. public class PageHelper extends PageMethod implements Dialect {
  2. //...
  3. @Override
  4. public boolean skip(MappedStatement ms, Object parameterObject, RowBounds rowBounds) {
  5. //此處的MSUtils.COUNT為一個常量值"_COUNT"
  6. if(ms.getId().endsWith(MSUtils.COUNT)){
  7. throw new RuntimeException( "在系統中發現了多個分頁插件,請檢查系統配置!");
  8. }
  9. //...
  10. }
  11. //...
  12. }

繼續向上找,找到了分頁插件的攔截器


   
   
  
  
          
  1. public class PageInterceptor implements Interceptor {
  2. //...
  3. private String countSuffix = "_COUNT";
  4. @Override
  5. public Object intercept(Invocation invocation) throws Throwable {
  6. try {
  7. //...
  8. //調用方法判斷是否需要進行分頁,如果不需要,直接返回結果
  9. if (!dialect.skip(ms, parameter, rowBounds)) {
  10. //反射獲取動態參數
  11. String msId = ms.getId();
  12. Configuration configuration = ms.getConfiguration();
  13. Map<String, Object> additionalParameters = (Map<String, Object>) additionalParametersField.get(boundSql);
  14. //判斷是否需要進行 count 查詢
  15. if (dialect.beforeCount(ms, parameter, rowBounds)) {
  16. String countMsId = msId + countSuffix;
  17. Long count;
  18. //先判斷是否存在手寫的 count 查詢
  19. MappedStatement countMs = getExistedMappedStatement(configuration, countMsId);
  20. if(countMs != null){
  21. count = executeManualCount(executor, countMs, parameter, boundSql, resultHandler);
  22. } else {
  23. countMs = msCountMap.get(countMsId);
  24. //自動創建
  25. if (countMs == null) {
  26. //根據當前的 ms 創建一個返回值為 Long 類型的 ms
  27. countMs = MSUtils.newCountMappedStatement(ms, countMsId);
  28. msCountMap.put(countMsId, countMs);
  29. }
  30. count = executeAutoCount(executor, countMs, parameter, boundSql, rowBounds, resultHandler);
  31. }
  32. //處理查詢總數
  33. //返回 true 時繼續分頁查詢,false 時直接返回
  34. if (!dialect.afterCount(count, parameter, rowBounds)) {
  35. //當查詢總數為 0 時,直接返回空的結果
  36. return dialect.afterPage( new ArrayList(), parameter, rowBounds);
  37. }
  38. }
  39. //判斷是否需要進行分頁查詢
  40. if (dialect.beforePage(ms, parameter, rowBounds)) {
  41. //生成分頁的緩存 key
  42. CacheKey pageKey = cacheKey;
  43. //處理參數對象
  44. parameter = dialect.processParameterObject(ms, parameter, boundSql, pageKey);
  45. //調用方言獲取分頁 sql
  46. String pageSql = dialect.getPageSql(ms, boundSql, parameter, rowBounds, pageKey);
  47. BoundSql pageBoundSql = new BoundSql(configuration, pageSql, boundSql.getParameterMappings(), parameter);
  48. //設置動態參數
  49. for (String key : additionalParameters.keySet()) {
  50. pageBoundSql.setAdditionalParameter(key, additionalParameters.get(key));
  51. }
  52. //執行分頁查詢
  53. resultList = executor.query(ms, parameter, RowBounds.DEFAULT, resultHandler, pageKey, pageBoundSql);
  54. } else {
  55. //不執行分頁的情況下,也不執行內存分頁
  56. resultList = executor.query(ms, parameter, RowBounds.DEFAULT, resultHandler, cacheKey, boundSql);
  57. }
  58. } else {
  59. //rowBounds用參數值,不使用分頁插件處理時,仍然支持默認的內存分頁
  60. resultList = executor.query(ms, parameter, rowBounds, resultHandler, cacheKey, boundSql);
  61. }
  62. return dialect.afterPage(resultList, parameter, rowBounds);
  63. } finally {
  64. dialect.afterAll();
  65. }
  66. }
  67. }

可以發現以_COUNT結尾的MappedStatement.id是由分頁插件的攔截器自動生成的,而這個由分頁插件的生成之后,怎么會又一次的調用skip方法呢?所以猜測是有多個分頁插件的攔截器影響到了。於是查閱了一下相關的資料,發現PageHelper引入了SpringBoot的自動配置,以下是自動配置的源碼:


   
   
  
  
          
  1. @Configuration
  2. @ConditionalOnBean(SqlSessionFactory.class)
  3. @EnableConfigurationProperties(PageHelperProperties.class)
  4. @AutoConfigureAfter(MybatisAutoConfiguration.class)
  5. public class PageHelperAutoConfiguration {
  6. @Autowired
  7. private List<SqlSessionFactory> sqlSessionFactoryList;
  8. @Autowired
  9. private PageHelperProperties properties;
  10. /**
  11. * 接受分頁插件額外的屬性
  12. *
  13. * @return
  14. */
  15. @Bean
  16. @ConfigurationProperties(prefix = PageHelperProperties.PAGEHELPER_PREFIX)
  17. public Properties pageHelperProperties() {
  18. return new Properties();
  19. }
  20. @PostConstruct
  21. public void addPageInterceptor() {
  22. PageInterceptor interceptor = new PageInterceptor();
  23. Properties properties = new Properties();
  24. //先把一般方式配置的屬性放進去
  25. properties.putAll(pageHelperProperties());
  26. //在把特殊配置放進去,由於close-conn 利用上面方式時,屬性名就是 close-conn 而不是 closeConn,所以需要額外的一步
  27. properties.putAll( this.properties.getProperties());
  28. interceptor.setProperties(properties);
  29. for (SqlSessionFactory sqlSessionFactory : sqlSessionFactoryList) {
  30. sqlSessionFactory.getConfiguration().addInterceptor(interceptor);
  31. }
  32. }
  33. }

至此,問題算是比較明白了,是因為多個分頁攔截器的作用,導致了該異常的出現,所以全局只要保留一個就可以了。


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM