Pagination and Sorting

Let’s learn to enable pagination and sorting capabilities in the JPA repositories.

PagingAndSortingRepository

So far, we’ve integrated our REST APIs for CRUD operations with the database by using JPA,s TodoRepository and TodoTypeRepository repositories. We also extended the CrudRepository interface. In this lesson, we’ll replace the CrudRepository with the PagingAndSortingRepository to enable pagination and sorting features.

TodoRepository extending PagingAndSortingRepository

Press + to interact
package io.educative.repositories;
import io.educative.domains.Todo;
import org.springframework.data.repository.PagingAndSortingRepository;
import org.springframework.stereotype.Repository;
@Repository
public interface TodoRepository extends PagingAndSortingRepository<Todo, Long> {
}

TodoTypeRepository extending PagingAndSortingRepository

Press + to interact
package io.educative.repositories;
import io.educative.domains.TodoType;
import org.springframework.data.repository.PagingAndSortingRepository;
import org.springframework.stereotype.Repository;
@Repository
public interface TodoTypeRepository extends PagingAndSortingRepository<TodoType, String> {
}

Let’s look at the findAll methods of the PagingAndSortingRepository interface with the Sort and Pageable parameters:

public interface PagingAndSortingRepository<T, ID>
...