Creating Repository
public interface ProductRepository extends PagingAndSortingRepository<Product, Integer> { List<Product> findAllByPrice(double price, Pageable pageable); }
Pagination
Pageable firstPageWithTwoElements = PageRequest.of(0, 2); Pageable secondPageWithFiveElements = PageRequest.of(1, 5);
Page<Product> allProducts = productRepository.findAll(firstPageWithTwoElements); List<Product> allTenDollarProducts = productRepository.findAllByPrice(10, secondPageWithFiveElements);
Pagination and Sorting
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")));
References
https://www.baeldung.com/spring-data-jpa-pagination-sorting
https://www.baeldung.com/queries-in-spring-data-mongodb