使用PageHelper分页的问题

      今天遇到这样个问题,利用PageHelper做分页,结果查出来的pagesize和total一样,这样前端总会查到一页,这与结果不符。
源码如下:

        PageHelper.startPage(pageNum, pageSize,true);
        List<StudentScoreCollectEntity> studentScoreEntities = studentScoreCollectDao.selectByExample(studentScoreCollectExample);
        if (CollectionUtils.isEmpty(studentScoreEntities)) {

            logger.error("调用本服务查询学生补考成绩为空");
            return new PageInfo<>();
        }
        List<String> studentIdList = new ArrayList<>();
        for (StudentScoreCollectEntity studentScoreEntity : studentScoreEntities) {
            String studentId = studentScoreEntity.getStudentId();
            studentIdList.add(studentId);
        }

        List<StudentModel> studentModels = studentFacade.queryStudentBasicInfoByIds(studentIdList);
        if (CollectionUtils.isEmpty(studentModels)) {

            logger.error("调用基础服务查询学生信息为空");
            return new PageInfo<>();
        }

        List<FailExamStudentModel> failExamStudentModels = publicMethod(studentScoreEntities, studentModels);

        PageInfo<FailExamStudentModel> pageInfo = new PageInfo<>(failExamStudentModels);

      查两个接口,然后两个返回值通过publicMethod()拼接在一起,再对该返回值做分页转换。

      走断点调试发现,可能和PageHelper.startPage(pageNum, pageSize,true);放的位置有关,上网查了查,都说这句要放在查询结果list之前,它只对紧接着操作数据库的代码起作用。
      于是试着将PageHelper.startPage(pageNum, pageSize,true);移动位置,结果查的还是不对。
      最后试了一种方法,赋值。
      前两个接口返回值条数一样多,干脆就将total赋值。代码修改如下:

        PageHelper.startPage(pageNum, pageSize,true);
        List<StudentScoreCollectEntity> studentScoreEntities = studentScoreCollectDao.selectByExample(studentScoreCollectExample);
        PageInfo<StudentScoreCollectEntity> sscPage = new PageInfo<>(studentScoreEntities);
        if (CollectionUtils.isEmpty(studentScoreEntities)) {

            logger.error("调用本服务查询学生补考成绩为空");
            return new PageInfo<>();
        }
        List<String> studentIdList = new ArrayList<>();
        for (StudentScoreCollectEntity studentScoreEntity : studentScoreEntities) {
            String studentId = studentScoreEntity.getStudentId();
            studentIdList.add(studentId);
        }

        List<StudentModel> studentModels = studentFacade.queryStudentBasicInfoByIds(studentIdList);
        if (CollectionUtils.isEmpty(studentModels)) {

            logger.error("调用基础服务查询学生信息为空");
            return new PageInfo<>();
        }

        List<FailExamStudentModel> failExamStudentModels = publicMethod(studentScoreEntities, studentModels);

        PageInfo<FailExamStudentModel> pageInfo = new PageInfo<>(failExamStudentModels);
        //赋值
        pageInfo.setPageNum(pageNum);
        pageInfo.setPageSize(pageSize);
        pageInfo.setTotal(sscPage.getTotal());

      暂时没想出其他更好的办法,如果您有不同建议,欢迎提出!

感谢您的阅读!

你可能感兴趣的:(【javaweb】)