Issuu on Google+

Spring MVC and MongoDB In this blogs we will discussed about spring MVC and MongoDB and also define differenece between Spring and MongoDB. You sould read this blogs for full details. Repository Class Using MongoDB : Spring has the capability to create loose coupling application's layers. To dealing with database we create a DAO layer. In Spring DAO layer contains the Repository class this class is responsible for all database related operations. In this example we will use the @Repository annotaion to lable the Repository class.

MongoDB is different kind of database, it is scalable, NoSQL Document Oriented database. MongoDB holds data in the form of document and collections. If we compare the MongoDb to other Relational databases then we can say that the document in the MongoDB is like a row in Relational database and collection is like a table which holds the collection of documents.


Document is like a key-value information. Document holds the all information of a single entity like a row in relational database. Key in the documents are called field. Collection is a group of documents. It is like a table in relational database.

Spring provides the MongoTemplate class to handle the database related operations. This class will provides the important functions like save(), insert(), findAll(), remove () etc. which facilitate the developer to use the MongoDB without any problem. The following sample code describes some of the functions. Example of Spring MVC and MongoDB : Use following dependencies in pom.xml 1. 2.

<dependencies> <dependency>

3.

<groupId>org.springframework</groupId>

4.

<artifactId>spring-orm</artifactId>

5.

<version>3.2.0.RELEASE</version>


6.

</dependency>

7.

<dependency>

8.

<groupId>org.springframework</groupId>

9.

<artifactId>spring-webmvc</artifactId>

10.

<version>3.2.0.RELEASE</version>

11.

</dependency>

12.

<dependency>

13.

<groupId>org.springframework.data</groupId>

14.

<artifactId>spring-data-mongodb</artifactId>

15.

<version>1.2.0.RELEASE</version>

16.

</dependency>

17.

<dependency>

18.

<groupId>jstl</groupId>

19.

<artifactId>jstl</artifactId>

20.

<version>1.2</version>

21. 22.

</dependency> </dependencies>

Create model class : Customer.java 1.

package com.evon.model;

2.

import org.springframework.data.annotation.Id;


3.

import org.springframework.data.mongodb.core.mapping.Document;

4. 5.

@Document

6.

public class Customer {

7. 8.

@Id

9.

private String id;

10.

private String name;

11. 12.

public String getId() { return id;

13. 14.

}

15.

public void setId(String id) { this.id = id;

16. 17.

}

18.

public String getName() { return name;

19. 20.

}

21.

public void setName(String name) {


this.name = name;

22. }

23. 24.

}

Create DAO interface : CustomerDAO.java 1.

package com.evon.service;

2.

import java.util.List;

3.

import com.evon.model.Customer;

4. 5.

public interface CustomerDAO {

6.

Read Full Blogs :- Spring MVC and MongoDB

You can check more informative blogs and tutorials at iphone development blogs section and can also browse the iOS developer forum, Java, PHP, etc. for posting and viewing latest questions on development.


Spring MVC and MongoDB