Searching...
Sunday, 17 November 2013

Spring Data Mongodb Insert List of Objects Example

08:29

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 insert list of objects using SpringData 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 (Customer.java)

package com.pretech;
public class Customer {
	public String name;
	public String address;
	public Customer(String name, String address) {
		super();
		this.name = name;
		this.address = address;
	}
	public Customer() {
	}
	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 "Customer [name=" + name + ", address=" + address + "]";
	}
}

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/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">
       <!-- 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 Insert Customer details

package com.pretech;
import java.util.ArrayList;
import java.util.List;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.GenericXmlApplicationContext;
import org.springframework.data.document.mongodb.MongoOperations;
public class SpringDataListInsertExample {
	public static void main(String args[]) {
		ApplicationContext ctx = new GenericXmlApplicationContext(
				"mongo-context.xml");
		MongoOperations mongoOperation = (MongoOperations) ctx
				.getBean("customermongoTemplate");
		Customer customer1 = new Customer("Sujay", "Bangalore");
		Customer customer2 = new Customer("Raghav", "Bangalore");
		Customer customer3 = new Customer("Shiva", "Bangalore");
		Customer customer4 = new Customer("Apar", "Bangalore");
		Customer customer5 = new Customer("Vijay", "Bangalore");
		List<Customer> customerList = new ArrayList<Customer>();
		customerList.add(customer1);
		customerList.add(customer2);
		customerList.add(customer3);
		customerList.add(customer4);
		customerList.add(customer5);
		mongoOperation.insertList(customerList);
	}
}

Output



image

0 comments: