![]() |
VOOZH | about |
Spring Framework is built on top of servlets. This particular web framework comes with very essential features with which we can develop efficient and effective web applications. On top of Spring Framework, Spring Boot was released in April 2014. The main aim behind the Spring Boot was the feature of Auto-Configuration. Spring MVC ( Model View Controller ) is the sub-domain or sub-project of the Spring Framework which is embedded in Spring's 'Web Starter' dependency. It is used for developing web applications. Web Starter also contains features for developing REST API. We use it to return the data or list of data instead of the HTML pages.
The data which is retrieved from a data source can be very large that it will be inconvenient to list all of them, that too directly. This retrieved or returned data can be controlled in a number of ways. For example, to limit the entities or objects return, the way they are sorted, etc. Here, Pagination comes into action.
spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver spring.datasource.url=jdbc:mysql://localhost:3306/userdetails spring.datasource.username=root spring.datasource.password=password spring.jpa.show-sql=true spring.jpa.hibernate.ddl-auto=update spring.jpa.properties.hibernate.dialect=org.hibernate.dialect.MySQL5Dialect
This class is mapped to a table in a database.
1. Persistence Annotations:
2. Lombok Annotations
The object of this class will be returned as a JSON response by the REST API.
Example:
Note: JpaRepository has 4 superinterfaces QueryByExampleExecutor<T>, Repository<T,ID>, CrudRepository<T,ID> and PagingAndSortingRepository<T, ID> which is the one that provides essential Pagination features.
CrudRepository provides the generalized JDBC methods which run SQL scripts to interact with the databases.
(Create, Read, Update, Delete).
public interface PagingAndSortingRepository<T,ID> extends CrudRepository<T,ID>
Methods of PagingAndSortingRepository interface are as follows:
Method | Description |
|---|---|
| Page<T> findAll(Pageable pageable) | Returns a Page of entities meeting the paging restriction provided in the Pageable object. |
| Iterable<T> findAll(Sort sort | Returns all entities sorted by the given options. |
Example:
This object of Pageable is passed to the findAll() method of PagingAndSortingRepository<T, ID> which accepts the same respective object and returns a Page object. Then the items retrieved from the Page object are eventually returned.
Example:
Output 1:
Output 2:
Conclusion:
- As most industries work with large datasets, REST APIs are very popular and widely used as they help in returning data responses.
- Therefore, for them, Pagination becomes a very crucial feature for them to apply.
- It also helps to build a good and manageable UI ( User Interface).