PageHelper 解析及实现原理


一、PageHelper介绍

面向关系型数据库的 SQL 查询和数据导出时,如果数据条数非常大,直接将所有数据一次性查出或者导出显然是不可行的。这时候就需要进行分页查询或分页导出,将查询或导出的数据按照指定大小分页加载或写入,从而提高查询或导出的效率。而分页查询或分页导出的实现过程比较繁琐,需要考虑很多细节问题,容易出错。因此,出现了一些支持分页查询或分页导出的插件或工具类,例如 MyBatis-Plus 的分页插件 PageHelper。
PageHelper是Mybatis-Plus中的一个插件,主要用于实现数据库的分页查询功能。其核心原理是将传入的页码和条数赋值给一个Page对象,并保存到本地线程ThreadLocal中。接下来,PageHelper会进入Mybatis的拦截器环节,在拦截器中获取并处理刚才保存在ThreadLocal中的分页参数。这些分页参数会与原本的SQL语句和内部已经定义好的SQL进行拼接,从而完成带有分页处理的SQL语句的构建。

二、PageHelper代码实现

1.导入插件

pom.xml中导入依赖

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

在springboot配置文件(application.yml)中声明插件

#pagehelper分页插件配置
pagehelper:
  helperDialect: mysql
  reasonable: true
  supportMethodsArguments: true
  params: count=countSql

2.业务代码

举例说明:有一个员工表,里面有数据若干,将员工数据全部查询并分页显示
Controller层代码(此处返回的是一个封装好的Result和PageResult类后续给出源码)

    @GetMapping("/page")
    @ApiOperation("员工分页查询")
    public Result<PageResult> page(EmployeePageQueryDTO employeePageQueryDTO){
        log.info("员工分页查询,参数为{}",employeePageQueryDTO);
        PageResult pageResult = employeeService.pageQuery(employeePageQueryDTO);
        return Result.success(pageResult);
    }

ServiceImpl:employeePageQueryDTO.getPage(),employeePageQueryDTO.getPageSize()分别是,要查询第几页、每页有几条数据

    @Override
    public PageResult pageQuery(EmployeePageQueryDTO employeePageQueryDTO) {
        //分页查询使用pagehelper插件,pom引入
        //开始分页查询
        PageHelper.startPage(employeePageQueryDTO.getPage(),employeePageQueryDTO.getPageSize());
        Page<Employee> page = employeeMapper.pageQuery(employeePageQueryDTO);
        long total = page.getTotal();
        List<Employee> records = page.getResult();
        return new PageResult(total,records);
    }

Mapper层:

    <select id="pageQuery" resultType="com.sky.entity.Employee">
        select * from employee
        <where>
            <if test="name != null and name != ''">
                and name like concat('%',#{name},'%')
            </if>
        </where>
        order by create_time asc
    </select>

辅助理解的代码 Result类和PageResult类

public class PageResult implements Serializable {

    private long total; //总记录数

    private List records; //当前页数据集合

}

@Data
public class Result<T> implements Serializable {

    private Integer code; //编码:1成功,0和其它数字为失败
    private String msg; //错误信息
    private T data; //数据

    public static <T> Result<T> success() {
        Result<T> result = new Result<T>();
        result.code = 1;
        return result;
    }

    public static <T> Result<T> success(T object) {
        Result<T> result = new Result<T>();
        result.data = object;
        result.code = 1;
        return result;
    }

    public static <T> Result<T> error(String msg) {
        Result result = new Result();
        result.msg = msg;
        result.code = 0;
        return result;
    }

}

三、PageHelper实现原理

核心原理是将传入的页码和条数赋值给一个Page对象,并保存到本地线程ThreadLocal中。接下来,PageHelper会进入Mybatis的拦截器环节,在拦截器中获取并处理刚才保存在ThreadLocal中的分页参数。这些分页参数会与原本的SQL语句和内部已经定义好的SQL进行拼接,从而完成带有分页处理的SQL语句的构建。
在这里插入图片描述
在这里插入图片描述


如有问题,欢迎联系博主

相关推荐

  1. PageHelper实现分页查询

    2024-07-18 15:48:01       36 阅读
  2. MyBatis-PageHelper 源码解说

    2024-07-18 15:48:01       29 阅读

最近更新

  1. docker php8.1+nginx base 镜像 dockerfile 配置

    2024-07-18 15:48:01       70 阅读
  2. Could not load dynamic library ‘cudart64_100.dll‘

    2024-07-18 15:48:01       74 阅读
  3. 在Django里面运行非项目文件

    2024-07-18 15:48:01       62 阅读
  4. Python语言-面向对象

    2024-07-18 15:48:01       72 阅读

热门阅读

  1. 力扣第十六题——最接近的三数之和

    2024-07-18 15:48:01       23 阅读
  2. 【Android】基础—常用控件、Intent

    2024-07-18 15:48:01       20 阅读
  3. C#中错误与异常处理

    2024-07-18 15:48:01       23 阅读
  4. zookeeper内部原理 (进阶介绍 三)

    2024-07-18 15:48:01       19 阅读
  5. Leetcode 14. 最长公共前缀

    2024-07-18 15:48:01       26 阅读
  6. Linux常用命令(简要总结)

    2024-07-18 15:48:01       20 阅读
  7. 《人生苦短,我用python·十》python的异常使用

    2024-07-18 15:48:01       21 阅读
  8. python open 文件名过长报错No such file or directory

    2024-07-18 15:48:01       17 阅读
  9. 异步编程 进程 线程 协程(补充

    2024-07-18 15:48:01       22 阅读
  10. 【学习笔记】lambda表达式

    2024-07-18 15:48:01       22 阅读
  11. NVIDIA-TensorRT-Python推理

    2024-07-18 15:48:01       21 阅读