java 如何使用分页和 spring 数据 jpa 获取 findAll() 服务的所有记录?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/41892105/
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
How can I get all the records for findAll () service using pagination and spring data jpa?
提问by Aditya Gupta
How can I get all the records for findAll ()
service using pagination and Spring Data JPA
when we do not apply filters it should return all the records rather than displaying it pagewise.I have findAll (Pageable pageable)
service and calling it from custom repository. IS it possible to get all the records in one page only using pagination?
如何findAll ()
使用分页获取服务的所有记录,Spring Data JPA
当我们不应用过滤器时,它应该返回所有记录而不是按页显示。我有findAll (Pageable pageable)
服务并从自定义存储库调用它。是否可以仅使用分页获取一页中的所有记录?
回答by xenteros
public interface UserRepository extends PagingAndSortingRepository<User, Long> {
//Page<User> findAll(Pageable pageable); is already in this repository.
}
So just in case you want to find the first page of size 20 try:
因此,如果您想找到大小为 20 的第一页,请尝试:
Page<User> users = repository.findAll(new PageRequest(0, 20));
If what you want to do is to get all entities on a single page, it doesn't make much sense but can be done in two steps:
如果您想要做的是将所有实体都放在一个页面上,这没有多大意义,但可以分两步完成:
int count = repository.count();
Page<User> users = repository.findAll(new PageRequest(0, count));
count()
comes from CrudRepository
which is extended by PagingAndSortingRepository
.
count()
来自 由CrudRepository
扩展PagingAndSortingRepository
。
回答by Svetlana
Use CrudRepository
instead of PagingAndSortingRepository
if you don't need paging
如果不需要分页,请使用CrudRepository
代替PagingAndSortingRepository
回答by MNassar
If you are using PagingAndSortingRepository and still want a list of "things",
如果您正在使用 PagingAndSortingRepository 并且仍然想要“事物”列表,
You can add a method List<Thing> findBy()
你可以添加一个方法 List<Thing> findBy()
If you have a RepositoryRestResource, it will be exposed as REST: /things/search/findBy
如果您有一个 RepositoryRestResource,它将作为 REST 公开: /things/search/findBy