SpringBoot2.0系列教程(七)Springboot框架添加PageHelper分页查询功能

Hello大家好,本章我们添加PageHelper分页查询功能。另求各路大神指点,感谢

一:什么是PageHelper

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

物理分页

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

支持多种分页方式

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

二:添加PageHelper依赖

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

然后鼠标右键选择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 id="selectAll" resultMap="BaseResultMap">
    select
    <include refid="Base_Column_List"/>
    from user_info
</select>

UserInfoMapper

List<UserInfo> selectAll();

UserInfoService

PageInfo<UserInfo> selectAll(Integer page,Integer size);

UserInfoServiceImpl

@Override
public PageInfo<UserInfo> selectAll(Integer page, Integer size) {
    //开启分页查询,写在查询语句上方
    //只有紧跟在PageHelper.startPage方法后的第一个Mybatis的查询(Select)方法会被分页。
    PageHelper.startPage(page, size);
    List<UserInfo> userInfoList = userInfoMapper.selectAll();
    PageInfo<UserInfo> 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<PageInfo<UserInfo>> selectAll(@RequestParam(defaultValue = "0") Integer page,
                                      @RequestParam(defaultValue = "0") Integer size) {
    PageInfo<UserInfo> 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<T> 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://gitee.com/beany/mySpringBoot

GitHub地址: https://github.com/MyBeany/mySpringBoot

写文章不易,如对您有帮助,请帮忙点下star

结尾

添加PageHelper分页查询功能已完成,后续功能接下来陆续更新,另求各路大神指点,感谢大家。

posted @ 2019-04-03 16:19  mr_初晨  阅读(1241)  评论(0编辑  收藏  举报