一文詳解SpringBoot如何使用pageHelper做分頁(yè)處理
分頁(yè)是常見(jiàn)大型項(xiàng)目都需要的一個(gè)功能,PageHelper是一個(gè)非常流行的MyBatis分頁(yè)插件,它支持多數(shù)據(jù)庫(kù)分頁(yè),無(wú)需修改SQL語(yǔ)句即可實(shí)現(xiàn)分頁(yè)功能。
本文在最后展示了兩種依賴(lài)驗(yàn)證的結(jié)果。
一、第一種依賴(lài)方式
1、在項(xiàng)目中使用 PageHelper 插件需要先添加依賴(lài):
<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è)分頁(yè)插件的屬性:
"dialect", "Mysql":指定了數(shù)據(jù)庫(kù)方言為Mysql。(主要是因?yàn)镾QL語(yǔ)句不同)。
"offsetAsPageNum", "true":這個(gè)屬性通常用于指定是否將傳入的 offset 參數(shù)當(dāng)作 pageNum (頁(yè)碼)使用。在這個(gè)配置中,它被設(shè)置為true,意味著如果分頁(yè)查詢(xún)時(shí)傳遞了offset(偏移量),PageHelper會(huì)將其視為頁(yè)碼來(lái)處理。然而,這個(gè)設(shè)置通常不是必需的,因?yàn)镻ageHelper默認(rèn)就是使用頁(yè)碼(pageNum)和每頁(yè)記錄數(shù)(pageSize)來(lái)進(jìn)行分頁(yè)的。
"rowBoundsWithCount", "true":這個(gè)屬性用于指定是否進(jìn)行 count 查詢(xún)以獲取總記錄數(shù)。在分頁(yè)查詢(xún)時(shí),知道總記錄數(shù)是有用的,因?yàn)樗梢宰屇阍谇岸苏故究傢?yè)數(shù)或總記錄數(shù)。設(shè)置為 true 表示 PageHelper 在執(zhí)行分頁(yè)查詢(xún)時(shí),會(huì)先執(zhí)行一個(gè) count 查詢(xún)來(lái)獲取總記錄數(shù)。
二、第二種依賴(lài)方式
<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ù)庫(kù) 可選 pagehelper.reasonable=true // 規(guī)整頁(yè)碼范圍,應(yīng)對(duì)負(fù)數(shù)或過(guò)大頁(yè)碼 pagehelper.support-methods-arguments=true // 規(guī)整可以通過(guò)方法參數(shù)獲取,可用可不用輸入即可 pagehelper.params=count=countSql
pagehelper: helper-dialect: mysql reasonable: true support-methods-arguments: true params: count=countSql
三、創(chuàng)建數(shù)據(jù)庫(kù)表格

分頁(yè)條件配置
pagehelper: helper-dialect: mysql reasonable: true // 規(guī)整頁(yè)碼范圍 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) {
// 這句代碼要放在查詢(xún) mapper 語(yǔ)句的前面
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>
五、第一種依賴(lài)展示結(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
]
}
}
六、第二種依賴(lài)展示結(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做分頁(yè)處理的文章就介紹到這了,更多相關(guān)SpringBoot pageHelper分頁(yè)處理內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
OPENCV+JAVA實(shí)現(xiàn)人臉識(shí)別
這篇文章主要為大家詳細(xì)介紹了OPENCV+JAVA實(shí)現(xiàn)人臉識(shí)別,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2018-02-02
Java中的靜態(tài)綁定和動(dòng)態(tài)綁定詳細(xì)介紹
這篇文章主要介紹了Java中的靜態(tài)綁定和動(dòng)態(tài)綁定詳細(xì)介紹,在Java中存在兩種綁定方式,一種為靜態(tài)綁定,又稱(chēng)作早期綁定,另一種就是動(dòng)態(tài)綁定,亦稱(chēng)為后期綁定,需要的朋友可以參考下2015-01-01
Java實(shí)現(xiàn)自動(dòng)獲取法定節(jié)假日詳細(xì)代碼
這篇文章主要給大家介紹了關(guān)于Java實(shí)現(xiàn)自動(dòng)獲取法定節(jié)假日的相關(guān)資料,獲取并處理節(jié)假日數(shù)據(jù)是一個(gè)常見(jiàn)需求,特別是在需要安排任務(wù)調(diào)度、假期通知等功能的場(chǎng)景中,需要的朋友可以參考下2024-05-05
SpringBoot使用Spring Security實(shí)現(xiàn)登錄注銷(xiāo)功能
這篇文章主要介紹了SpringBoot使用Spring Security實(shí)現(xiàn)登錄注銷(xiāo)功能,本文通過(guò)實(shí)例代碼給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友參考下吧2020-09-09
重新啟動(dòng)IDEA時(shí)maven項(xiàng)目SSM框架文件變色所有@注解失效
這篇文章主要介紹了重新啟動(dòng)IDEA時(shí)maven項(xiàng)目SSM框架文件變色所有@注解失效,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2020-03-03
Spring Boot 集成 InfluxDB 3.x完整教程
本文詳細(xì)介紹了如何在SpringBoot項(xiàng)目中集成InfluxDB3.x,并提供了一個(gè)完整的教程,包括環(huán)境準(zhǔn)備、添加依賴(lài)、配置連接信息、創(chuàng)建客戶(hù)端配置類(lèi)、數(shù)據(jù)寫(xiě)入與查詢(xún)示例以及關(guān)鍵注意事項(xiàng),感興趣的朋友跟隨小編一起看看吧2026-01-01
Java調(diào)用Python代碼實(shí)現(xiàn)Word轉(zhuǎn)化為PDF格式
這篇文章主要為大家詳細(xì)介紹了Java如何實(shí)現(xiàn)調(diào)用Python代碼實(shí)現(xiàn)Word轉(zhuǎn)化為PDF格式,文中的示例代碼講解詳細(xì),感興趣的小伙伴可以跟隨小編一起學(xué)習(xí)一下2026-03-03

