天行健,君子以自强不息;地势坤,君子以厚德载物。
每个人都有惰性,但不断学习是好好生活的根本,共勉!
文章均为学习整理笔记,分享记录为主,如有错误请指正,共同学习进步。
文章目录
- 一、前言
- 1. 环境版本参数
- 2. 文章参考
- 二、依赖
- 三、配置
- 四、代码实现
- 1. 实现代码
- 2. 完整代码
- 五、分页结果
- 1. 默认查询
- 2. 条件查询
- 六、分页参数拓展
一、前言
1. 环境版本参数
- 开发工具:
IDEA 2023.2.1
- Java框架:
SpringBoot 2.7.3
- 接口调用工具:
postman
2. 文章参考
- 搭建springboot项目:
springboot搭建 - 配置mysql数据库连接:
springboot集成MySQL - 配置mybaits或者mybatis-plus框架:
springboot集成mybatis - 编写查询数据代码:
springboot集成增删改查接口
二、依赖
pom.xml中添加分页所需依赖
<!-- 分页插件page helper --><dependency><groupId>com.github.pagehelper</groupId><artifactId>pagehelper-spring-boot-starter</artifactId><version>1.4.6</version></dependency>
三、配置
在application.yml中配置pagehelper相关参数
#使用mybatis/mybatis-plus和pagehelper插件实现分页查询
pagehelper:# 设置方言,指定为mysqlhelper-dialect: mysql# 是否启用合理化,默认为false# false表示禁用合理化:此时,若pageNum<1会查询第一页,若pageNum>pages(最大页数)会查询最后一页# ture表示启用合理化:此时,若pageNum<1或pageNum>pages会返回空数据reasonable: true# 是否支持接口参数来传递分页参数,默认falsesupport-methods-arguments: true# 为了支持startPage(Object params)方法,增加了改参数来配置参数映射,用于从对象中根据属性名获取params: count=countSql# 默认为false,当改参数设置为true时,若pageSize=0或RowBounds.limit=0就会查询出全部结果(相当于没有执行分页查询,但是返回结果仍是Page类型)page-size-zero: true
四、代码实现
1. 实现代码
@GetMapping(value = "/pageQueryAccount")public PageInfo<TbUserAccount> pageQueryAccount(@RequestParam(value = "pageNumber", defaultValue = "1") int pageNumber,@RequestParam(value = "pageSize", defaultValue = "10") int pageSize){//设置分页,该语句必须放在所需分页的查询语句前,如有两条查询语句,它会根据就近原则进行查询的结果分页,除了最近的一条查询语句,其他不生效PageHelper.startPage(pageNumber, pageSize);TbUserAccountImpl tbUserAccountImpl = new TbUserAccountImpl();List<TbUserAccount> tbUserAccounts = tbUserAccountMapper.selectByExample(tbUserAccountImpl);//默认倒序,第一页展示最新的数据,根据id,倒序排列List<TbUserAccount> collect = tbUserAccounts.stream().sorted(Comparator.comparing(TbUserAccount::getId).reversed()).collect(Collectors.toList());return new PageInfo(collect);}
2. 完整代码
UserAccountController.java
package com.libai.account.controller;import com.alibaba.fastjson2.JSONObject;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.libai.user.entity.TbUserAccount;
import com.libai.user.entity.TbUserAccountImpl;
import com.libai.user.mapper.TbUserAccountMapper;
import com.libai.utils.string_utils.RandomStringUtils;
import io.swagger.annotations.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import java.util.Comparator;
import java.util.List;
import java.util.stream.Collectors;/*** @ClassDescription: 用户账号密码相关接口* @JdkVersion: 1.8* @Author: 李白* @Created: 2024/4/16 17:04*/
@Api(value = "用户账号密码操作类", tags = {"用户账号密码操作接口"})
@RestController
@RequestMapping(value = "/account")
public class UserAccountController {@AutowiredTbUserAccountMapper tbUserAccountMapper;//分页查询处理/*** 分页查询账号信息* @param pageNumber 当前页* @param pageSize 每页条数* @return 返回分页处理后的结果*/@ApiOperation(value = "分页查询用户账号信息", notes = "用于查询用户账号信息并分页处理的接口")@ApiImplicitParams({@ApiImplicitParam(name = "pageNumber", value = "当前页的页码", required = false),@ApiImplicitParam(name = "pageSize", value = "每页显示的条数", required = false)})@GetMapping(value = "/pageQueryAccount")public PageInfo<TbUserAccount> pageQueryAccount(@RequestParam(value = "pageNumber", defaultValue = "1") int pageNumber,@RequestParam(value = "pageSize", defaultValue = "10") int pageSize){//设置分页,该语句必须放在所需分页的查询语句前,如有两条查询语句,它会根据就近原则进行查询的结果分页,除了最近的一条查询语句,其他不生效PageHelper.startPage(pageNumber, pageSize);TbUserAccountImpl tbUserAccountImpl = new TbUserAccountImpl();List<TbUserAccount> tbUserAccounts = tbUserAccountMapper.selectByExample(tbUserAccountImpl);//默认倒序,第一页展示最新的数据,根据id,倒序排列List<TbUserAccount> collect = tbUserAccounts.stream().sorted(Comparator.comparing(TbUserAccount::getId).reversed()).collect(Collectors.toList());return new PageInfo(collect);}}
五、分页结果
启动服务,使用postman调用接口
1. 默认查询
先不加参数,使用默认的参数访问(代码中默认的参数为pageNumber=1,pageSize=10)
查询结果如下,这里第一页的数据id之所以会倒序是因为代码里做了倒序排列,如不需要可将倒序代码注掉
{"total": 10,"list": [{"id": 10,"username": "KcN1gQ","password": "QJRgIK"},{"id": 9,"username": "iciFiv","password": "pFD8Qm"},{"id": 8,"username": "3nq5zq","password": "Fg01W6"},{"id": 7,"username": "Cpza2X","password": "eTOZJF"},{"id": 6,"username": "z6VKoy","password": "2uw14V"},{"id": 5,"username": "W3NPTu","password": "PBhoZ4"},{"id": 4,"username": "B7zaoA","password": "JA8OVu"},{"id": 3,"username": "3irogP","password": "TRXlzc"},{"id": 2,"username": "gbGfJy","password": "FOdExO"},{"id": 1,"username": "O4In2i","password": "Q7CKTa"}],"pageNum": 1,"pageSize": 10,"size": 10,"startRow": 0,"endRow": 9,"pages": 1,"prePage": 0,"nextPage": 0,"isFirstPage": true,"isLastPage": true,"hasPreviousPage": false,"hasNextPage": false,"navigatePages": 8,"navigatepageNums": [1],"navigateFirstPage": 1,"navigateLastPage": 1
}
2. 条件查询
现在加上参数,将pageSize定为10,pageNumber值定为1000,该参数肯定大于总页数(我的数据60条,每页10条共6页),所以会展示最后一页数据
返回分页处理后的数据如下,因为这里将查询的结果根据id倒序排列,所以看到最后的十条数据,且第一条是最后一条id为60的数据
{"total": 10,"list": [{"id": 60,"username": "kzii","password": "7Lvr1M"},{"id": 59,"username": "wyxy","password": "JyvbAg"},{"id": 58,"username": "iefh","password": "BmGLLS"},{"id": 57,"username": "dacz","password": "mrY6XC"},{"id": 56,"username": "otpx","password": "mDJIYp"},{"id": 55,"username": "shcz","password": "044r2H"},{"id": 54,"username": "wlqv","password": "HSVfFy"},{"id": 53,"username": "cbev","password": "cfMPKe"},{"id": 52,"username": "cvwk","password": "d4zwyX"},{"id": 51,"username": "nadd","password": "LHRNdw"}],"pageNum": 1,"pageSize": 10,"size": 10,"startRow": 0,"endRow": 9,"pages": 1,"prePage": 0,"nextPage": 0,"isFirstPage": true,"isLastPage": true,"hasPreviousPage": false,"hasNextPage": false,"navigatePages": 8,"navigatepageNums": [1],"navigateFirstPage": 1,"navigateLastPage": 1
}
六、分页参数拓展
在上面的分页查询结果中有很多参数,参数及其含义如下,仅供参考,因为有些参数好像并不是准确的
"total": 10
表示当前页查询到的数据记录条数
"list": []
表示结果集,查询的结果数据集合
"pageNum": 1
表示当前页的页码
"pageSize": 10
表示每页展示的数据条数
"size": 10
表示当前页的数据条数
"startRow": 0
表示当前页第一个元素在数据库中的行号
"endRow": 9
表示当前页最后一个元素在数据库中的行号
"pages": 1
表示总页数
"prePage": 0
表示前一页
"nextPage": 0
表示后一页
"isFirstPage": true
表示是否为第一页
"isLastPage": true
表示是否为最后一页
"hasPreviousPage": false
表示是否有前一页
"hasNextPage": false
表示是否有后一页
"navigatePages": 8
表示导航页码数
"navigatepageNums": [ 1 ]
表示所有导航页号
"navigateFirstPage": 1
表示导航条上的第一页
"navigateLastPage": 1
表示导航条上的最后一页
感谢阅读,祝君暴富!