我爱java系列---【springboot中分页插件pagehelper自定义返回结果类型】

1.引入依赖

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

2.在yml文件中添加配置

 可以省略,这个版本有默认值。

3.示例代码

返回结果处理工具类:

public class PageHelperVoHandler<T> {

    public PageResult<T> resultHandler(PageInfo<T> pageList, Integer currentPage, Integer pageSize) {
        try {
            //总记录数
            long total = pageList.getTotal();
            //判断:如果查询出来的结果为空,则直接返回null
            if (total <= 0) {
                return new PageResult<T>(StatusCode.OK, Msg.SELECTOK, total, new ArrayList<>());
            }
            //总页数
            long totalPages = total / pageSize;
            if (total % pageSize != 0) {
                totalPages++;
            }
            //判断:当前页大于总页数,返回null
            if (currentPage <= totalPages) {
                return new PageResult<T>(StatusCode.OK, Msg.SELECTOK, total, pageList.getList());
            } else {
                return new PageResult<T>(StatusCode.OK, "当前页的值不能大于总页数的值:" + totalPages, pageList.getTotal(), null);
            }
        } catch (Exception e) {
            throw new RuntimeException("分页结果处理异常");
        }
    }
}

实现类:

@Service
@Slf4j
@Transactional(rollbackFor = Exception.class)
public class  ProvinceService {
    @Autowired
    private ProvinceMapper provinceMapper;

    public PageInfo<Province> findPageByCondition(ProvinceQueryCondition queryCondition, int currentPage, int pageSize) {
        try {
            log.info("省份管理实现类-条件+分页查询-入参:queryCondition:{},currentPage:{},pageSize:{}", queryCondition, currentPage, pageSize);
            PageHelper.startPage(currentPage, pageSize);
            return PageInfo.of(provinceMapper.findPageByCondition(queryCondition));
        } catch (Exception e) {
            log.error(e.getMessage(), e);
            throw new RuntimeException();
        }
    }
}

controller控制层:

@CrossOrigin
@RequestMapping("/nide/provinceManage")
@Api(tags = "省份管理初级接口")
@RestController
@Slf4j
public class ProvinceController {
    @Autowired
    private ProvinceService provinceService;

    @ApiOperation("条件分页查询省份信息列表")
    @PostMapping("/findPageByCondition/{currentPage}/{pageSize}")
    public PageResult<Province> findPageByCondition(@RequestBody ProvinceQueryCondition queryCondition, @PathVariable int currentPage, @PathVariable int pageSize) {
        log.info("省份管理初级接口-条件分页查询-入参:queryCondition:{},currentPage:{},pageSize{}", queryCondition, currentPage, pageSize);
        try {
            PageInfo<Province> provinceList = provinceService.findPageByCondition(queryCondition, currentPage, pageSize);
            return new PageHelperVoHandler<Province>().resultHandler(provinceList, currentPage, pageSize);//调用上边的工具类
        } catch (Exception e) {
            log.error(e.getMessage(), e);
            return new PageResult<>(StatusCode.ERROR, Msg.SELECTERROR, 0L, null);
        }
    }
}
愿你走出半生,归来仍是少年!
原文地址:https://www.cnblogs.com/hujunwei/p/11977695.html