How to get all results in one page using Spring Da

2019-06-15 08:16发布

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.

4条回答
爷、活的狠高调
2楼-- · 2019-06-15 08:31

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

查看更多
不美不萌又怎样
3楼-- · 2019-06-15 08:38

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.

查看更多
干净又极端
4楼-- · 2019-06-15 08:42

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

Pageable p = null;
return customerRepository.findAll(p);
查看更多
叼着烟拽天下
5楼-- · 2019-06-15 08:45

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

Pageable wholePage = Pageable.unpaged();
return customerRepository.findAll(wholePage);
查看更多
登录 后发表回答