欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

SpringBoot之使用PageHelper插件

程序员文章站 2022-06-18 23:18:19
...

一、在pom.xml中引入PageHelper插件的依赖

    <!-- 分页插件 -->
    <dependency>
      <groupId>com.github.pagehelper</groupId>
      <artifactId>pagehelper-spring-boot-starter</artifactId>
      <version>1.1.2</version>
    </dependency>
    <dependency>
         <groupId>com.github.pagehelper</groupId>
        <artifactId>pagehelper</artifactId>
        <version>5.1.2</version>
    </dependency>
    <dependency>
        <groupId>com.github.pagehelper</groupId>
        <artifactId>pagehelper-spring-boot-autoconfigure</artifactId>
        <version>1.2.3</version>
    </dependency>

二、在application.properties中配置PageHelper的参数

#pagehelper配置
pagehelper.helperDialect: mysql
pagehelper.reasonable: true
pagehelper.supportMethodsArguments: true
pagehelper.params: count=countSql

三、使用PageHelper

//Service层
    @Override
    public ResultData list(ShopCheckRecordDto shopCheckRecordDto) {
        ResultData resultData = new ResultData(true, "查询成功");
        //pageIndex:页数 pageSize:每页最大数量
        if (null != shopCheckRecordDto.getPageIndex() && null != shopCheckRecordDto.getPageSize()) {
            PageHelper.startPage(shopCheckRecordDto.getPageIndex(), shopCheckRecordDto.getPageSize());
        }
        List<ShopCheckRecordDto> shopCheckRecords = shopCheckRecordMapper.selectList(shopCheckRecordDto);
        if (shopCheckRecordDto.getPageIndex() != null) {
            //PageInfo
            PageInfo<ShopCheckRecordDto> pageInfo = new PageInfo<>(shopCheckRecords);
            //根据pageInfo来获取总数目
            resultData.addData("total", pageInfo.getTotal());
        }
        resultData.addData("data", shopCheckRecords);
        return resultData;
    }

 

相关标签: PageHelper