In this tutorial, we will learn on spring data with PagingAndSortingRepository interface.

PagingAndSortingRepository is another Spring Data repository interface, which builds upon the CrudRepository interface and adds support for pagination and sorting of data

User Entity:

@Table(name = "user")
public class User {
    @Column(name = "user_id")
    private Long userId;
    private String email;

	public Long getUserId() {
		return userId;

	public void setUserId(Long userId) {
		this.userId = userId;

	public String getEmail() {
		return email;

	public void setEmail(String email) { = email;

Create UserRepository class that should me marked with @Repository Annotation and Extends JpaRepository

public interface UserRepository extends PagingAndSortingRepository<User, Long> {

In this example, the UserRepository interface is defined as extending the PagingAndSortingRepository interface, where User is the entity class and Long is the type of the primary key.

By extending PagingAndSortingRepository, the UserRepository interface automatically gets a set of methods for performing CRUD operations on the User entity, as well as additional methods for pagination and sorting.

Here is an example of how to use the additional methods provided by PagingAndSortingRepository

Create UserServiceImpl class that should be marked with @Service Annotation:

public class UserServiceImpl {

	private UserRepository userRepo;
	public void saveUser(User user) {;


	public User getUser(Long userId) {
		return  userRepo.findOne(userId);

	public void deleteUser(Long userId) {


        public Page<User> getPaginatedUsers(int page, int size){
           Page<User> pagedUsers = userRepo.findAll(PageRequest.of(page, size));
           return pagedUsers ;

        public List<User> getUsersByEmail(String email){
           List<User> users = userRepo.findAll(;
           return users;

PagingAndSortingRepository Interface methods:

  • findAll(Pageable pageable): Returns a Page of entities meeting the paging restriction provided in the Pageable object.
  • findAll(Sort sort): Returns all entities sorted by the given options.
  • findAllById(Iterable<ID> ids, Sort sort): Returns all entities with the given ids sorted by the given options.

PagingAndSortingRepository provides methods for pagination and sorting, which can be useful when working with large datasets. The findAll(Pageable pageable) method takes a Pageable object, which can be used to specify the page number, page size, and sorting criteria to be used when retrieving data from the repository. The findAll(Sort sort) method takes a Sort object, which can be used to specify the sorting criteria to be used when retrieving data from the repository.

It also allows you to find all entities by ids with a given sorting order.

It is important to mention that it doesn’t provide any other additional method than pagination and sorting. If you need more advanced functionality such as support for filtering, you may consider using JpaRepository.