如何使用Spring Data Pagination在一个页面中获取所有结果

时间:2017-02-05 15:29:39

标签: java spring spring-data spring-data-jpa spring-repositories

我希望在单页中获得所有结果,我已尝试使用

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

上面没有用,有没有办法实现这个目的?似乎无法通过自定义查询来实现here

4 个答案:

答案 0 :(得分:25)

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

new PageRequest(0, Integer.MAX_VALUE);

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

答案 1 :(得分:7)

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

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

答案 2 :(得分:5)

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

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

答案 3 :(得分:2)

从spirng-data-commons@2.1.0起,正确的语法为PageRequest.of(0, Integer.MAX_VALUE)。 您可以查看here

相关问题