如果我们不应用过滤器,我怎么能使用分页和Spring Data JPA获取findAll()服务的所有记录呢?它应该返回所有记录而不是逐页显示它.我有findAll(Pageable pageable)服务并从自定义存储库中调用它.是否可以仅使用分页在一个页面中获取所有记录?
解决方法:
public interface UserRepository extends PagingAndSortingRepository {
//Page findAll(Pageable pageable); is already in this repository.
}
所以,如果你想找到第20页的第一页,请尝试:
Page users = repository.findAll(new PageRequest(0, 20));
如果您想要做的是将所有实体放在一个页面上,那么它没有多大意义,但可以分两步完成:
int count = repository.count();
Page users = repository.findAll(new PageRequest(0, count));
count()来自CrudRepository,它由PagingAndSortingRepository扩展.
标签:java,spring,spring-data-jpa
来源: https://codeday.me/bug/20190608/1197918.html