Spring Data JPAを使用したページ区切りとソート

Spring Data JPAを使用したページネーションとソート

1. 概要

ページネーションは、大規模なデータセットがあり、小さなチャンクでユーザーに表示する場合に役立ちます。

また、ページング中にいくつかの基準でそのデータを並べ替える必要があることがよくあります。

このチュートリアルでは、we’ll learn how to easily paginate and sort using Spring Data JPA.

参考文献:

Spring Data JPA @Query

Spring Data JPAで@Queryアノテーションを使用して、JPQLとネイティブSQLを使用してカスタムクエリを定義する方法を学びます。

Spring Data JPAリポジトリの派生クエリメソッド

Spring Data JPAのクエリ派生メカニズムを調べます。

2. 初期設定

まず、Productエンティティがあるとします。

@Entity
public class Product {

    @Id
    private long id;
    private String name;
    private double price;

    // constructors, getters and setters

}

ドメインクラスとして。 各Productインスタンスには、一意の識別子(id、そのname、およびそれに関連付けられたprice)があります。

3. リポジトリの作成

Productsにアクセスするには、ProductRepositoryが必要です。

public interface ProductRepository extends PagingAndSortingRepository {

    List findAllByPrice(double price, Pageable pageable);
}

PagingAndSortingRepository, we get findAll(Pageable pageable) and findAll(Sort sort) methods for paging and sorting.を拡張することによって

または、PagingAndSortingRepositoryも拡張するため、代わりにJpaRepositoryを拡張することを選択することもできます。

ここでfindAllByPriceを使用して行ったように、PagingAndSortingRepositorywe can add our own methods that take Pageable and Sort as parametersを拡張したら。

新しい方法を使用してProductsをページ付けする方法を見てみましょう。

4. ページ付け

リポジトリをPagingAndSortingRepositoryから拡張したら、次のことを行う必要があります。

  1. Pageableインターフェイスの実装であるPageRequestオブジェクトを作成または取得します

  2. 使用するリポジトリメソッドへの引数としてPageRequestオブジェクトを渡します

要求されたページ番号とページサイズを渡すことで、PageRequestオブジェクトを作成できます。

ここで、the page counts starts at zero:

Pageable firstPageWithTwoElements = PageRequest.of(0, 2);

Pageable secondPageWithFiveElements = PageRequest.of(1, 5);

Spring MVCでは、Spring Data Web Supportを使用してコントローラーでPageableインスタンスを取得することもできます。

PageRequestオブジェクト\を取得したら、リポジトリのメソッドを呼び出しながらそれを渡すことができます。

Page allProducts = productRepository.findAll(firstPageWithTwoElements);

List allTenDollarProducts =
  productRepository.findAllByPrice(10, secondPageWithFiveElements);

findAll(Pageable pageable)メソッドは、デフォルトでPage<T>オブジェクトを返します。

ただし、we can choose to return either a Page<T>, a Slice<T> or a List<T> from any of our custom methods returning a paginated data

Page<T>インスタンスは、Productsのリストを持つことに加えて、使用可能なページの総数も認識します。 It triggers an additional count query to achieve it. To avoid such an overhead cost, we can instead return a Slice<T> or a List<T>.

Sliceは、次のスライスが使用可能かどうかのみを認識します。

5. ページネーションとソート

同様に、クエリ結果を並べ替えるだけで、メソッドにpass an instance of Sortを追加するだけで済みます。

Page allProductsSortedByName = productRepository.findAll(Sort.by("name"));

ただし、both sort and page our data?にしたい場合はどうなりますか

これを行うには、並べ替えの詳細をPageRequestオブジェクト自体に渡します。

Pageable sortedByName =
  PageRequest.of(0, 3, Sort.by("name"));

Pageable sortedByPriceDesc =
  PageRequest.of(0, 3, Sort.by("price").descending());

Pageable sortedByPriceDescNameAsc =
  PageRequest.of(0, 5, Sort.by("price").descending().and(Sort.by("name")));

並べ替えの要件に基づいて、PageRequestインスタンスの作成中にwe can specify the sort fields and the sort directionを実行します。

いつものように、このPageableタイプのインスタンスをリポジトリのメソッドに渡すことができます。

6. 結論

この記事では、Spring Data JPAでクエリ結果をページ分割およびソートする方法を学びました。

いつものように、このチュートリアルで使用される完全なコード例は、over on Githubで入手できます。