我爱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);
        }
    }
}
复制代码

posted on   少年攻城狮  阅读(3519)  评论(0编辑  收藏  举报

编辑推荐:
· AI与.NET技术实操系列(二):开始使用ML.NET
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
· 理解Rust引用及其生命周期标识(上)
· 浏览器原生「磁吸」效果!Anchor Positioning 锚点定位神器解析
阅读排行:
· DeepSeek 开源周回顾「GitHub 热点速览」
· 物流快递公司核心技术能力-地址解析分单基础技术分享
· .NET 10首个预览版发布:重大改进与新特性概览!
· AI与.NET技术实操系列(二):开始使用ML.NET
· 单线程的Redis速度为什么快?
< 2025年3月 >
23 24 25 26 27 28 1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28 29
30 31 1 2 3 4 5

导航

统计

点击右上角即可分享
微信分享提示