In this Post we will see how to Integrate Spring Data + MongoDB + Maven step by step.
To integrate Spring with MongoDB we require Spring Data and Mongo DB jars.
To integrate Spring with MongoDB we require Spring Data and Mongo DB jars.
Below is the Content for Pom.xml:
Below Pom.xml Contains the Spring and MongoDB maven dependency configuration.
<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/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.mahesh.common</groupId>
<artifactId>SpringMongoDBExample</artifactId>
<packaging>jar</packaging>
<version>1.0</version>
<name>SpringMongoExample</name>
<url>http://maven.apache.org</url>
<dependencies>
<!-- Spring framework -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-core</artifactId>
<version>3.2.2.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>3.2.2.RELEASE</version>
</dependency>
<!-- mongodb java driver -->
<dependency>
<groupId>org.mongodb</groupId>
<artifactId>mongo-java-driver</artifactId>
<version>2.11.0</version>
</dependency>
<!-- Spring data mongodb -->
<dependency>
<groupId>org.springframework.data</groupId>
<artifactId>spring-data-mongodb</artifactId>
<version>1.2.0.RELEASE</version>
</dependency>
<dependency>
<groupId>cglib</groupId>
<artifactId>cglib</artifactId>
<version>2.2.2</version>
</dependency>
</dependencies>
<build>
<finalName>SpringMongoDBExample</finalName>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>2.0.2</version>
<configuration>
<source>1.6</source>
<target>1.6</target>
</configuration>
</plugin>
</plugins>
</build>
</project>
Below is the Content for HelloWorldExample.java
package com.mahesh.spring;
import java.util.List;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import org.springframework.data.mongodb.core.MongoOperations;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.data.mongodb.core.query.Update;
import com.mahesh.config.SpringMongoConfig;
import com.mahesh.model.User;
//import org.springframework.context.support.GenericXmlApplicationContext;
public class HelloWorldExample {
public static void main(String[] args) {
// For XML
//ApplicationContext ctx = new GenericXmlApplicationContext("SpringConfig.xml");
// For Annotation
ApplicationContext ctx = new AnnotationConfigApplicationContext(SpringMongoConfig.class);
MongoOperations mongoOperation = (MongoOperations) ctx.getBean("mongoTemplate");
User user = new User("mahesh", "password123");
// save
mongoOperation.save(user);
// now user object got the created id.
System.out.println("1. user : " + user);
// query to search user
Query searchUserQuery = new Query(Criteria.where("username").is("mahesh"));
// find the saved user again.
User savedUser = mongoOperation.findOne(searchUserQuery, User.class);
System.out.println("2. find - savedUser : " + savedUser);
// update password
mongoOperation.updateFirst(searchUserQuery, Update.update("password", "new password"),
User.class);
// find the updated user object
User updatedUser = mongoOperation.findOne(
new Query(Criteria.where("username").is("mahesh")), User.class);
System.out.println("3. updatedUser : " + updatedUser);
// delete
//mongoOperation.remove(searchUserQuery, User.class);
// List, it should be empty now.
List<User> listUser = mongoOperation.findAll(User.class);
System.out.println("4. Number of user = " + listUser.size());
}
}
Below is the Content for SpringMongoConfig.java
package com.mahesh.config;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.mongodb.MongoDbFactory;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.SimpleMongoDbFactory;
import com.mongodb.MongoClient;
@Configuration
public class SpringMongoConfig {
public @Bean
MongoDbFactory mongoDbFactory() throws Exception {
return new SimpleMongoDbFactory(new MongoClient(), "mahesh");
}
public @Bean
MongoTemplate mongoTemplate() throws Exception {
MongoTemplate mongoTemplate = new MongoTemplate(mongoDbFactory());
return mongoTemplate;
}
}
Below is the Content for Spring Configuration file SpringConfig.xml
<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/context
http://www.springframework.org/schema/context/spring-context-3.0.xsd
http://www.springframework.org/schema/data/mongo
http://www.springframework.org/schema/data/mongo/spring-mongo-1.0.xsd
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd">
<mongo:mongo host="127.0.0.1" port="27017" />
<mongo:db-factory dbname="mahesh" />
<bean id="mongoTemplate" class="org.springframework.data.mongodb.core.MongoTemplate">
<constructor-arg name="mongoDbFactory" ref="mongoDbFactory" />
</bean>
</beans>
OutPut:
1. user : User [id=53ff4fbd1da74f81ce7b5877, username=mahesh, password=password123]
2. find - savedUser : User [id=53ff4f2f1da7367f5fc43e0f, username=mahesh, password=new password]
3. updatedUser : User [id=53ff4f2f1da7367f5fc43e0f, username=mahesh, password=new password]
4. Number of user = 2
No comments:
Post a Comment