Spring-Mongo Template Pagination
1. Introduction
In this blog, we will be creating a sample project where we would use the spring mongo template for pagination. By default, the spring-mongo template has no method for pagination.
2. Spring Data
Spring Data is an umbrella project which contains many submodules, each specific to a particular database. In this article, we’ll be covering the Spring-Mongo template for fetching the records with pagination.
3. MongoDB
MongoDB is a document-oriented NoSQL database that stores JSON-like documents with dynamic schemas. It is commonly used for high volume data storage.
4. Setup
In this section, we will quickly set up a project using Spring Initializr. Using this tool, we can quickly provide a list of Dependencies we need and download the bootstrapped application:
5. Maven Dependencies
Here is the pom.xml file, you should see the below dependencies added:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 | <dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-data-mongodb</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> <exclusions> <exclusion> <groupId>org.junit.vintage</groupId> <artifactId>junit-vintage-engine</artifactId> </exclusion> </exclusions> </dependency> </dependencies> |
6. User Entity
In this section, we will make a simple User entity with some fields to demonstrate simple MongoDB queries:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 | @Document(collection = "user") public class User { @Id private String id; private String userName; private String firstName; private String lastName; private String email; ... } |
7. Repository
Now let’s define a custom data layer interface:
1 2 3 4 | public interface UserCustomRepository { Page<User> findUsers(Pageable pageable); } |
8. Implementing Repository
Now let’s implement the above data layer interface. We will make use of Mongo Template bean which is initialized by Spring Boot. Below is the implementation class:
1 2 3 4 5 6 7 8 9 10 11 | @Repository public class UserRepositoryImpl implements UserCustomRepository { private final MongoTemplate mongoTemplate; public UserRepositoryImpl(MongoTemplate mongoTemplate) { this.mongoTemplate = mongoTemplate; } ... } |
9. Using Paginated Queries
Now let’s have a look at the pagination queries using Mongo Template. We had used PageableExecutionUtils class of Spring Data for pagination.
1 2 3 4 5 6 7 8 9 | @Override public Page<User> findUsers(Pageable pageable) { Query query = new Query(); long count = this.mongoTemplate.count(query, User.class); query.with(pageable); List<User> users = mongoTemplate.find(query, User.class); return PageableExecutionUtils.getPage(users, pageable, () -> count); } |
10. Conclusion
In this quick tutorial, we saw how we can use pagination in custom queries using Mongo Template. The source code for this application is available over on GitHub.
Write a Comment