Spring Data for MongoDB is part of the umbrella Spring Data project which aims to provide a familiar and consistent Spring-based programming model for for new datastores while retaining store-specific features and capabilities. See more about SpringData
In this example we will see how to create simple queries and get customer details which are saved in MongoDB by using Spring Data and MongoDB.
Prerequisites: MongoDB should up and running
1. Create a Maven Project
Create a Maven project and add below dependencies
<repositories><repository><id>spring-milestone</id><name>Spring Maven MILESTONE Repository</name><url>http://maven.springframework.org/milestone</url></repository></repositories><dependencies><dependency><groupId>org.springframework</groupId><artifactId>spring-core</artifactId><version>3.0.5.RELEASE</version></dependency><dependency><groupId>org.springframework</groupId><artifactId>spring-context</artifactId><version>3.0.5.RELEASE</version></dependency><!-- mongodb java driver --><dependency><groupId>org.mongodb</groupId><artifactId>mongo-java-driver</artifactId><version>2.5.2</version></dependency><dependency><groupId>org.springframework.data</groupId><artifactId>spring-data-mongodb</artifactId><version>1.0.0.M2</version></dependency><dependency><groupId>cglib</groupId><artifactId>cglib</artifactId><version>2.2</version></dependency></dependencies>
2. Create a POJO (CustomerPOJO.java)
package com.pretech;public class CustomerPOJO {private String name;private String address;private int age;public int getAge() {return age;}public void setAge(int age) {this.age = age;}public CustomerPOJO(String name, String address, int age) {super();this.name = name;this.address = address;this.age = age;}public CustomerPOJO() {}public String getName() {return name;}public void setName(String name) {this.name = name;}public String getAddress() {return address;}public void setAddress(String address) {this.address = address;}@Overridepublic String toString() {return "CustomerPOJO [name=" + name + ", address=" + address + ", age="+ age + "]";}}
3. Spring configuration (mongo-context.xml)
Place this file in to src/main/resources folder
<?xml version="1.0" encoding="UTF-8"?><beans xmlns="http://www.springframework.org/schema/beans"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context"xmlns:mongo="http://www.springframework.org/schema/data/mongo"xsi:schemaLocation="http://www.springframework.org/schema/contexthttp://www.springframework.org/schema/context/spring-context-3.0.xsdhttp://www.springframework.org/schema/data/mongohttp://www.springframework.org/schema/data/mongo/spring-mongo-1.0.xsdhttp://www.springframework.org/schema/beanshttp://www.springframework.org/schema/beans/spring-beans-3.0.xsd"><!-- Default bean name is 'mongo' --><mongo:mongo host="localhost" port="27017" /><bean id="customermongoTemplate" class="org.springframework.data.document.mongodb.MongoTemplate"><constructor-arg ref="mongo" /><constructor-arg name="databaseName" value="customerdb" /><constructor-arg name="defaultCollectionName" value="customerCollection" /></bean><!-- To translate any MongoExceptions thrown in @Repository annotated classes --><context:annotation-config /></beans>
4. Create a Main class to save Customer details
package com.pretech;import java.util.List;import org.springframework.context.ApplicationContext;import org.springframework.context.support.GenericXmlApplicationContext;import org.springframework.data.document.mongodb.MongoOperations;import org.springframework.data.document.mongodb.query.Criteria;import org.springframework.data.document.mongodb.query.Query;public class SpringDataCriteriaExample {public static void main(String[] args) {ApplicationContext ctx = new GenericXmlApplicationContext("mongo-context.xml");MongoOperations mongoOperation = (MongoOperations) ctx.getBean("customermongoTemplate");CustomerPOJO customer = new CustomerPOJO("Raju", "Bangalore",20);mongoOperation.save("customerDetails", customer);CustomerPOJO customer1 = new CustomerPOJO("Santhos", "chennai",19);mongoOperation.save("customerDetails", customer1);CustomerPOJO customer2 = new CustomerPOJO("Shiva", "hyd",45);mongoOperation.save("customerDetails", customer2);CustomerPOJO customer3 = new CustomerPOJO("Rahkan", "mysore",55);mongoOperation.save("customerDetails", customer3);//Get All CustomerList<CustomerPOJO> allCustomers = mongoOperation.find("customerDetails",new Query(),CustomerPOJO.class);System.out.println("All Customers : " + allCustomers);//Get all customers name is ShivaList<CustomerPOJO> savedCustomer1 = mongoOperation.find("customerDetails",new Query(Criteria.where("name").is("Shiva")),CustomerPOJO.class);System.out.println("Name Customers : " + savedCustomer1);//Customer less than 21 yearsList<CustomerPOJO> savedCustomer = mongoOperation.find("customerDetails",new Query(Criteria.where("age").lte(21)),CustomerPOJO.class);System.out.println("Minor Customers : " + savedCustomer);//Customers greater than 21 yearsList<CustomerPOJO> savedCustomer2 = mongoOperation.find("customerDetails",new Query(Criteria.where("age").gte(21)),CustomerPOJO.class);System.out.println("Major Customers : " + savedCustomer2);}}
Output
Console
All Customers : [CustomerPOJO [name=Raju, address=Bangalore, age=20], CustomerPOJO [name=Santhos, address=chennai, age=19], CustomerPOJO [name=Shiva, address=hyd, age=45], CustomerPOJO [name=Rahkan, address=mysore, age=55]]
Name Customers : [CustomerPOJO [name=Shiva, address=hyd, age=45]]
Minor Customers : [CustomerPOJO [name=Raju, address=Bangalore, age=20], CustomerPOJO [name=Santhos, address=chennai, age=19]]
Major Customers : [CustomerPOJO [name=Shiva, address=hyd, age=45], CustomerPOJO [name=Rahkan, address=mysore, age=55]]
Mongodb
No comments:
Post a Comment