[英]How can I get all the records for findAll () service using pagination and spring data jpa?
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.当我们不应用过滤器时,如何使用分页和
Spring Data JPA
获取findAll ()
服务的所有记录,它应该返回所有记录而不是按页面显示。我有findAll (Pageable pageable)
服务并从自定义存储库调用它. IS it possible to get all the records in one page only using pagination?是否可以仅使用分页在一页中获取所有记录?
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
扩展。
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
如果您不需要分页,请使用CrudRepository
而不是PagingAndSortingRepository
Now PageRequest constructor is protected and you cannot create with 'new'.现在 PageRequest 构造函数受到保护,您不能使用“新”创建。 You have to call static method:
你必须调用静态方法:
PageRequest.of(int page, int size)
PageRequest.of(int page, int size, @NotNull Sort sort)
PageRequest.of(int page, int size, @NotNull Direction direction, @NotNull String... properties)
And solution looks like:解决方案如下:
Page<User> users = repository.findAll(PageRequest.of(0, 20));
And of course your UserRepository interface should be extends from PagingAndSortingRepository:当然,您的 UserRepository 接口应该是从 PagingAndSortingRepository 扩展而来的:
public interface UserRepository extends PagingAndSortingRepository<User, Long>
Step 1:步骤1:
In your repository,implement the interface JpaSpecificationExecutor, which as overloaded findAll method, which accepts the Specification Object and page object created above.在您的存储库中,实现接口 JpaSpecificationExecutor,它作为重载的 findAll 方法,接受上面创建的规范对象和页面对象。
The method signature in interface is: Page<T> findAll(@Nullable Specification<T> spec, Pageable pageable);
The repository class looks like this:存储库类如下所示:
@Repository
public interface MyRepository extends
CrudRepository<JobRecord, Long>,
JpaSpecificationExecutor<JobRecord> {
Implement your custom specification with predicates使用谓词实现您的自定义规范
static Specification<JobRecord> findByParam1AndParam2AndParam3(String param1,String param2,String param3) {
return (jobRecord, cq, builder) -> {
List<Predicate> predicates = new ArrayList<>();
predicates.add(builder.equal(jobRecord.get("param1"), "param1"));
predicates.add(builder.equal(jobRecord.get("param2"), "param2"));
predicates.add(builder.equal(jobRecord.get("param3"), "param3"));
// we can add sorting here
cq.orderBy(cb.desc(jobRecord.get("submittedAt")));
// AND all predicates
return builder.and(predicates.toArray(new Predicate[0]));
};
}
Step 2:第2步:
In your Service, create a Pageable page object as:在您的服务中,创建一个 Pageable 页面对象:
Pageable page = PageRequest.of(0, 5);// 0 is the firstResult and 5 is pageSize which we can fetch from queryParams
The findAll method can be used with pagination as:-- findAll 方法可以与分页一起使用:--
List<Job> jobs = repository.findAll(findByParam1AndParam2AndParam3("param1","param2","param3"), page)
.stream()
.map(JobRecord::toModel)
.collect(Collectors.toList());
return new JobList().jobList(jobs);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.