0

我正在使用 PageImpl 类将页面转换为新页面,但 totalpages 属性的默认值为 0。我想将 totalPages 设置为特定数字。有可能改变它吗?

代码

    public Page<InformationDTO> getInformationById(String classId, int page, int size) {
        Pageable pageable = PageRequest.of(page, size);
        List<Information> InformationList = informationRepository.findByClassIdOrderByCreateDateDesc(classId, pageable).getContent();
        List<InformationDTO> InformationDTOList = new ArrayList<>();
        if(!InformationList.isEmpty()){
            for (Information information: informationList){
                informationDTOList.add(new InformationDTO(information));
            }
        }
        return new PageImpl<InformationDTO>(informationDTOList,pageable,informationList.size());
    }
4

2 回答 2

0

要获得一个页面作为答案,您需要更改代码中的一行

// Earlier
List<Information> InformationList = informationRepository.findByClassIdOrderByCreateDateDesc(classId, pageable).getContent();

// Changed line
Page<Information> InformationList = informationRepository.findByClassIdOrderByCreateDateDesc(classId, pageable);


// Then you will not be required to explicitly change into pageable
PageImpl<InformationDTO>(informationDTOList,pageable,informationList.size());

案例 1 查找最大页数

InformationList.getTotalPages()

案例 2 - 您的场景 - 来自集合对象 如果想要分页数据,那么您必须从 PageImpl 类中获得帮助。

它提供了 2 个构造函数来执行此操作

PageImpl(List<T> content, Pageable pageable, long total)

在哪里

  1. 内容 - 此页面的内容(您的收藏对象)。
  2. pageable – 分页信息
  3. total – 可用项目的总量。

还有另一个构造函数

PageImpl(List<T> content)

注意 - 这将导致创建的页面与整个列表相同。

于 2021-07-16T19:47:07.097 回答
0

尝试这个:-

int totalPages = InformationList.getTotalPages();
于 2021-07-17T04:44:00.133 回答