简体   繁体   English

如何使用分页和 spring data jpa 获取 findAll () 服务的所有记录?

[英]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.

相关问题 如何使用 spring 数据 jpa 返回该日期的日期和记录数? - How can I return date and count of records for that date using spring data jpa? 如何使用 Spring Data Pagination 在一页中获取所有结果 - How to get all results in one page using Spring Data Pagination Spring Data:抽象基础服务中的JPA存储库,用于调用带有规范的findAll - Spring Data: JPA Repository in abstract base service to call findAll with specification 使用spring-data-jpa中的PagingAndSortingRepository获取所有记录 - fetch all records using PagingAndSortingRepository in spring-data-jpa Spring Data JPA-在MySQL上使用findAll时发生异常 - Spring Data JPA - exception when using findAll with MySQL Spring Data JPA - 使用标志列的存储库findAll方法 - Spring Data JPA - repository findAll method using flag column Spring data jpa 未选择所有记录 - Spring data jpa not selecting all records 如何使用Spring Data JPA删除子记录 - How to delete child records using Spring Data JPA spring数据jpa:更新@许多边表后,无法以相同方法通过findAll @one边表获得正确的更改 - spring data jpa: after update @many side table, can't get correct changes by findAll @one side table in the same method 使用SQL Server的Spring Data JPA FINDALL - Spring data jpa FINDALL with SQL Server
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM