一文詳解SpringBoot如何使用pageHelper做分頁處理
分頁是常見大型項(xiàng)目都需要的一個(gè)功能,PageHelper是一個(gè)非常流行的MyBatis分頁插件,它支持多數(shù)據(jù)庫分頁,無需修改SQL語句即可實(shí)現(xiàn)分頁功能。
本文在最后展示了兩種依賴驗(yàn)證的結(jié)果。
一、第一種依賴方式
1、在項(xiàng)目中使用 PageHelper 插件需要先添加依賴:
<dependency> <groupId>com.github.pagehelper</groupId> <artifactId>pagehelper</artifactId> <version>4.1.3</version> </dependency>
2、這種方式需要配置一個(gè) config 文件
package com.wen.config; import com.github.pagehelper.PageHelper; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import java.util.Properties; /** * @author : rjw * @date : 2024-09-20 */ @Configuration public class MyBatisConfig { @Bean public PageHelper pageHelper() { PageHelper pageHelper = new PageHelper(); Properties properties = new Properties(); properties.setProperty("dialect", "Mysql"); properties.setProperty("offsetAsPageNum", "true"); properties.setProperty("rowBoundsWithCount", "true"); pageHelper.setProperties(properties); return pageHelper; } }
3、setProperty
方法設(shè)置了三個(gè)分頁插件的屬性:
"dialect", "Mysql":指定了數(shù)據(jù)庫方言為Mysql。(主要是因?yàn)镾QL語句不同)。
"offsetAsPageNum", "true":這個(gè)屬性通常用于指定是否將傳入的 offset 參數(shù)當(dāng)作 pageNum (頁碼)使用。在這個(gè)配置中,它被設(shè)置為true,意味著如果分頁查詢時(shí)傳遞了offset(偏移量),PageHelper會將其視為頁碼來處理。然而,這個(gè)設(shè)置通常不是必需的,因?yàn)镻ageHelper默認(rèn)就是使用頁碼(pageNum)和每頁記錄數(shù)(pageSize)來進(jìn)行分頁的。
"rowBoundsWithCount", "true":這個(gè)屬性用于指定是否進(jìn)行 count 查詢以獲取總記錄數(shù)。在分頁查詢時(shí),知道總記錄數(shù)是有用的,因?yàn)樗梢宰屇阍谇岸苏故究傢摂?shù)或總記錄數(shù)。設(shè)置為 true 表示 PageHelper 在執(zhí)行分頁查詢時(shí),會先執(zhí)行一個(gè) count 查詢來獲取總記錄數(shù)。
二、第二種依賴方式
<dependency> <groupId>com.github.pagehelper</groupId> <artifactId>pagehelper-spring-boot-starter</artifactId> <version>1.2.10</version> </dependency>
這種方式需要在配置文件配置一下,application.properties
或 application.yml
。
pagehelper.helper-dialect=mysql // 數(shù)據(jù)庫 可選 pagehelper.reasonable=true // 規(guī)整頁碼范圍,應(yīng)對負(fù)數(shù)或過大頁碼 pagehelper.support-methods-arguments=true // 規(guī)整可以通過方法參數(shù)獲取,可用可不用輸入即可 pagehelper.params=count=countSql
pagehelper: helper-dialect: mysql reasonable: true support-methods-arguments: true params: count=countSql
三、創(chuàng)建數(shù)據(jù)庫表格
分頁條件配置
pagehelper: helper-dialect: mysql reasonable: true // 規(guī)整頁碼范圍 support-methods-arguments: true // 規(guī)整方法參數(shù)獲取
四、代碼示例
關(guān)于統(tǒng)一 API 響應(yīng)結(jié)果封裝,代碼示例在 SpringBoot 項(xiàng)目統(tǒng)一 API 響應(yīng)結(jié)果封裝。
關(guān)于 mybatis 的項(xiàng)目搭建在 SpringBoot 項(xiàng)目整合 MyBatis 框架 。
1、TestController
package com.wen.controller; import com.wen.data.Result; import com.wen.data.ResultGenerator; import com.wen.dto.TbUser; import com.wen.service.TestService; import org.apache.ibatis.annotations.Param; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController; @RestController @RequestMapping("/test") public class TestController { @Autowired private TestService testService; @GetMapping("/select") public Result<?> selectUserByPage( @Param("pageSize") Integer pageSize, @Param("pageNumber") Integer pageNumber){ return ResultGenerator.genSuccessResult(testService.selectUserByPage(pageSize, pageNumber)); } }
2、TestService
package com.wen.service; import com.github.pagehelper.PageInfo; import com.wen.dto.TbUser; public interface TestService { PageInfo<TbUser> selectUserByPage(Integer pageSize, Integer pageNumber); }
3、TestServiceImpl
package com.wen.service.impl; import com.github.pagehelper.PageHelper; import com.github.pagehelper.PageInfo; import com.wen.dto.TbUser; import com.wen.mapper.TbUserMapper; import com.wen.service.TestService; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; import java.util.List; @Service public class TestServiceImpl implements TestService { @Autowired private TbUserMapper tbUserMapper; @Override public PageInfo<TbUser> selectUserByPage(Integer pageSize, Integer pageNumber) { // 這句代碼要放在查詢 mapper 語句的前面 PageHelper.startPage(pageNumber, pageSize); List<TbUser> tbUsers = tbUserMapper.selectUser(); PageInfo<TbUser> tbUserPageInfo = new PageInfo<>(tbUsers); return tbUserPageInfo; } }
4、TbUserMapper
package com.wen.mapper; import com.wen.dto.TbUser; import org.apache.ibatis.annotations.Mapper; import java.util.List; @Mapper public interface TbUserMapper { List<TbUser> selectUser(); }
5、TbUserMapper.xml
<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd"> <mapper namespace="com.wen.mapper.TbUserMapper"> <select id="selectUser" resultType="com.wen.dto.TbUser"> SELECT username, password FROM tb_user </select> </mapper>
五、第一種依賴展示結(jié)果
http://localhost:8080/test/select?pageSize=5&pageNumber=1
{ "code": 1, "message": "SUCCESS", "data": { "pageNum": 1, "pageSize": 5, "size": 5, "orderBy": null, "startRow": 1, "endRow": 5, "total": 7, "pages": 2, "list": [ { "id": 0, "username": "laowang", "password": "112233" }, { "id": 0, "username": "laoli", "password": "123456" }, { "id": 0, "username": "lisi", "password": "3344" }, { "id": 0, "username": "wangwu", "password": "6677" }, { "id": 0, "username": "周周", "password": "111" } ], "firstPage": 1, "prePage": 0, "nextPage": 2, "lastPage": 2, "isFirstPage": true, "isLastPage": false, "hasPreviousPage": false, "hasNextPage": true, "navigatePages": 8, "navigatepageNums": [ 1, 2 ] } }
六、第二種依賴展示結(jié)果
http://localhost:8080/test/select?pageSize=5&pageNumber=1
{ "code": 1, "message": "SUCCESS", "data": { "total": 7, "list": [ { "id": 0, "username": "laowang", "password": "112233" }, { "id": 0, "username": "laoli", "password": "123456" }, { "id": 0, "username": "lisi", "password": "3344" }, { "id": 0, "username": "wangwu", "password": "6677" }, { "id": 0, "username": "周周", "password": "111" } ], "pageNum": 1, "pageSize": 5, "size": 5, "startRow": 1, "endRow": 5, "pages": 2, "prePage": 0, "nextPage": 2, "isFirstPage": true, "isLastPage": false, "hasPreviousPage": false, "hasNextPage": true, "navigatePages": 8, "navigatepageNums": [ 1, 2 ], "navigateFirstPage": 1, "navigateLastPage": 2 } }
到此這篇關(guān)于一文詳解SpringBoot如何使用pageHelper做分頁處理的文章就介紹到這了,更多相關(guān)SpringBoot pageHelper分頁處理內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Java實(shí)現(xiàn)拓?fù)渑判虻氖纠a
這篇文章我們要講的是拓?fù)渑判颍@是一個(gè)針對有向無環(huán)圖的算法,主要是為了解決前驅(qū)后繼的關(guān)系,感興趣的小伙伴可以跟隨小編一起學(xué)習(xí)一下2022-05-05Java web Hibernate如何與數(shù)據(jù)庫鏈接
這篇文章主要介紹了Java web Hibernate如何與數(shù)據(jù)庫鏈接,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2020-06-06如何使用Gradle實(shí)現(xiàn)類似Maven的profiles功能
這篇文章主要介紹了如何使用Gradle實(shí)現(xiàn)類似Maven的profiles功能,本文通過實(shí)例代碼給大家介紹的非常詳細(xì),感興趣的朋友跟隨小編一起看看吧2024-06-06淺談java.util.concurrent包中的線程池和消息隊(duì)列
這篇文章主要介紹了淺談java.util.concurrent包中的線程池和消息隊(duì)列,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2019-08-08springboot?+rabbitmq+redis實(shí)現(xiàn)秒殺示例
本文主要介紹了springboot?+rabbitmq+redis實(shí)現(xiàn)秒殺示例,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2022-07-07Netty分布式ByteBuf中PooledByteBufAllocator剖析
這篇文章主要為大家介紹了Netty分布式ByteBuf剖析PooledByteBufAllocator簡述,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-03-03