1、官網https://pagehelper.github.io/
2、集成springboot文檔 https://github.com/pagehelper/pagehelper-spring-boot
使用細則:https://github.com/pagehelper/Mybatis-PageHelper/blob/master/wikis/zh/HowToUse.md
example:https://github.com/abel533/MyBatis-Spring-Boot
3、分頁插件參數介紹
分頁插件提供了多個可選參數,在springboot application.properties 中以 pagehelper.<參數名> = <參數值> 的形式進行配置即可,分頁插件可選參數如下
參數 |
說明 |
---|---|
dialect默認情況下會使用 PageHelper 方式進行分頁,如果想要實現自己的分頁邏輯,可以實現 Dialect(com.github.pagehelper.Dialect) 接口,然後配置該屬性爲實現類的全限定名稱。 下面幾個參數都是針對默認 dialect 情況下的參數。使用自定義 dialect 實現時,下面的參數沒有任何作用。 |
|
helperDialect |
特別注意:使用 SqlServer2012 數據庫時,需要手動指定爲 sqlserver2012,否則會使用 SqlServer2005 的方式進行分頁。
|
offsetAsPageNum |
|
rowBoundsWithCount |
|
pageSizeZero |
|
reasonable |
|
params |
|
supportMethodsArguments |
|
autoRuntimeDialect |
|
closeConn |
|
aggregateFunctions(5.1.5+) |
|
重要提示:
|
4、查看pagehelper-spring-boot-starter 引用依賴的結構 (pagehelper-spring-boot/pagehelper-spring-boot-starter/pom.xml)
5、原理
(1)mybatis集成pagehelper,需要在Mybatis的配置文件中註冊需要使用的Plugin ,
PageHelper中對應的Plugin實現類就是com.github.pagehelper.PageHelper自身
mybatis plugin 實則是org.apache.ibatis.plugin.Interceptor接口,
因爲Interceptor的核心是其中的plugin(Object target)方法,而對於plugin(Object target)方法的實現,我們在需要對對應的對象進行攔截時會通過org.apache.ibatis.plugin.Plugin的靜態方法wrap(Object target, Interceptor interceptor)返回一個代理對象,而方法入參就是當前的Interceptor實現類。
<plugins> <plugin interceptor="com.github.pagehelper.PageHelper"/> </plugins>
(2) PageHelper攔截的是org.apache.ibatis.executor.Executor的query方法
(3)ThreadLocal:
java.lang.ThreadLocal
(4)PageHelper.startPage(..): 這樣PageHelper會把分頁信息存入一個ThreadLocal變量中,在攔截到Executor的query方法執行時會從對應的ThreadLocal中獲取分頁信息,獲取到了,則進行分頁處理,處理完了後又會把ThreadLocal中的分頁信息清理掉,以便不影響下一次的查詢操作
(5)com.github.pagehelper.Page: 在進行分頁查詢時,我們的返回結果一般是一個java.util.List,PageHelper分頁查詢後的結果會變成com.github.pagehelper.Page類型,其繼承了java.util.ArrayList,我們自己聲明mapper 方法中的返回爲List時也不會報錯,因爲做了強轉
com.github.pagehelper.Page中包含有返回結果的分頁信息,包括總記錄數,總的分頁數等信息,所以一般我們需要把返回結果強轉爲com.github.pagehelper.Page類型
6、框架集成
(1)導入maven依賴
只需要導入pagehelper-spring-boot-starter 依賴,其他的不需要
(2)配置pagehelper參數(配置幾個常用核心參數,,其他參數可以根據業務情況配置)
注意: 在springboot 、mybatis、 pagehelper的集成中,不需要在手動爲mybatis添加plugin, 因爲pagehelper-spring-boot-starter 已經做了自動化配置