[英]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 通过
Sort
和Pageable
查询数据。 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
)。
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.在我的情况下,要同时使用
Pageable
和Sorting
我使用如下。 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.