Sorting Query Results with Spring Data – 用Spring Data对查询结果进行排序

最后修改: 2018年 11月 27日

中文/混合/英文(键盘快捷键:t)

1. Introduction

1.绪论

In this tutorial, we’ll learn how to sort query results with Spring Data.

在本教程中,我们将学习如何使用Spring Data对查询结果进行排序。

First, we’ll take a look at the schema of the data that we want to query and sort. Then we’ll discuss how to achieve that with Spring Data.

首先,我们将看一下我们想要查询和排序的数据模式。然后,我们将讨论如何用Spring Data实现这一目标。

Let’s get started!

让我们开始吧!

2. The Test Data

2.测试数据

Below we have some sample data. Although we’ve represented it here as a table, we could use any one of the databases supported by Spring Data to persist it.

下面我们有一些示例数据。虽然我们在这里把它表示为一个表,但我们可以使用Spring Data支持的任何一个数据库来持久化它。

The question we’re going to answer is, “Who is occupying which seat on the airline?” To make this more user-friendly, we’ll sort by seat number.

我们要回答的问题是,”谁占据了航空公司的哪个座位?”为了更方便用户,我们将按座位号排序。

First Name Last Name Seat Number
Jill Smith 50
Eve Jackson 94
Fred Bloggs 22
Ricki Bobbie 36
Siya Kolisi 85

3. Domain

3.领域

To create a Spring Data Repository, we need to provide a domain class, as well as an id type.

要创建Spring Data Repository,我们需要提供一个域类,以及一个id类型。

Here we’ve modeled our passenger as a JPA entity, but we could have just as easily modeled it as a MongoDB document, or any other model abstraction:

在这里,我们将乘客建模为JPA实体,但我们也可以很容易地将其建模为MongoDB文档,或任何其他模型抽象。

@Entity
class Passenger {

    @Id
    @GeneratedValue
    @Column(nullable = false)
    private Long id;

    @Basic(optional = false)
    @Column(nullable = false)
    private String firstName;

    @Basic(optional = false)
    @Column(nullable = false)
    private String lastName;

    @Basic(optional = false)
    @Column(nullable = false)
    private int seatNumber;

    // constructor, getters etc.
}

4. Sorting With Spring Data

4.用Spring数据进行排序

We have a few different options at our disposal for sorting with Spring Data.

我们有几个不同的选择,可以用Spring Data进行分类。

4.1. Sorting With the OrderBy Method Keyword

4.1.使用OrderBy方法关键字进行排序

One option is to use Spring Data’s method derivation, whereby the query is generated from the method name and signature.

一种选择是使用Spring Data的方法推导,即从方法名称和签名中生成查询。

All we need to do here to sort our data is include the keyword OrderBy in our method name, along with the property name(s) and direction (Asc or Desc) by which we want to sort.

在这里,我们需要做的就是在我们的方法名称中加入关键词OrderBy,以及我们想要排序的属性名称和方向(升或降)。

We can use this convention to create a query that returns our passengers in ascending order by seat number:

我们可以使用这个惯例来创建一个查询,按照座位号的升序来返回我们的乘客。

interface PassengerRepository extends JpaRepository<Passenger, Long> {

    List<Passenger> findByOrderBySeatNumberAsc();
}

We can also combine this keyword with all the standard Spring Data method names.

我们也可以将这个关键字与所有标准的Spring Data方法名称结合起来。

Let’s see an example of a method that finds passengers by last name and orders by seat number:

让我们看看一个方法的例子,该方法按姓氏查找乘客按座位号排序。

List<Passenger> findByLastNameOrderBySeatNumberAsc(String lastName);

4.2. Sorting With a Sort Parameter

4.2.用Sort参数进行排序

Our second option is to include a Sort parameter specifying the property name(s) and direction by which we want to sort:

我们的第二个选择是包括一个Sort参数,指定我们想要排序的属性名称和方向。

List<Passenger> passengers = repository.findAll(Sort.by(Sort.Direction.ASC, "seatNumber"));

In this case, we’re using the findAll() method, and adding the Sort option when calling it.

在这种情况下,我们使用findAll()方法,并在调用它时添加Sort选项。

We can also add this parameter to a new method definition:

我们也可以把这个参数添加到一个新的方法定义中。

List<Passenger> findByLastName(String lastName, Sort sort);

Finally, if perhaps we’re paging, we can specify our sort in a Pageable object:

最后,如果也许我们要分页,我们可以在一个Pageable对象中指定我们的排序。

Page<Passenger> page = repository.findAll(PageRequest.of(0, 1, Sort.by(Sort.Direction.ASC, "seatNumber")));

5. Conclusion

5.总结

We have two easy options for sorting data with Spring Data: method derivation using the OrderBy keyword, or using the Sort object as a method parameter.

我们有两个简单的选择来用Spring Data进行数据排序:使用OrderBy关键字进行方法推导,或者使用Sort对象作为方法参数。

As always, the code used in this article is available over on GitHub.

一如既往,本文中使用的代码可在GitHub上找到。