Springboot 框架添加 PageHelper 分页查询功能

一:什么是 PageHelper

PageHelper 是一款好用的开源免费的 Mybatis 第三方物理分页插件

物理分页

支持常见的 12 种数据库。Oracle,MySql,MariaDB,SQLite,DB2,PostgreSQL,SqlServer 等

支持多种分页方式

支持常见的 RowBounds(PageRowBounds),PageHelper.startPage 方法调用,Mapper 接口参数调用

二:添加 PageHelper 依赖


com.github.pagehelper pagehelper-spring-boot-starter 1.2.5

然后鼠标右键选择 Maven→Reimport 进行依赖下载

三:添加 PageHelper 配置

application.properties中添加

logging.level.com.example.demo.dao=DEBUG
pagehelper.helperDialect=mysql
pagehelper.reasonable=true
pagehelper.supportMethodsArguments=true
pagehelper.params=count=countSql
pagehelper.page-size-zero=true

四:使用方法

UserInfoMapper.xml


select from user_info

UserInfoMapper

List selectAll();

UserInfoService

PageInfo selectAll(Integer page,Integer size);

UserInfoServiceImpl

@Override
public PageInfo selectAll(Integer page, Integer size) {
    //开启分页查询,写在查询语句上方
    //只有紧跟在PageHelper.startPage方法后的第一个Mybatis的查询(Select)方法会被分页。
    PageHelper.startPage(page, size);
    List userInfoList = userInfoMapper.selectAll();
    PageInfo pageInfo = new PageInfo<>(userInfoList);
    return pageInfo;
}

UserInfoController

@ApiOperation(value = "查询用户", notes = "分页查询用户所有")
@ApiImplicitParams({
        @ApiImplicitParam(name = "page", value = "当前页码",
                dataType = "Integer", paramType = "query"),
        @ApiImplicitParam(name = "size", value = "每页显示条数",
                dataType = "Integer", paramType = "query")
})
@PostMapping("/selectAll")
public RetResult> selectAll(@RequestParam(defaultValue = "0") Integer page,
                                      @RequestParam(defaultValue = "0") Integer size) {
    PageInfo pageInfo = userInfoService.selectAll(page, size);
    return RetResponse.makeOKRsp(pageInfo);
}

五:PageHelper 中默认 PageInfo 的成员变量介绍

//当前页
private int pageNum;
//每页的数量
private int pageSize;
//当前页的数量
private int size;
//当前页面第一个元素在数据库中的行号
private int startRow;
//当前页面最后一个元素在数据库中的行号
private int endRow;
//总记录数
private long total;
//总页数
private int pages;
//结果集
private List list;
//第一页
private int firstPage;
//前一页
private int prePage;
//是否为第一页
private boolean isFirstPage;
//是否为最后一页
private boolean isLastPage;
//是否有前一页
private boolean hasPreviousPage;
//是否有下一页
private boolean hasNextPage;
//导航页码数
private int navigatePages;
//所有导航页号
private int[] navigatepageNums; 

六:功能测试

地址:http://192.168.1.104:8080/userInfo/selectAll

情况一:不传参数,默认为全查询

{
    "code": 200,
    "data": {
        "endRow": 2,
        "firstPage": 0,
        "hasNextPage": false,
        "hasPreviousPage": false,
        "isFirstPage": false,
        "isLastPage": true,
        "lastPage": 0,
        "list": [
            {
                "id": 1,
                "userName": "Mr_初晨"
            },
            {
                "id": 2,
                "userName": "Mr_初晨"
            }
        ],
        "navigateFirstPage": 0,
        "navigateLastPage": 0,
        "navigatePages": 8,
        "navigatepageNums": [],
        "nextPage": 0,
        "orderBy": "",
        "pageNum": 0,
        "pageSize": 0,
        "pages": 0,
        "prePage": 0,
        "size": 2,
        "startRow": 1,
        "total": 2
    },
    "msg": "success"
}

情况二:参数 page=2&size=1

{
    "code": 200,
    "data": {
        "endRow": 2,
        "firstPage": 1,
        "hasNextPage": false,
        "hasPreviousPage": true,
        "isFirstPage": false,
        "isLastPage": true,
        "lastPage": 2,
        "list": [
            {
                "id": 2,
                "userName": "Mr_初晨"
            }
        ],
        "navigateFirstPage": 1,
        "navigateLastPage": 2,
        "navigatePages": 8,
        "navigatepageNums": [
            1,
            2
        ],
        "nextPage": 0,
        "orderBy": "",
        "pageNum": 2,
        "pageSize": 1,
        "pages": 2,
        "prePage": 1,
        "size": 1,
        "startRow": 2,
        "total": 2
    },
    "msg": "success"
}

转载

链接:https://juejin.im/post/5adc90726fb9a07ab5087c7f