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

为君一笑 提交于 2019-12-08 14:50:30

问题


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.


回答1:


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.




回答2:


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

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



回答3:


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

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



回答4:


As of spirng-data-commons@2.1.0 correct syntax is PageRequest.of(0, Integer.MAX_VALUE). You can look here



来源:https://stackoverflow.com/questions/42053786/how-to-get-all-results-in-one-page-using-spring-data-pagination

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!