springboot使用Pagehelper进行分页查询,total总是为1

第一步:导入jar包依赖

<!-- 分页助手启动器 -->
        <dependency>
            <groupId>com.github.pagehelper</groupId>
            <artifactId>pagehelper-spring-boot-starter</artifactId>
            <version>1.2.5</version>
        </dependency>

第二步:
在controller层写分页功能

//必须在分页查询语句之前,pageNum:当前页,pageSize:页面显示条数
		PageHelper.startPage(pageNum,pageSize);
		//查询语句
        List<SmartPerson> personList = smartEyesService.smartEyesPesonList(type, companyCode);
        //将查询结果封装进pageInfo
        PageInfo<SmartPerson> pageInfo = new PageInfo<>(personList);

今天遇到了一个大坑

在使用springboot整合pageHelper插件进行分页的时候,按照上述步骤应该是没有任何问题的,然而我这边出现了一个问题,就是total总是为1。
起初我还以为是我的包导入的有问题,我试了几种网上博客提到的包,也无济于事。后来我无意中在一篇博客中看到,原来pageHelper在统计total的时候,会在查询语句执行之前进行一次;count(0)查询,即查询总条数
查询total
接着才会处理查询语句(用于分页的主体),
查询分页内容
这个插件会在我们的查询语句的后面智能追加上limit ?,?这样的查询条件,用于分页的实现。
我思考了一下,发现我在service层的查询方法中,对于查询的结果进行了一次解析和封装,并且我方法的返回值是我自定义的一个list,也就是说,在controller层调用这个方法的时候,实际上是得到了我处理过的值,而不是直接查询出来的结果。这也就是为什么我在controller层进行分页的时候,total总为1 的缘故。

public List<SmartPerson> smartEyesPesonList(int type, String companyCode) {
        List<SmartEyes> list = smartEyesMapper.smartEyesPesonList(type, companyCode);
        List<SmartPerson> smartPersonList = new ArrayList<>();
        for (SmartEyes smartEyes : list) {
//            HashMap<String,Object> map = new HashMap<>();
            String jsonBody = smartEyes.getData();
            String arrayBody = JSONObject.parseObject(jsonBody).getJSONArray("message").get(0).toString();
            if (arrayBody != null && !arrayBody.isEmpty()) {
//                String innerBody = gson.toJson(arrayBody.substring(1,arrayBody.length()-1).replaceAll(" ",""));
//                String innerBody = arrayBody.substring(1,arrayBody.length()-1).replaceAll(" ","");
                SmartPerson smartPerson = gson.fromJson(arrayBody, SmartPerson.class);
                smartPerson.setType(getType(type));
                if (smartEyes.getGmtUpdate() == null) {
                    smartPerson.setState("未处理");
                    smartPerson.setUseTime("0天");
                } else {
                    smartPerson.setState("已处理");
                    smartPerson.setUseTime(getTime(smartEyes.getGmtUpdate(), smartEyes.getGmtCreate()));
                }
                smartPersonList.add(smartPerson);
            }

        }
        return smartPersonList;
    }

接下来我对service层的方法进行了分页处理

public PageInfo smartEyesPersonList(int type, String companyCode, int pageNum, int pageSize) {
        //对查询信息进行分页
        PageHelper.startPage(pageNum, pageSize);
        List<SmartEyes> list = smartEyesMapper.smartEyesPesonList(type, companyCode);
        PageInfo<SmartPerson> pageInfo = new PageInfo(list);
        //对人员信息进行处理
        List<SmartPerson> smartPersonList = new ArrayList<>();
        for (SmartEyes smartEyes : list) {
//            HashMap<String,Object> map = new HashMap<>();
            String jsonBody = smartEyes.getData();
            String arrayBody = JSONObject.parseObject(jsonBody).getJSONArray("message").get(0).toString();
            if (arrayBody != null && !arrayBody.isEmpty()) {
//                String innerBody = gson.toJson(arrayBody.substring(1,arrayBody.length()-1).replaceAll(" ",""));
//                String innerBody = arrayBody.substring(1,arrayBody.length()-1).replaceAll(" ","");
                SmartPerson smartPerson = gson.fromJson(arrayBody, SmartPerson.class);
                smartPerson.setType(getType(type));
                if (smartEyes.getGmtUpdate() == null) {
                    smartPerson.setState("未处理");
                    smartPerson.setUseTime("0天");
                } else {
                    smartPerson.setState("已处理");
                    smartPerson.setUseTime(getTime(smartEyes.getGmtUpdate(), smartEyes.getGmtCreate()));
                }
                smartPersonList.add(smartPerson);
            }
        }
        //将人员信息封装进pageInfo
        pageInfo.setList(smartPersonList);
        return pageInfo;
    }

这样处理的原因:
我本来需要取出SmartEyes中的几个字段进行解析,我取出了数据库中所有的SmartEyes数据,然后进行解析并将解析出来的结果封装为smartPerson对象,将smartPerson组成的smartPersonList作为返回值。
在添加分页功能的时候,首先将查询出来的SmartEyes结果集进行分页,在分页的时候,PageInfo<SmartPerson> pageInfo = new PageInfo(list);起到了决定性的作用,它使得我能够将我解析封装好的结果集smartPersonList塞进pageInfo 体中通pageInfo.setList(smartPersonList);完成最后的操作,并返回PageInfo 对象。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值