简体   繁体   中英

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. 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:

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 .

If you are using PagingAndSortingRepository and still want a list of "things",

You can add a method List<Thing> findBy()

If you have a RepositoryRestResource, it will be exposed as REST: /things/search/findBy

如果您不需要分页,请使用CrudRepository而不是PagingAndSortingRepository

Now PageRequest constructor is protected and you cannot create with 'new'. 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:

public interface UserRepository extends PagingAndSortingRepository<User, Long>

Step 1:

In your repository,implement the interface JpaSpecificationExecutor, which as overloaded findAll method, which accepts the Specification Object and page object created above.

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:

In your Service, create a Pageable page object as:

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:--

List<Job> jobs = repository.findAll(findByParam1AndParam2AndParam3("param1","param2","param3"), page)
      .stream()
      .map(JobRecord::toModel)
      .collect(Collectors.toList());
    return new JobList().jobList(jobs);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM