Tuesday, 24 November 2015

Spring and Mongodb Integration


Step-1

Add below dependency  inside the pom.xml

                 <dependency>
<groupId>org.springframework.data</groupId>
<artifactId>spring-data-mongodb</artifactId>
<version>1.2.0.RELEASE</version>
</dependency>

Step-2
Annotate the pojo used in mongodb as shown below

@Document(collection="person")
public class Person {

@Id
private String id;
private String name;

public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}

}


Step-3
Define "mongoTemplate"  as shown below

<!-- Factory bean that creates the Mongo instance -->
<bean id="mongo" class="org.springframework.data.mongodb.core.MongoFactoryBean">
<property name="host" value="localhost" />
</bean>

<!-- MongoTemplate for connecting and quering the documents in the database -->
<bean id="mongoTemplate" class="org.springframework.data.mongodb.core.MongoTemplate">
<constructor-arg name="mongo" ref="mongo" />
<constructor-arg name="databaseName" value="synergy-bank-app" />
</bean>

<!-- Use this post processor to translate any MongoExceptions thrown in @Repository annotated classes -->

<bean class="org.springframework.dao.annotation.PersistenceExceptionTranslationPostProcessor" />


Step-4
autowired above mongoTemplate inside the dao layer.............


@Repository
public class PersonService {

@Autowired
private MongoTemplate mongoTemplate;

public static final String COLLECTION_NAME = "person";

public void addPerson(Person person) {
if (!mongoTemplate.collectionExists(Person.class)) {
mongoTemplate.createCollection(Person.class);
}
person.setId(UUID.randomUUID().toString());
mongoTemplate.insert(person, COLLECTION_NAME);
}

public List<Person> listPerson() {
return mongoTemplate.findAll(Person.class, COLLECTION_NAME);
}

public List<Person> findPersonByName(String name) {
Query query = new Query();
query.addCriteria(Criteria.where("name").is(name));
return mongoTemplate.find(query, Person.class, COLLECTION_NAME);
}


public void deletePerson(Person person) {
mongoTemplate.remove(person, COLLECTION_NAME);
}

public void updatePerson(Person person) {
mongoTemplate.insert(person, COLLECTION_NAME);
}

public static void main(String[] args) {
System.out.println(UUID.randomUUID().toString());
}

}


Command to start the mongodb
D:\mongodb\bin>mongod -d dbpath=c:\mongodb\data

dbpath= must be there to start the server


Microsoft Windows [Version 6.2.9200]
(c) 2012 Microsoft Corporation. All rights reserved.

C:\Users\cool>d:

D:\>cd mongodb

D:\mongodb>cd bin

D:\mongodb\bin>mongo.exe
MongoDB shell version: 2.6.11-pre-
connecting to: test
Welcome to the MongoDB shell.
For interactive help, type "help".
For more comprehensive documentation, see
        http://docs.mongodb.org/
Questions? Try the support group
        http://groups.google.com/group/mongodb-user
> show dbs;
admin  (empty)
local  0.078GB
> use nagen
switched to db nagen
> db
nagen
> show dbs;
admin  (empty)
local  0.078GB
> db
nagen
> show dbs;
admin  (empty)
local  0.078GB
> show dbs;
admin             (empty)
local             0.078GB
synergy-bank-app  0.078GB
> show dbs;
admin             (empty)
local             0.078GB
synergy-bank-app  0.078GB
> show dbs;
admin             (empty)
local             0.078GB
synergy-bank-app  0.078GB
> use synergy-bank-app
switched to db synergy-bank-app
>




No comments:

Post a Comment