Searching...
Saturday, 9 November 2013

Spring Data MongoDB Simple Save Example

08:27

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 save and retrieve Customer bean using SpringData and Mongodb.

Prerequisites: Mongodb should be up and running

1. Create a Maven Project
Create a Maven project and add below dependencies

<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>
    <repositories>
        <repository>
            <id>spring-milestone</id>
            <name>Spring Maven MILESTONE Repository</name>
            <url>http://maven.springframework.org/milestone</url>
        </repository>
    </repositories>
 

2. Create Customer entity

package com.vinod.model;

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 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 save Customer details using spring data

package com.vinod.service;

import java.util.Iterator;
import java.util.List;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.GenericXmlApplicationContext;
import org.springframework.data.document.mongodb.MongoOperations;

import com.vinod.model.Customer;

public class SpringDataInsert {
    public static void main(String args[]) {
        ApplicationContext ctx = new GenericXmlApplicationContext("application-context.xml");
        MongoOperations mongoOperation = (MongoOperations) ctx.getBean("customermongoTemplate");
        Customer customer = new Customer("Vinod", "Bangalore");
        mongoOperation.save("customerDetails", customer);
        // List
        List<Customer> listCustomer = mongoOperation.getCollection("customerDetails", Customer.class);
        Iterator<Customer> iterator = listCustomer.iterator();
        while (iterator.hasNext()) {
            System.out.println(iterator.next());
        }
    }
}
 

5. Output

Customer [name=Vinod, address=Bangalore]

6. Done!! Download example

https://github.com/kkvinodkumaran/myrepository/tree/master/vinod-spring-data

0 comments: