<beans xmlns:context="http://www.springframework.org/schema/context" xmlns:mongo="http://www.springframework.org/schema/data/mongo" xmlns:p="http://www.springframework.org/schema/p" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://www.springframework.org/schema/beans" xsi:schemalocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd http://www.springframework.org/schema/data/mongo http://www.springframework.org/schema/data/mongo/spring-mongo-1.0.xsd"> <!-- Default bean name is 'mongo' --> <mongo:mongo host="localhost" port="27017"/> <!-- Default bean name is 'mongo' --> <mongo:mongo> <mongo:options connections-per-host="100" threads-allowed-to-block-for-connection-multiplier="5" max-wait-time="120000000" connect-timeout="10000000" socket-keep-alive="true" socket-timeout="15000000" auto-connect-retry="true"/> </mongo:mongo> <context:annotation-config/> <context:component-scan base-package="com.dineshonjava.mongo"> <context:exclude-filter type="annotation" expression="org.springframework.context.annotation.Configuration"/> </context:component-scan> <!-- Offers convenience methods and automatic mapping between MongoDB JSON documents and your domain classes. --> <bean class="org.springframework.data.mongodb.core.MongoTemplate" id="mongoTemplate"> <constructor-arg ref="mongo"/> <constructor-arg name="databaseName" value="dineshonjavaDB"/> </bean> </beans>The MongoTemplate is configured with a reference to a MongoDBFactoryBean (which handles the actual database connectivity) and is setup with a database name used for this example.
package com.dineshonjava.mongo.dto; import org.springframework.data.annotation.Id; import org.springframework.data.mongodb.core.mapping.Document; /** * @author Dinesh Rajput * */ @Document(collection = "dojCollection") public class Employee { @Id private int empId; private String empName; private long salary; private int empAge; public int getEmpId() { return empId; } public void setEmpId(int empId) { this.empId = empId; } public String getEmpName() { return empName; } public void setEmpName(String empName) { this.empName = empName; } public long getSalary() { return salary; } public void setSalary(long salary) { this.salary = salary; } public int getEmpAge() { return empAge; } public void setEmpAge(int empAge) { this.empAge = empAge; } @Override public String toString() { return "Employee [age=" + empAge + ", empName=" + empName + ", empId=" + empId + ", salary=" + salary + "]"; } }Now if you look at the class more closely you will see some Spring Data specific annotations like @Id and @Document . The @Document annotation identifies a domain object that is going to be persisted to MongoDB. Now that we have a persistable domain object we can move on to the real interaction.
package com.dineshonjava.mongo.main; import java.util.List; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.data.mongodb.core.MongoOperations; import org.springframework.stereotype.Repository; import com.dineshonjava.mongo.dto.Employee; /** * @author Dinesh Rajput * */ @Repository public class HelloMongoDB { @Autowired MongoOperations mongoOperations; public void execute() { if (mongoOperations.collectionExists(Employee.class)) { mongoOperations.dropCollection(Employee.class); } mongoOperations.createCollection(Employee.class); Employee employee = new Employee(); employee.setEmpId(1001); employee.setEmpName("Dinesh Rajput"); employee.setSalary(70000); employee.setEmpAge(26); mongoOperations.insert(employee); List<Employee> results = mongoOperations.findAll(Employee.class); System.out.println("Results: " + results); } }
package com.dineshonjava.mongo.main; import org.springframework.context.ConfigurableApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; /** * @author Dinesh Rajput * */ public class HelloMongoTestApp { /** * @param args */ public static void main(String[] args) { ConfigurableApplicationContext context = new ClassPathXmlApplicationContext("mongo-config.xml"); HelloMongoDB hello = (HelloMongoDB) context.getBean("helloMongoDB"); hello.execute(); System.out.println( "DONE!" ); } }
Labels: mongodb