分页是常见大型项目都需要的一个功能,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 {
@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
方法设置了三个分页插件的属性:
"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
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>
五、第一种依赖展示结果
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
}
}