SpringBoot 项目如何使用 pageHelper 做分页处理 (含两种依赖方式)

分页是常见大型项目都需要的一个功能,PageHelper是一个非常流行的MyBatis分页插件,它支持多数据库分页,无需修改SQL语句即可实现分页功能。

本文在最后展示了两种依赖验证的结果。

文章目录

    • 一、第一种依赖方式
    • 二、第二种依赖方式
    • 三、创建数据库表格
    • 四、代码示例
      • 1、TestController
      • 2、TestService
      • 3、TestServiceImpl
      • 4、TbUserMapper
      • 5、TbUserMapper.xml
    • 五、第一种依赖展示结果
    • 六、第二种依赖展示结果

一、第一种依赖方式

1、在项目中使用 PageHelper 插件需要先添加依赖:

<dependency><groupId>com.github.pagehelper</groupId><artifactId>pagehelper</artifactId><version>4.1.3</version>
</dependency>

2、这种方式需要配置一个 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 {@Beanpublic 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 方法设置了三个分页插件的属性:

  • "dialect", "Mysql":指定了数据库方言为Mysql。(主要是因为SQL语句不同)。
  • "offsetAsPageNum", "true":这个属性通常用于指定是否将传入的 offset 参数当作 pageNum (页码)使用。在这个配置中,它被设置为true,意味着如果分页查询时传递了offset(偏移量),PageHelper会将其视为页码来处理。然而,这个设置通常不是必需的,因为PageHelper默认就是使用页码(pageNum)和每页记录数(pageSize)来进行分页的。
  • "rowBoundsWithCount", "true":这个属性用于指定是否进行 count 查询以获取总记录数。在分页查询时,知道总记录数是有用的,因为它可以让你在前端展示总页数或总记录数。设置为 true 表示 PageHelper 在执行分页查询时,会先执行一个 count 查询来获取总记录数。

二、第二种依赖方式

<dependency><groupId>com.github.pagehelper</groupId><artifactId>pagehelper-spring-boot-starter</artifactId><version>1.2.10</version>
</dependency>

2、这种方式需要在配置文件配置一下,application.propertiesapplication.yml

pagehelper.helper-dialect=mysql   // 数据库   可选
pagehelper.reasonable=true        // 规整页码范围,应对负数或过大页码
pagehelper.support-methods-arguments=true  // 规整可以通过方法参数获取,可用可不用输入即可
pagehelper.params=count=countSql
pagehelper:helper-dialect: mysqlreasonable: truesupport-methods-arguments: trueparams: count=countSql

三、创建数据库表格

在这里插入图片描述
3、分页条件配置

pagehelper:helper-dialect: mysqlreasonable: true   // 规整页码范围support-methods-arguments: true   // 规整方法参数获取

四、代码示例

关于统一 API 响应结果封装,代码示例在 SpringBoot 项目统一 API 响应结果封装 。

关于 mybatis 的项目搭建在 SpringBoot 项目整合 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 {@Autowiredprivate 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 {@Autowiredprivate TbUserMapper tbUserMapper;@Overridepublic 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>

五、第一种依赖展示结果

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]}
}

六、第二种依赖展示结果

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}
}

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.xdnf.cn/news/145822.html

如若内容造成侵权/违法违规/事实不符,请联系一条长河网进行投诉反馈,一经查实,立即删除!

相关文章

Virtuoso服务在centos中自动停止的原因分析及解决方案

目录 前言1. 问题背景2. 原因分析2.1 终端关闭导致信号12.2 nohup命令的局限性 3. 解决方案3.1 使用 screen 命令保持会话3.2 使用 tmux 作为替代方案3.3 使用系统服务&#xff08;systemd&#xff09; 4. 其他注意事项4.1 网络配置4.2 日志监控 结语 前言 在使用Virtuoso作为…

Transformer 的可视化解释

Transformer 的可视化解释&#xff1a;了解 LLM Transformer 模型如何与交互式可视化配合使用 部署 Nodejs version > 20.0 git clone https://github.com/poloclub/transformer-explainer.git cd transformer-explainer npm install npm run dev# fix: cnpm install --pl…

AD9854 为什么输出波形幅度受限??

&#x1f3c6;本文收录于《全栈Bug调优(实战版)》专栏&#xff0c;主要记录项目实战过程中所遇到的Bug或因后果及提供真实有效的解决方案&#xff0c;希望能够助你一臂之力&#xff0c;帮你早日登顶实现财富自由&#x1f680;&#xff1b;同时&#xff0c;欢迎大家关注&&am…

lambda 自调用递归

从前序与中序遍历序列构造二叉树 官方解析实在是记不住&#xff0c;翻别人的题解发现了一个有意思的写法 class Solution { public:TreeNode* buildTree(vector<int>& preorder, vector<int>& inorder) {auto dfs [](auto&& dfs, auto&&…

Pandas和matplotlib实现同期天气温度对比

目录 1、下载近两年的天气Excel数据 2、pandas加载Excel 3、将时间作为索引 4、按日计算最值、均值 5、选取近两年同期温度数据 6、同期温度曲线对比,共享y轴 1、下载近两年的天气Excel数据 一个免费的天气数据下载网址:METAR北京(机场)历史天气 (rp5.ru) 选择”北京天…

centos 7.9安装k8s

前言 Kubernetes单词来自于希腊语&#xff0c;含义是领航员&#xff0c;生产环境级别的容器编排技术&#xff0c;可实现容器的自动部署扩容以及管理。Kubernetes也称为K8S&#xff0c;其中8代表中间8个字符&#xff0c;是Google在2014年的开源的一个容器编排引擎技术&#xff…

一文读懂SpringCLoud

一、前言 只有光头才能变强 认识我的朋友可能都知道我这阵子去实习啦&#xff0c;去的公司说是用SpringCloud(但我觉得使用的力度并不大啊~~)… 所以&#xff0c;这篇主要来讲讲SpringCloud的一些基础的知识。(我就是现学现卖了&#xff0c;主要当做我学习SpringCloud的笔记吧&…

【JPCS出版】第二届应用统计、建模与先进算法国际学术会议(ASMA2024,9月27日-29)

第二届应用统计、建模与先进算法国际学术会议 2024 2nd International Conference on Applied Statistics, Modeling and Advanced Algorithms&#xff08;ASMA2024&#xff09; 会议官方 会议官网&#xff1a;www.icasma.org 2024 2nd International Conference on Applied …

Moveit2与gazebo联合仿真:添加摄像头传感器

1.代码更新修改 1.1 添加物理关节 如图&#xff0c;在原有机械臂的基础上添加camera_link和base_camera_joint作为传感器的几何属性 对应的xml代码如下 <link name"${prefix}camera_link"><collision><geometry><box size"0.01 0.1 0.05&…

【Python】练习:控制语句(二)第4关

第4关&#xff1a;控制结构综合实训 第一题第二题&#xff08;※&#xff09;第三题&#xff08;※&#xff09;第四题&#xff08;※&#xff09;第五题&#xff08;※&#xff09;第六题&#xff08;※&#xff09; 第一题 #第一题def rankHurricane(velocity):#请在下面编写…

毫米波雷达预警功能 —— 盲区检测(BSD)预警

文档声明&#xff1a; 以下资料均属于本人在学习过程中产出的学习笔记&#xff0c;如果错误或者遗漏之处&#xff0c;请多多指正。并且该文档在后期会随着学习的深入不断补充完善。感谢各位的参考查看。 笔记资料仅供学习交流使用&#xff0c;转载请标明出处&#xff0c;谢谢配…

MySQL高阶1875-将工资相同的雇员分组

目录 题目 准备数据 分析数据 题目 编写一个解决方案来获取每一个被分配到组中的雇员的 team_id 。 返回的结果表按照 team_id 升序排列。如果相同&#xff0c;则按照 employee_id 升序排列。 这家公司想要将 工资相同 的雇员划分到同一个组中。每个组需要满足如下要求&a…

Lichee NanoKVM基本使用环境

Lichee NanoKVM基本使用环境 本文章主要记录一些自己在初期的使用&#xff0c;以及自己的一些经验 &#xff0c;非常感谢sipeed NanoKVM官方使用教程 外观&#xff08;博主自己的是lite版本&#xff0c;非常感谢sipeed&#xff09; Lichee NanoKVM 是基于 LicheeRV Nano 的 I…

msvcp120dll丢失问题的相关分享,4种靠谱的修复msvcp120dll的方法

在你启动某个软件或游戏的过程中&#xff0c;如果屏幕上突然出现一条提示说“msvcp120.dll文件缺失”这时候请不要紧张&#xff0c;要解决这个问题还是比较简单的。msvcp120.dll 是一个关键的系统文件&#xff0c;属于 Microsoft Visual C 可再发行组件包的一部分。它包含了许多…

电影《祝你幸福!》观后感

上周看了电影《祝你幸福&#xff01;》&#xff0c;虽然讲述的是一个悲伤的故事&#xff0c;但自己看来&#xff0c;其实更是一个人遭遇创伤后&#xff0c;如何自己走出来的过程&#xff0c;尤其重大精神创伤。另外作为本部电影的主角&#xff0c;另一个身份是律师&#xff0c;…

编译成功!QT/6.7.2/Creator编译Windows64 MySQL驱动(MSVC版)

相邻你找了很多博文&#xff0c;都没有办法。现在终于找到了正宗。 参考 GitHub - thecodemonkey86/qt_mysql_driver: Typical symptom: QMYSQL driver not loaded. Solution: get pre-built Qt SQL driver plug-in required to establish a connection to MySQL / MariaDB u…

小红书本地生活,要生活还是生意?

8月&#xff0c;沉寂许久的小红书本地生活突然动作频频。8月23日&#xff0c;小红书新增本地生活服务商管理规范和入驻规则&#xff0c;10天后正式宣布开放全国49座城市的餐饮团购类目&#xff0c;并将技术服务费从0.6%最新调整至2.6%&#xff0c;49城餐饮商家自此打通门店团购…

python开发子域名扫描器

python开发子域名扫描器 1. 前言2. 子域名扫描器开发2.1. 第一阶段2.2. 第二阶段2.3. 第三阶段2.4. 第四阶段 3. 总结 1. 前言 不想对内容解释过多了&#xff0c;自行去百度搜索相关的参数怎么使用的吧。对于编写工具基本上用到的无非就是多线程、请求等等这些&#xff0c;很多…

【Elasticsearch】-spring boot 依赖包冲突问题

<dependency><groupId>org.elasticsearch</groupId><artifactId>elasticsearch</artifactId><version>7.17.24</version></dependency> 在pom的配置中&#xff0c;只引入了elasticsearch-7.17.24 &#xff0c;但实际上会同时…

android编译make详细过程日志查看showcommands/verbose.log

背景&#xff1a; 平时做aosp开发时候&#xff0c;如果要编译某一个模块就会直接使用命令make&#xff0c;或者make xxx模块。 比如&#xff1a; make SettingsProvider make SystemUI make bootanimation这样就直接有对应的apk&#xff0c;或者bin文件了&#xff0c;具体这些…