Searching...
Tuesday, 19 November 2013

Spring Data Mongodb CRUD operation using MongoTemplate

06:00

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 simple CRUD operation using SpringData MongoTemplate.

Prerequisites: Mongodb should up and running

1. Create a Maven Project

Create a Maven project and add below dependencies

<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.pretech</groupId>
	<artifactId>spring-data-fs-example</artifactId>
	<version>0.0.1-SNAPSHOT</version>
	<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.7.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.1.0.RELEASE</version>
		</dependency>
		<dependency>
			<groupId>cglib</groupId>
			<artifactId>cglib</artifactId>
			<version>2.2</version>
		</dependency>
	</dependencies>
</project>   

2. Create a Main class for CRUD operations

package com.pretech;
import org.springframework.data.mongodb.core.MongoTemplate;
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.mongodb.Mongo;
public class SpringDataMongoTemplate {
	public static void main(String[] args) {
		Mongo mongo;
		try {
			mongo = new Mongo("localhost");
			MongoTemplate mongoTemplate = new MongoTemplate(mongo,
					"StudentDocuments");
			// Create Collections
			if (!mongoTemplate.collectionExists("Students")) {
				mongoTemplate.createCollection("Students");
			}
			System.out.println("Collection created "+mongoTemplate.getCollectionNames());
			// Insert Student details
			mongoTemplate.save(new Student("Santhosh", "Bangalore"));
			// Select student details
			System.out.println("Student details after insert"
					+ mongoTemplate.findAll(Student.class));
			// Update Student Details
			mongoTemplate.updateMulti(
					new Query(Criteria.where("name").is("Santhosh")),
					Update.update("address", "New Bangalore"), Student.class);
			System.out.println("Student details after update"
					+ mongoTemplate.findAll(Student.class));
			// Delete Student details
			mongoTemplate.remove(new Query(Criteria.where("name")
					.is("Santhosh")),Student.class);
			System.out.println("Student details after Delete"
					+ mongoTemplate.findAll(Student.class));
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
}
class Student {
	String name;
	String address;
	public Student(String name, String address) {
		super();
		this.name = name;
		this.address = address;
	}
	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;
	}
	@Override
	public String toString() {
		return "Student [name=" + name + ", address=" + address + "]";
	}
}

Run it



Console output


Collection created [Students, system.indexes]
Student details after insert[Student [name=Santhosh, address=Bangalore]]
Student details after update[Student [name=Santhosh, address=New Bangalore]]
Student details after Delete[]

0 comments: