SpringBoot之使用PageHelper插件

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

    
    
      com.github.pagehelper
      pagehelper-spring-boot-starter
      1.1.2
    
    
         com.github.pagehelper
        pagehelper
        5.1.2
    
    
        com.github.pagehelper
        pagehelper-spring-boot-autoconfigure
        1.2.3
    

二、在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 shopCheckRecords = shopCheckRecordMapper.selectList(shopCheckRecordDto);
        if (shopCheckRecordDto.getPageIndex() != null) {
            //PageInfo
            PageInfo pageInfo = new PageInfo<>(shopCheckRecords);
            //根据pageInfo来获取总数目
            resultData.addData("total", pageInfo.getTotal());
        }
        resultData.addData("data", shopCheckRecords);
        return resultData;
    }

 

你可能感兴趣的:(JavaEE专区)