In today’s post I would give you a sample program which loads data from MongoDb using Spring Data as page by page. This is useful when you load large data set into screen. The complete source code of this post can be found github. Libraries Use pom.xml
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 |
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>com.ourownjava</groupId> <artifactId>spring-data-mongo-pagination</artifactId> <version>0.0.1-SNAPSHOT</version> <packaging>jar</packaging> <name>spring-data-mongo-pagination</name> <url>http://maven.apache.org</url> <properties> <spring.framework.version>3.2.4.RELEASE</spring.framework.version> <spring.data.mongo.version>1.2.2.RELEASE</spring.data.mongo.version> <junit.version>4.8.1</junit.version> <nosqlunit.version>0.7.7</nosqlunit.version> <mongodb.java.driver.version>2.11.0</mongodb.java.driver.version> </properties> <dependencies> <dependency> <groupId>org.springframework.data</groupId> <artifactId>spring-data-mongodb</artifactId> <version>${spring.data.mongo.version}</version> </dependency> <dependency> <groupId>org.mongodb</groupId> <artifactId>mongo-java-driver</artifactId> <version>${mongodb.java.driver.version}</version> </dependency> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>${junit.version}</version> <scope>test</scope> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-test</artifactId> <version>${spring.framework.version}</version> <scope>test</scope> </dependency> <dependency> <groupId>com.lordofthejars</groupId> <artifactId>nosqlunit-mongodb</artifactId> <version>${nosqlunit.version}</version> <scope>test</scope> </dependency> </dependencies> </project> |
ProductRepository Source Code
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 |
package com.ourownjava.spring.data.mongo.pagination; import org.springframework.data.domain.PageRequest; import org.springframework.data.domain.Pageable; import org.springframework.data.repository.PagingAndSortingRepository; /** * * @author ourownjava.com * */ public interface ProductRepository extends PagingAndSortingRepository<product, string=""> { public static class PageSpecification { /** * * @param pageIndex * @return */ public static Pageable constructPageSpecification(final int pageIndex, final int pageSize) { Pageable pageSpecification = new PageRequest(pageIndex, pageSize); return pageSpecification; } } } |
Product Domain Object […]
Continue reading…