您好,登錄后才能下訂單哦!
這篇文章主要介紹了SpringBoot如何整合PageHelper實(shí)現(xiàn)分頁查詢功能,具有一定借鑒價(jià)值,感興趣的朋友可以參考下,希望大家閱讀完這篇文章之后大有收獲,下面讓小編帶著大家一起了解一下。
在中央倉庫sonatype中搜索 pageHelper
,找到 pagehelper-spring-boot-starter
可以自選版本,選擇你所使用的依賴管理工具對應(yīng)的依賴坐標(biāo),例如我用的依賴管理工具是Maven,那我就選擇對應(yīng)Maven的依賴坐標(biāo)。
添加pagehelper-spring-boot-starter
的Maven依賴
<dependency> <groupId>com.github.pagehelper</groupId> <artifactId>pagehelper-spring-boot-starter</artifactId> <version>1.4.1</version> </dependency>
添加該依賴之后無需再添加MyBatis的依賴,因?yàn)樵撘蕾嚹J(rèn)依賴了MyBatis:
使用yaml格式:
pagehelper: helper-dialect: mysql reasonable: true support-methods-arguments: true params: count=countSql
使用properties格式:
# 指定分頁插件的方言
pagehelper.helper-dialect=mysql
# 分頁合理化
pagehelper.reasonable=true
# 支持方法參數(shù)
pagehelper.support-methods-arguments=true
#配置參數(shù)映射
pagehelper.params=count=countSql
參數(shù)說明:
可參考官方文檔
運(yùn)行結(jié)果:
默認(rèn)SQL語句就是查詢?nèi)繑?shù)據(jù)
使用PageHelper.startPage(pageNum,pageSize)
方法來設(shè)置分頁信息,分別是當(dāng)前頁數(shù)和每頁顯示的總記錄數(shù)
注意:必須在mapper接口中的方法執(zhí)行之前設(shè)置該分頁信息
運(yùn)行結(jié)果:
PageHelper.startPage(pageNum,pageSize)方法只對后面一次查詢生效
下面有2次查詢所有數(shù)據(jù),第1次查詢所有數(shù)據(jù)前插入了PageHelper.startPage(pageNum,pageSize)
方法,所以第1次查詢是分頁查詢,而第2次查詢前沒有插入PageHelper.startPage(pageNum,pageSize)
方法,所以不是分頁查詢。
運(yùn)行結(jié)果:
驗(yàn)證一下數(shù)據(jù)庫的全部數(shù)據(jù)也確實(shí)只有7條:
感謝你能夠認(rèn)真閱讀完這篇文章,希望小編分享的“SpringBoot如何整合PageHelper實(shí)現(xiàn)分頁查詢功能”這篇文章對大家有幫助,同時(shí)也希望大家多多支持億速云,關(guān)注億速云行業(yè)資訊頻道,更多相關(guān)知識等著你來學(xué)習(xí)!
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。