如何使用 Spring 数据分页在一页中获取所有结果

How to get all results in one page using Spring Data Pagination

我想在单页中获取所有结果,我试过

Pageable p = new PageRequest(1, Integer.MAX_VALUE);
return customerRepository.findAll(p);

以上是不行的,请问有什么方法可以实现吗?似乎无法通过自定义查询实现 here

您的页面请求不正确,因为您在错误的页面上查找结果。应该是:

PageRequest.of(0, Integer.MAX_VALUE);

结果的第一页是 0。由于您要返回所有记录,因此它们都在此页上。

如果您为 Pageable 传递 null,Spring 将忽略它并带来所有数据。

Pageable p = null;
return customerRepository.findAll(p);

从 spring-data-commons@2.1.0 开始,正确的语法是 PageRequest.of(0, Integer.MAX_VALUE)。 你可以看看here

更正确的方法是使用Pageable.unpaged()

Pageable wholePage = Pageable.unpaged();
return customerRepository.findAll(wholePage);