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
Name | Version |
---|---|
Spring Framwork | 3.2.4.RELEASE |
Spring Data Mongo | 1.2.2.RELEASE |
JUnit | 4.8.1 |
No SQL Unit | 0.7.7 |
MongoDB Java Driver | 2.11.0 |
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
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 |
package com.ourownjava.spring.data.mongo.pagination; import org.springframework.data.annotation.Id; import org.springframework.data.mongodb.core.mapping.Document; /** * * @author ourownjava.com * */ @Document(collection="product") public class Product { @Id private String id; private String name; private Double price; public String getId() { return id; } public void setId(final String id) { this.id = id; } public String getName() { return name; } public void setName(final String name) { this.name = name; } public Double getPrice() { return price; } public void setPrice(final Double price) { this.price = price; } } |
ProductRepository Test Class
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 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 |
package com.ourownjava.spring.data.mongo.pagination; import static com.lordofthejars.nosqlunit.mongodb.MongoDbRule.MongoDbRuleBuilder.newMongoDbRule; import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertFalse; import static org.junit.Assert.assertTrue; import org.junit.Rule; import org.junit.Test; import org.junit.runner.RunWith; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.context.ApplicationContext; import org.springframework.context.annotation.ComponentScan; import org.springframework.context.annotation.Configuration; import org.springframework.context.annotation.PropertySource; import org.springframework.data.domain.Page; import org.springframework.data.mongodb.config.AbstractMongoConfiguration; import org.springframework.data.mongodb.repository.config.EnableMongoRepositories; import org.springframework.test.context.ContextConfiguration; import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; import com.foursquare.fongo.Fongo; import com.lordofthejars.nosqlunit.annotation.UsingDataSet; import com.lordofthejars.nosqlunit.mongodb.MongoDbRule; import com.mongodb.Mongo; /** * @author ourownjava.com * */ @RunWith(SpringJUnit4ClassRunner.class) @ContextConfiguration public class TestProductRepository { private static final int NUMBER_OF_PRODUCTS_PER_PAGE = 2; @Rule public MongoDbRule mongoDbRule = newMongoDbRule().defaultSpringMongoDb( "pagination-test"); @Autowired private ApplicationContext applicationContext; @Autowired private ProductRepository productRepository; @Test @UsingDataSet(locations = { "/testData/product.json" }) public void shouldLoadPageByPage() { Page products = productRepository .findAll(ProductRepository.PageSpecification .constructPageSpecification(0, NUMBER_OF_PRODUCTS_PER_PAGE)); assertEquals(2, products.getSize()); assertFalse(products.hasPreviousPage()); assertTrue(products.hasNextPage()); assertEquals(3, products.getTotalPages()); assertEquals(0, products.getNumber()); assertEquals(2, products.getNumberOfElements()); assertEquals(6, products.getTotalElements()); products = productRepository .findAll(ProductRepository.PageSpecification .constructPageSpecification(1, NUMBER_OF_PRODUCTS_PER_PAGE)); assertEquals(2, products.getSize()); assertTrue(products.hasPreviousPage()); assertTrue(products.hasNextPage()); assertEquals(3, products.getTotalPages()); assertEquals(1, products.getNumber()); assertEquals(2, products.getNumberOfElements()); assertEquals(6, products.getTotalElements()); products = productRepository .findAll(ProductRepository.PageSpecification .constructPageSpecification(2, NUMBER_OF_PRODUCTS_PER_PAGE)); assertEquals(2, products.getSize()); assertTrue(products.hasPreviousPage()); assertFalse(products.hasNextPage()); assertEquals(3, products.getTotalPages()); assertEquals(2, products.getNumber()); assertEquals(2, products.getNumberOfElements()); assertEquals(6, products.getTotalElements()); } @Test @UsingDataSet(locations = { "/testData/product.json" }) public void shouldLoadProductsAndValidateData(){ Page products = productRepository .findAll(ProductRepository.PageSpecification .constructPageSpecification(0, NUMBER_OF_PRODUCTS_PER_PAGE)); assertEquals(2, products.getSize()); assertFalse(products.hasPreviousPage()); assertTrue(products.hasNextPage()); assertEquals(3, products.getTotalPages()); assertEquals(0, products.getNumber()); assertEquals(2, products.getNumberOfElements()); assertEquals(6, products.getTotalElements()); assertEquals("p1", products.getContent().get(0).getId()); assertEquals("pname1", products.getContent().get(0).getName()); assertEquals(Double.valueOf(11.11), products.getContent().get(0).getPrice()); assertEquals("p2", products.getContent().get(1).getId()); assertEquals("pname2", products.getContent().get(1).getName()); assertEquals(Double.valueOf(12.12), products.getContent().get(1).getPrice()); } @Configuration @EnableMongoRepositories @ComponentScan(basePackageClasses = { ProductRepository.class }) @PropertySource("classpath:application.properties") static class MongoConfiguration extends AbstractMongoConfiguration { @Override protected String getDatabaseName() { return "pagination-test"; } @Override public Mongo mongo() { return new Fongo("mongo-test").getMongo(); } @Override protected String getMappingBasePackage() { return "com.ourownjava.spring.data.pagination.*"; } } } |
Pleaes refer the original source code in github for test data and other configuration files.