springboot 之分页插件pageHelper的使用

版权声明:欢迎转载,转载请标明转载地址 https://blog.csdn.net/u010520146/article/details/86589447

一.前言

pageHelper的诞生,让你无需你自己去封装以及关心sql分页等问题,使用很方便,获取取数据也是非常方便。
源码地址:
https://github.com/pagehelper/Mybatis-PageHelper
中文文档:
https://apidoc.gitee.com/free/Mybatis_PageHelper/

二.开始使用

本例使用的springboot版本是2.1.1

1. poml包加入
   	<dependency>
   		<groupId>com.github.pagehelper</groupId>
   		<artifactId>pagehelper-spring-boot-starter</artifactId>
   		<version>1.2.3</version>
   	</dependency>
2.dao层获取数据

dao层接口

public interface PagesDaoMapper {
    List<pages> list();
}

mybatis获取数据

<?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.example.mybatispaging.dao.PagesDaoMapper">
    <select id="list"   resultType="com.example.mybatispaging.model.pages">
        select *  from `pages`
    </select>
</mapper>
3.application.yml配置
pagehelper:
    helperDialect: mysql
    reasonable: false
    supportMethodsArguments: true
    params: count=countSql
    offset-as-page-num: true
    row-bounds-with-count: true
解释:

helperDialect:
分页插件会自动检测当前的数据库链接,自动选择合适的分页方式。 你可以配置helperDialect属性来指定分页插件使用哪种方言。配置时,可以使用下面的缩写值:
oracle,mysql,mariadb,sqlite,hsqldb,postgresql,db2,sqlserver,informix,h2,sqlserver2012,derby
reasonable:
分页合理化参数,默认值为false。当该参数设置为 true 时,pageNum<=0 时会查询第一页,pageNum>pages(超过总数时),会查询最后一页。默认false 时,直接根据参数进行查询。
supportMethodsArguments:
支持通过 Mapper 接口参数来传递分页参数,默认值false,分页插件会从查询方法的参数值中,自动根据上面 params 配置的字段中取值,查找到合适的值时就会自动分页。 使用方法可以参考测试代码中的com.github.pagehelper.test.basic 包下的 ArgumentsMapTest 和 ArgumentsObjTest。
params:
为了支持startPage(Object params)方法,增加了该参数来配置参数映射,用于从对象中根据属性名取值, 可以配置 pageNum,pageSize,count,pageSizeZero,reasonable,不配置映射的用默认值, 默认值为pageNum=pageNum;pageSize=pageSize;count=countSql;reasonable=reasonable;pageSizeZero=pageSizeZero。
offset-as-page-num:
默认值为 false,该参数对使用 RowBounds 作为分页参数时有效。 当该参数设置为 true 时,会将 RowBounds 中的 offset 参数当成 pageNum 使用,可以用页码和页面大小两个参数进行分页。
row-bounds-with-count:
默认值为false,该参数对使用 RowBounds 作为分页参数时有效。 当该参数设置为true时,使用 RowBounds 分页会进行 count 查询。

4.controller测试
    @Resource
    PagesDaoMapper pagesDaoMapper;

    @GetMapping("/list")
    public TResult pageList(Integer currentPage, Integer pageSize){
        // 必须在mapper接口中的方法执行之前设置该分页信息
        currentPage = currentPage == null || currentPage<= 0 ? 1:currentPage;
        pageSize = pageSize == null || pageSize<= 0 ? 10:pageSize;
        //执行分页操作
        PageHelper.startPage(currentPage, pageSize);
        //查询数据库数据
        List<pages> list = pagesDaoMapper.list();//全部商品
        return new TResult().setList(list);
    }
}
5.结果测试

表pages里面总的有32条数据
1.访问 http://localhost:8013/list?currentPage=1&pageSize=10 ,结果为

{“errorCode”:0,“model”:null,“list”:[{“id”:4,“name”:“名字1”,“description”:“描述1”},{“id”:5,“name”:“名字2”,“description”:“描述2”},{“id”:6,“name”:“名字3”,“description”:“描述3”},{“id”:7,“name”:“名字4”,“description”:“描述4”},{“id”:8,“name”:“名字5”,“description”:“描述5”},{“id”:9,“name”:“名字6”,“description”:“描述6”},{“id”:10,“name”:“名字7”,“description”:“描述7”},{“id”:11,“name”:“名字8”,“description”:“描述8”},{“id”:12,“name”:“名字9”,“description”:“描述9”},{“id”:13,“name”:“名字10”,“description”:“描述10”}]}

2.访问 http://localhost:8013/list?currentPage=4&pageSize=10 结果为:

{“errorCode”:0,“model”:null,“list”:[{“id”:34,“name”:“名字31”,“description”:“描述31”},{“id”:35,“name”:“名字32”,“description”:“描述32”}]}

3.访问 http://localhost:8013/list?currentPage=5&pageSize=10 结果为:

{“errorCode”:0,“model”:null,“list”:[]}

扫描二维码关注公众号,回复: 5052530 查看本文章

猜你喜欢

转载自blog.csdn.net/u010520146/article/details/86589447