Databases Reference
In-Depth Information
We can use this interface to paginate the query results with Spring Data JPA by:
1.
Creating a new PageRequest object. We can use the PageRequest class
because it implements the Pageable interface.
2.
Passing the created object to a repository method as a parameter.
If we are using query methods for creating our queries, we have got two options for
the return type of the query method:
• If we need to access the metadata of the requested page, we can make our
query method return Page<T> where T is the type of the managed entity.
• If we are interested in only getting the contacts of the requested page, we
should make our query method return List<T> , where T is the type of the
managed entity.
In order to add pagination to our contact manager application, we have to make
changes to the service layer of our application and implement the pagination. Both of
these tasks are described in more detail in the following subsections.
Changing the service layer
Since the Spring Data JPA repositories are just interfaces, we have to create the
PageRequest objects on the service layer. This means that we have to figure out a
way to pass the pagination parameters to the service layer and use these parameters
to create the PageRequest object. We can achieve this goal by following these steps:
1.
We create a class that stores the pagination parameters and the search term.
2.
We change the method signatures of the service interface.
3.
We implement a way to create PageRequest objects.
Creating a class for pagination parameters
First, we have to create a class that is used to store both the pagination parameters
and the used search term. Spring Data provides a custom argument resolver called
PageableArgumentResolver that will automatically build the PageRequest object
by parsing the request parameters. More information about this approach is available
at http://static.springsource.org/spring-data/data-jpa/docs/current/
reference/html/#web-pagination .
 
Search WWH ::




Custom Search