分页是常见大型项目都需要的一个功能,PageHelper是一个非常流行的MyBatis分页插件,它支持多数据库分页,无需修改SQL语句即可实现分页功能。
本文在最后展示了两种依赖验证的结果。
文章目录
一、第一种依赖方式
1、在项目中使用 PageHelper 插件需要先添加依赖:
<dependency><groupId>com.github.pagehelper</groupId><artifactId>pagehelper</artifactId><version>4.1.3</version></dependency>
2、这种方式需要配置一个 config 文件
packagecom.wen.config;importcom.github.pagehelper.PageHelper;importorg.springframework.context.annotation.Bean;importorg.springframework.context.annotation.Configuration;importjava.util.Properties;/**
* @author : rjw
* @date : 2024-09-20
*/@ConfigurationpublicclassMyBatisConfig{@BeanpublicPageHelperpageHelper(){PageHelper pageHelper =newPageHelper();Properties properties =newProperties();
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.properties
或
application.yml
。
pagehelper.helper-dialect=mysql // 数据库 可选
pagehelper.reasonable=true // 规整页码范围,应对负数或过大页码
pagehelper.support-methods-arguments=true // 规整可以通过方法参数获取,可用可不用输入即可
pagehelper.params=count=countSql
pagehelper:
helper-dialect: mysql
reasonable: true
support-methods-arguments: true
params: count=countSql
三、创建数据库表格
3、分页条件配置
pagehelper:
helper-dialect: mysql
reasonable: true // 规整页码范围
support-methods-arguments: true // 规整方法参数获取
四、代码示例
关于统一 API 响应结果封装,代码示例在 SpringBoot 项目统一 API 响应结果封装 。
关于 mybatis 的项目搭建在 SpringBoot 项目整合 MyBatis 框架 。
1、TestController
packagecom.wen.controller;importcom.wen.data.Result;importcom.wen.data.ResultGenerator;importcom.wen.dto.TbUser;importcom.wen.service.TestService;importorg.apache.ibatis.annotations.Param;importorg.springframework.beans.factory.annotation.Autowired;importorg.springframework.web.bind.annotation.GetMapping;importorg.springframework.web.bind.annotation.RequestMapping;importorg.springframework.web.bind.annotation.RestController;@RestController@RequestMapping("/test")publicclassTestController{@AutowiredprivateTestService testService;@GetMapping("/select")publicResult<?>selectUserByPage(@Param("pageSize")Integer pageSize,@Param("pageNumber")Integer pageNumber){returnResultGenerator.genSuccessResult(testService.selectUserByPage(pageSize, pageNumber));}}
2、TestService
packagecom.wen.service;importcom.github.pagehelper.PageInfo;importcom.wen.dto.TbUser;publicinterfaceTestService{PageInfo<TbUser>selectUserByPage(Integer pageSize,Integer pageNumber);}
3、TestServiceImpl
packagecom.wen.service.impl;importcom.github.pagehelper.PageHelper;importcom.github.pagehelper.PageInfo;importcom.wen.dto.TbUser;importcom.wen.mapper.TbUserMapper;importcom.wen.service.TestService;importorg.springframework.beans.factory.annotation.Autowired;importorg.springframework.stereotype.Service;importjava.util.List;@ServicepublicclassTestServiceImplimplementsTestService{@AutowiredprivateTbUserMapper tbUserMapper;@OverridepublicPageInfo<TbUser>selectUserByPage(Integer pageSize,Integer pageNumber){// 这句代码要放在查询 mapper 语句的前面PageHelper.startPage(pageNumber, pageSize);List<TbUser> tbUsers = tbUserMapper.selectUser();PageInfo<TbUser> tbUserPageInfo =newPageInfo<>(tbUsers);return tbUserPageInfo;}}
4、TbUserMapper
packagecom.wen.mapper;importcom.wen.dto.TbUser;importorg.apache.ibatis.annotations.Mapper;importjava.util.List;@MapperpublicinterfaceTbUserMapper{List<TbUser>selectUser();}
5、TbUserMapper.xml
<?xml version="1.0" encoding="UTF-8"?><!DOCTYPEmapperPUBLIC"-//mybatis.org//DTD Mapper 3.0//EN""http://mybatis.org/dtd/mybatis-3-mapper.dtd"><mappernamespace="com.wen.mapper.TbUserMapper"><selectid="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}}
版权归原作者 青灯文案 所有, 如有侵权,请联系我们删除。