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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/42053786/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-11 23:53:23  来源:igfitidea点击:

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

javaspringspring-dataspring-data-jpaspring-repositories

提问by Ruwanka Madhushan

I want to get all the results in single page, I've tried with

我想在单页中获得所有结果,我已经尝试过

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

Above is not working, is there any methods to achieve this? Seems like it cannot be achieved from custom query as asked here.

以上不起作用,有什么方法可以实现吗?似乎无法从此处询问的自定义查询中实现。

采纳答案by Branislav Lazic

Your page request is incorrect because you are looking for results at the wrong page. It should be:

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

new PageRequest(0, Integer.MAX_VALUE);

First page for results is 0. Since you're returning all records, they are all on this page.

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

回答by Karthik Bose

If you pass null for Pageable, Spring will ignore it and brings all data.

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

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

回答by radbrawler

As of [email protected] correct syntax is PageRequest.of(0, Integer.MAX_VALUE). You can look here

[email protected] 开始,正确的语法是PageRequest.of(0, Integer.MAX_VALUE). 你可以看这里

回答by chekmare

The more correct way is to use Pageable.unpaged()

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

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