boot集成mybatis分页插件pagehelper

dongxurr 2020-01-29

导入依赖

<!-- https://mvnrepository.com/artifact/com.github.pagehelper/pagehelper-spring-boot-starter -->
<dependency>
    <groupId>com.github.pagehelper</groupId>
    <artifactId>pagehelper-spring-boot-starter</artifactId>
    <version>1.2.12</version>
</dependency>

application.yml配置

pagehelper:
  # dialect: ①
  # 分页插件会自动检测当前的数据库链接,自动选择合适的分页方式(可以不设置)
  helper-dialect: mysql
  # 上面数据库设置后,下面的设置为true不会改变上面的结果(默认为true)
  auto-dialect: true
  page-size-zero: false # ②
  reasonable: true # ③
  # 默认值为 false,该参数对使用 RowBounds 作为分页参数时有效。(一般用不着)
  offset-as-page-num: false
  # 默认值为 false,RowBounds是否进行count查询(一般用不着)
  row-bounds-with-count: false
  #params: ④
  #support-methods-arguments: 和params配合使用,具体可以看下面的讲解
  # 默认值为 false。设置为 true 时,允许在运行时根据多数据源自动识别对应方言的分页
  auto-runtime-dialect: false # ⑤
  # 与auto-runtime-dialect配合使用
  close-conn: true
  # 用于控制默认不带 count 查询的方法中,是否执行 count 查询,这里设置为true后,total会为-1
  default-count: false
  #dialect-alias: ⑥

在serviceImpl中使用

@Override
public PageInfo queryFollowmeBasicInfo(int pageNum, int pageSize) {
    PageHelper.startPage(pageNum, pageSize, true);
    List<Map<String, Object>> mapList = followMeMapper.queryFollowmeBasicInfo();
    mapList.forEach(map -> {
        String equity = map.get("equityData").toString();
        JSONArray array = JSON.parseArray(equity);
        List<Map> list = JSON.parseArray(array.toJSONString(), Map.class);
        List<Map<String, Object>> listE = MapConvert.listMapConvert(list);
        map.put("equityData", listE);
        String deposit = map.get("depositWithdraw").toString();
        JSONArray array1 = JSON.parseArray(deposit);
        List<Map> list1 = JSON.parseArray(array1.toJSONString(), Map.class);
        List<Map<String, Object>> listD = MapConvert.listMapConvert(list1);
        map.put("depositWithdraw", listD);
    });
    PageInfo pageInfo = new PageInfo(mapList);
    return pageInfo;
}

详解

首先使用:PageHelper.startPage(pageNum, pageSize, true);
一定要带true,此时会有总条数,总页数等信息

pageInfo信息

{
    "pageInfo": {
        //总条数
        "total": 3,
        //返回的数据
        "list": [{
            "id": 1,
            "versionCode": 1,
            "versionName": "1",
            "versionMsg": "1",
            "apkName": "1",
            "downUrl": "1",
            "createDate": "2019-03-19T01:19:12.000+0000",
            "ifWork": 1,
            "ifDelete": 0,
            "ifForce": 0,
            "memo": "111",
            "phoneType": 1
        }, {
            "id": 2,
            "versionCode": 2,
            "versionName": "2",
            "versionMsg": "2",
            "apkName": "2",
            "downUrl": "2",
            "createDate": "2019-03-19T01:19:50.000+0000",
            "ifWork": 1,
            "ifDelete": 0,
            "ifForce": 0,
            "memo": "222",
            "phoneType": 2
        }],
        //页码
        "pageNum": 1,
        //每页数量
        "pageSize": 2,
        "size": 2,
        "startRow": 1,
        "endRow": 2,
        //总页数
        "pages": 2,
        "prePage": 0,
        "nextPage": 2,
        //是否第一页
        "isFirstPage": true,
        //是否最后一页
        "isLastPage": false,
        //前面是否还有页
        "hasPreviousPage": false,
        //是否还有下一页
        "hasNextPage": true,
        "navigatePages": 8,
        "navigatepageNums": [1, 2],
        "navigateFirstPage": 1,
        "navigateLastPage": 2
    }
}

参考

https://pagehelper.github.io/docs/howtouse/ (官方文档)

https://blog.csdn.net/shuaigg001/article/details/94385572 (主参考)

https://blog.csdn.net/J080624/article/details/88743766 (分析)

相关推荐