How to combine pagination with a criteria query in Spring Data JPA?

后端 未结 2 1632
旧巷少年郎
旧巷少年郎 2021-02-13 06:30

I use PagingAndSortingRepository and findAll(Pageable pageable) method to paging my data. I think that there is no way to provide any condition. For example sometime I want sele

2条回答
  •  北恋
    北恋 (楼主)
    2021-02-13 07:24

    The PagingAndSortingRepository just adds the very basic CRUD methods in pagination mode. As the reference documentation describes, you can simply add a Pageable parameter to any query method you define to achieve pagination of this query.

    interface CustomerRepository implements Repository {
    
      Page findByLastname(String lastname, Pageable pageable);
    
      List findByFirstname(String firstname, Pageable pageable);
    }
    

    The first method will return a Page containing the page metadata like total elements available etc. To calculate this data it will trigger an additional count query for the query derived. The second query method returns the plain sliced result set without the count query being executed.

提交回复
热议问题