简体   繁体   English

如何通过排序和分页使用 Spring 数据 JPA 开箱即用地查询数据?

[英]How to query data out of the box using Spring data JPA by both Sort and Pageable?

I am trying Spring data JPA in my project.我正在我的项目中尝试Spring 数据 JPA I want to know if there is an out-of-the-box API to query data, by both Sort and Pageable .我想知道是否有开箱即用的 API 通过SortPageable查询数据。 Of course, I know I can write that method myself, I just want to know if there is an out-of-the-box one.当然,我知道我可以自己编写那个方法,我只是想知道是否有一个开箱即用的方法。 My DAO extends JpaRepository , and I found there are the following methods I can invoke:我的 DAO 扩展JpaRepository ,我发现可以调用以下方法:

findAll();
findAll(Pageable pageable);
findAll(Sort sort);

But there is no such method as findAll(Sort sort, Pageable pageable) , so I am curious.但是没有findAll(Sort sort, Pageable pageable)这样的方法,所以我很好奇。

There are two ways to achieve this:有两种方法可以实现这一点:

final PageRequest page1 = new PageRequest(
  0, 20, Direction.ASC, "lastName", "salary"
);

final PageRequest page2 = new PageRequest(
  0, 20, new Sort(
    new Order(Direction.ASC, "lastName"), 
    new Order(Direction.DESC, "salary")
  )
);

dao.findAll(page1);

As you can see the second form is more flexible as it allows to define different direction for every property ( lastName ASC, salary DESC ).正如您所看到的,第二种形式更加灵活,因为它允许为每个属性定义不同的方向( lastName ASC, salary DESC )。

Pageable has an option to specify sort as well. Pageable 也有一个选项来指定排序。 From the java doc来自java 文档

PageRequest(int page, int size, Sort.Direction direction, String... properties) 

Creates a new PageRequest with sort parameters applied.创建一个应用了排序参数的新 PageRequest。

in 2020, the accepted answer is kinda out of date since the PageRequest is deprecated, so you should use code like this :在 2020 年,接受的答案有点过时,因为PageRequest已被弃用,因此您应该使用如下代码:

Pageable page = PageRequest.of(pageable.getPageNumber(), pageable.getPageSize(), Sort.by("id").descending());
return repository.findAll(page);

Spring Pageable has a Sort included. Spring Pageable包含一个 Sort 。 So if your request has the values it will return a sorted pageable.因此,如果您的请求具有值,它将返回一个排序的可分页。

request: domain.com/endpoint?sort=[FIELDTOSORTBY]&[FIELDTOSORTBY].dir=[ASC|DESC]&page=0&size=20请求: domain.com/endpoint?sort=[FIELDTOSORTBY]&[FIELDTOSORTBY].dir=[ASC|DESC]&page=0&size=20

That should return a sorted pageable by field provided in the provided order.这应该返回按提供的顺序提供的按字段排序的可分页。

In my case, to use Pageable and Sorting at the same time I used like below.在我的情况下,要同时使用PageableSorting我使用如下。 In this case I took all elements using pagination and sorting by id by descending order:在这种情况下,我使用分页和按id按降序排序的所有元素:

modelRepository.findAll(PageRequest.of(page, 10, Sort.by("id").descending()))

Like above based on your requirements you can sort data with 2 columns as well.像上面一样,根据您的要求,您也可以使用 2 列对数据进行排序。

It's possible to provide sort parameter & sort direction right in the URL of the request.可以在请求的 URL 中提供排序参数和排序方向。 Spring Pageable automatically parses the URL parameters & creates Pageable object that is obtained by your rest controller. Spring Pageable automatically parses the URL parameters & creates Pageable object that is obtained by your rest controller. I believe it's one of the best solutions since it helps to avoid manual PageRequest/Pageable objects instantiation (notice the comma character before sort direction):我相信这是最好的解决方案之一,因为它有助于避免手动 PageRequest/Pageable 对象实例化(注意排序方向之前的逗号字符):

domain.com/endpoint?sort=[FIELD],[asc|desc]&page=3&size=100 domain.com/endpoint?sort=[FIELD],[asc|desc]&page=3&size=100

PS Other example of sort direction usage described in Spring documentation not works for me (might be outdated). PS Spring 文档中描述的排序方向用法的其他示例不适用于我(可能已过时)。

 public List<Model> getAllData(Pageable pageable){
       List<Model> models= new ArrayList<>();
       modelRepository.findAllByOrderByIdDesc(pageable).forEach(models::add);
       return models;
   }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 spring 数据 jpa @query 和 pageable - spring data jpa @query and pageable JPA Criteria查询与Spring Data Pageable的总和 - JPA Criteria query sum with Spring Data Pageable 排序实体及其嵌套集合@OneToMany Spring Data JPA with Pageable - Sort entity and its nested collection @OneToMany Spring Data JPA with Pageable 使用@ManyToMany对Spring Data JPA进行分页 - Spring Data JPA Pageable with @ManyToMany Spring Jpa 数据、Pageable、Pagerequest - Spring Jpa Data , Pageable , Pagerequest 如何创建未分页但已排序的 Pageable.of(unpaged, unpaged, Sort ) 到 spring 数据 jpa 存储库? - How to create unpaged but sorted Pageable.of(unpaged, unpaged, Sort ) to spring data jpa repository? Spring Data JPA:具有Specification和Pageable的findAll在计数查询上失败 - Spring Data JPA: findAll with Specification and Pageable fails on count query 如何从具有多个计数和 Group by 查询的 Spring Data JPA 返回可分页的自定义对象? - How to return a pageable custom object from a Spring Data JPA with multiple counts and Group by query? Spring Data JPA,如何通过Pageable获取最后一页 - Spring Data JPA, how to get the last Page through Pageable 如何在没有 IllegalArgumentException 的情况下使用 Jpa 中的自定义查询获取可分页的数据? - How to fetch data as pageable with custom query in Jpa without IllegalArgumentException?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM