Spring Data JPA使用Sort進行排序(Using Sort)

原文鏈接:https://blog.csdn.net/tiguer/article/details/91444097

結合@Query註解,我們可以使用Sort來對結果進行排序。

1、在CustomerRepository內添加方法

?

1

2

3

4

5

6

7

8

9

10

/**

 * 一個參數,匹配兩個字段

 * @param name2

 * @param sort 指定排序的參數,可以根據需要進行調整

 * @return

 * 這裏Param的值和=:後面的參數匹配,但不需要和方法名對應的參數值對應

 *

 */

@Query("select c from Customer c where c.firstName=:name or c.lastName=:name")

List<Customer> findByName4(@Param("name") String name2,Sort sort);

方法一如既往,是聲明式的,只是在原有方法的基礎上,加上Sort(org.springframework.data.domain.Sort)作爲參數即可。

2、在CustomerController中測試

?

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

/**

 * @Query註解方式查詢,

 * 用@Param指定參數,匹配firstName和lastName

 */

@RequestMapping("/findByName")

public void findByName4(){

  //按照ID倒序排列

  System.out.println("直接創建sort對象,通過排序方法和屬性名");

  Sort sort = new Sort(Sort.Direction.DESC,"id");

  List<Customer> result = repository.findByName4("Bauer",sort);

  for (Customer customer:result){

    System.out.println(customer.toString());

  }

  System.out.println("-------------------------------------------");

  //按照ID倒序排列

  System.out.println("通過Sort.Order對象創建sort對象");

  Sort sortx = new Sort(new Sort.Order(Sort.Direction.DESC,"id"));

  List<Customer> resultx = repository.findByName4("Bauer",sort);

  for (Customer customer:result){

    System.out.println(customer.toString());

  }

  System.out.println("-------------------------------------------");

 

  System.out.println("通過排序方法和屬性List創建sort對象");

  List<String> sortProperties = new ArrayList<>();

  sortProperties.add("id");

  sortProperties.add("firstName");

  Sort sort2 = new Sort(Sort.Direction.DESC,sortProperties);

  List<Customer> result2 = repository.findByName4("Bauer",sort2);

  for (Customer customer:result2){

    System.out.println(customer.toString());

  }

  System.out.println("-------------------------------------------");

 

  System.out.println("通過創建Sort.Order對象的集合創建sort對象");

  List<Sort.Order> orders = new ArrayList<>();

  orders.add(new Sort.Order(Sort.Direction.DESC,"id"));

  orders.add(new Sort.Order(Sort.Direction.ASC,"firstName"));

  List<Customer> result3 = repository.findByName4("Bauer",new Sort(orders));

  for (Customer customer:result3){

    System.out.println(customer.toString());

  }

  System.out.println("-------------------------------------------");

}

這裏總共列舉了四種排序方式:

1)直接創建Sort對象,適合對單一屬性做排序

2)通過Sort.Order對象創建Sort對象,適合對單一屬性做排序

3)通過屬性的List集合創建Sort對象,適合對多個屬性,採取同一種排序方式的排序

4)通過Sort.Order對象的List集合創建Sort對象,適合所有情況,比較容易設置排序方式

對應着我們的使用場景來進行選擇創建Sort對象的方式。

注意,這裏並沒有列舉所有的Sort使用方式,還有忽略大小寫,使用JpaSort.unsafe、聚合函數等進行排序,查詢的屬性值是Entity的屬性名,不是數據庫的字段,要注意到!!

更多用法,請參考源碼:http://www.icnws.com/wp-content/uploads/2017/06/Sort.java_.txt

參考:

官方文檔,http://docs.spring.io/spring-data/jpa/docs/current/reference/html/

DEMO,https://github.com/icnws/spring-data-jpa-demo

 

先說下Sort類常用的幾個構造方法

1.

public Sort(Order... orders) {
   this(Arrays.asList(orders));
}

2.

public Sort(List<Order> orders) {

   Assert.notNull(orders, "Orders must not be null!");

   this.orders = Collections.unmodifiableList(orders);
}

3.

public Sort(String... properties) {
   this(DEFAULT_DIRECTION, properties);
}

4.

public Sort(Direction direction, String... properties) {
   this(direction, properties == null ? new ArrayList<>() : Arrays.asList(properties));
}

5.

public Sort(Direction direction, List<String> properties) {

   if (properties == null || properties.isEmpty()) {
      throw new IllegalArgumentException("You have to provide at least one property to sort by!");
   }

   this.orders = new ArrayList<>(properties.size());

   for (String property : properties) {
      this.orders.add(new Order(direction, property));
   }
}

注:Direction是用來標識列屬性升序還是降序排序的

    properties即爲列屬性

在上面5個方法中,4,5方法只能實現一種排序方向。

                               3方法輸入列名,按照默認的排序方式(ASC)

              在介紹下Order的構造方法:

  public Order (Direction direction,String properties);    --------Order維護一個Direction和一個列屬性

   所以,採用的方法;

@RequestMapping("/sort")
public List<Person> sort(){
    List<Sort.Order> orders=new ArrayList<>();
    orders.add(new Sort.Order(Sort.Direction.DESC,"age"));     ---age降序
    orders.add(new Sort.Order(Sort.Direction.ASC,"name"));     ---naem升序
    return personRepository.findAll(Sort.by(orders));
}

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章