溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點(diǎn)擊 登錄注冊 即表示同意《億速云用戶服務(wù)條款》

SpringBoot如何整合PageHelper實(shí)現(xiàn)分頁查詢功能

發(fā)布時(shí)間:2022-03-03 13:40:07 來源:億速云 閱讀:319 作者:小新 欄目:開發(fā)技術(shù)

這篇文章主要介紹了SpringBoot如何整合PageHelper實(shí)現(xiàn)分頁查詢功能,具有一定借鑒價(jià)值,感興趣的朋友可以參考下,希望大家閱讀完這篇文章之后大有收獲,下面讓小編帶著大家一起了解一下。

使用方法

導(dǎo)入依賴

在中央倉庫sonatype中搜索 pageHelper,找到 pagehelper-spring-boot-starter

SpringBoot如何整合PageHelper實(shí)現(xiàn)分頁查詢功能

可以自選版本,選擇你所使用的依賴管理工具對應(yīng)的依賴坐標(biāo),例如我用的依賴管理工具是Maven,那我就選擇對應(yīng)Maven的依賴坐標(biāo)。

SpringBoot如何整合PageHelper實(shí)現(xiàn)分頁查詢功能

添加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:

SpringBoot如何整合PageHelper實(shí)現(xiàn)分頁查詢功能

編寫配置文件

使用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ù)說明:

可參考官方文檔

SpringBoot如何整合PageHelper實(shí)現(xiàn)分頁查詢功能

準(zhǔn)備數(shù)據(jù)表

SpringBoot如何整合PageHelper實(shí)現(xiàn)分頁查詢功能

準(zhǔn)備Mapper接口

SpringBoot如何整合PageHelper實(shí)現(xiàn)分頁查詢功能

測試查詢?nèi)繑?shù)據(jù)

SpringBoot如何整合PageHelper實(shí)現(xiàn)分頁查詢功能

運(yùn)行結(jié)果:

默認(rèn)SQL語句就是查詢?nèi)繑?shù)據(jù)

SpringBoot如何整合PageHelper實(shí)現(xiàn)分頁查詢功能

PageHelper 分頁查詢

使用PageHelper.startPage(pageNum,pageSize)方法來設(shè)置分頁信息,分別是當(dāng)前頁數(shù)和每頁顯示的總記錄數(shù)

注意:必須在mapper接口中的方法執(zhí)行之前設(shè)置該分頁信息

SpringBoot如何整合PageHelper實(shí)現(xiàn)分頁查詢功能

運(yùn)行結(jié)果:

SpringBoot如何整合PageHelper實(shí)現(xiàn)分頁查詢功能

PageHelper.startPage(pageNum,pageSize)方法只對后面一次查詢生效

下面有2次查詢所有數(shù)據(jù),第1次查詢所有數(shù)據(jù)前插入了PageHelper.startPage(pageNum,pageSize)方法,所以第1次查詢是分頁查詢,而第2次查詢前沒有插入PageHelper.startPage(pageNum,pageSize)方法,所以不是分頁查詢。

SpringBoot如何整合PageHelper實(shí)現(xiàn)分頁查詢功能

運(yùn)行結(jié)果:

SpringBoot如何整合PageHelper實(shí)現(xiàn)分頁查詢功能

驗(yàn)證一下數(shù)據(jù)庫的全部數(shù)據(jù)也確實(shí)只有7條:

SpringBoot如何整合PageHelper實(shí)現(xiàn)分頁查詢功能

感謝你能夠認(rèn)真閱讀完這篇文章,希望小編分享的“SpringBoot如何整合PageHelper實(shí)現(xiàn)分頁查詢功能”這篇文章對大家有幫助,同時(shí)也希望大家多多支持億速云,關(guān)注億速云行業(yè)資訊頻道,更多相關(guān)知識等著你來學(xué)習(xí)!

向AI問一下細(xì)節(jié)

免責(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)容。

AI