Spring Data and Pagination
We are dealing with a huge amount of data, that's why it is important to use Pagination in order to make the application more performant. In this article, we will see how to paginate using Spring Data API.
Let's start with the entity.
Creating a repository:
To access the Video data, we need to create the interface VideoRepository.
We can access the methods findAll(Pageable pageable) and findAll(Sort sort) by making it extending PagingAndSortingRepository. We can also create ower own methods by passing Pageable as parameters.
We will show the example of displaying the list of videos by category.
To create the controller that lists the videos by category using pagination, we must
- Create the Repository (already explained above)
- Create thea PageRequest object.
- Pass the PageRequest object as an argument to the repository method
We create a div which display the indexes of the pages. Then, add a link to this indexes which contains the category and the number of page clicked.