Anda di halaman 1dari 3

Spring MVC and MongoDB

In this blogs we will discussed about spring MVC and MongoDB and also define difference
between Spring and MongoDB. You should 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.
3.
4.
5.
6.
7.
8.
9.
10.
11.
12.
13.
14.
15.
16.
17.
18.
19.
20.
21.
22.

<dependencies>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-orm</artifactId>
<version>3.2.0.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>3.2.0.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework.data</groupId>
<artifactId>spring-data-mongodb</artifactId>
<version>1.2.0.RELEASE</version>
</dependency>
<dependency>
<groupId>jstl</groupId>
<artifactId>jstl</artifactId>
<version>1.2</version>
</dependency>
</dependencies>

Create model class : Customer.java


1.
2.
3.
4.
5.
6.
7.
8.
9.
10.
11.
12.
13.
14.
15.
16.
17.
18.
19.
20.
21.
22.
23.
24.

package com.evon.model;
import org.springframework.data.annotation.Id;
import org.springframework.data.mongodb.core.mapping.Document;
@Document
public class Customer {
@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;
}
}

Create DAO interface : CustomerDAO.java


1.
2.
3.
4.
5.
6.

package com.evon.service;
import java.util.List;
import com.evon.model.Customer;
public interface CustomerDAO {
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.

Anda mungkin juga menyukai