In this tutorial we will create a simple Spring MVC 3 application that uses a document-oriented database for its persistence layer. We will be using MongoDB as our database. We will explore and discover how easy it is to integrate MongoDB with Spring MVC 3. Our application is a simple CRUD service for managing a list of Persons. We will provide functionalities for adding, deleting, editing, and viewing of all registered persons.
MongoDB
These are few benefits with MongoDB.
In order to complete this tutorial, you will be have to install a copy of MongoDB. You can download from http://www.mongodb.org/display/DOCS/Quickstart. The installation is really easy.
MongoDB is used if there are more number of write than reads. Some thing like Audit logs which will have frequent writes than reads and also huge data will be stored.
pom.xml
(download source code link is provided for complete project access)
Lets start with MongoDBFactory.
MongoDBFactory.java
method "getMongo()" will be used to get Mongo DB reference using host and port.
method "getDB()" Will be used to get reference to your DB created. This is some thing like MySql
"create database myDatabase". This method is trying to get "myDatabase" from MongoDB.
method "getCollection()" collections are like tables in mongodb stored as key value pairs.
Instead of factory if you want to write the same code it will look like below.
Now our schema is based on Person class. Below is the class Person we like to store in the DB.
Person.java
Now "PersonService" java class to do CRUD operations.
PersonService.java
This service defines our basic CRUD system. We have the following public methods:
getAll() - for retrieving all persons
edit() - for editing
delete() - for deleting
add() - for adding
get() - for retrieving single person
We also have the following private methods:
init() - for initializing our database
getDBObject() - for retrieving a single Mongo object
The database is initialized once in the PersonService's constructor through the init() method:
editedpage.jsp
Spring MVC Configuration xmls are defined below.
web.xml
<servlet>
MongoDB
These are few benefits with MongoDB.
- Document-oriented storage
- Full Index Support
- Replication & High Availability
- Scale horizontally without compromising functionality.
- Rich, document-based queries.
- Atomic modifiers for contention-free performance.
- Flexible aggregation and data processing.
- Store files of any size without complicating your stack.
- Enterprise class support, training, and consulting available.
In order to complete this tutorial, you will be have to install a copy of MongoDB. You can download from http://www.mongodb.org/display/DOCS/Quickstart. The installation is really easy.
MongoDB is used if there are more number of write than reads. Some thing like Audit logs which will have frequent writes than reads and also huge data will be stored.
pom.xml
(download source code link is provided for complete project access)
Lets start with MongoDBFactory.
MongoDBFactory.java
method "getDB()" Will be used to get reference to your DB created. This is some thing like MySql
"create database myDatabase". This method is trying to get "myDatabase" from MongoDB.
method "getCollection()" collections are like tables in mongodb stored as key value pairs.
Instead of factory if you want to write the same code it will look like below.
// Retrieve a db
Mongo m =
new
Mongo(
"localhost"
,
27017
);
m.getDB(
"mydb"
);
// where mydb is the database name
// Retrieve a collectionn
Mongo m =
new
Mongo(
"localhost"
,
27017
);
DB db = m.getDB(
"mydb"
);
db.getCollection(
"mycollection"
);
// where mycollection is the collection name
The same you can see in command prompt below.
Now our schema is based on Person class. Below is the class Person we like to store in the DB.
Person.java
Now "PersonService" java class to do CRUD operations.
PersonService.java
getAll() - for retrieving all persons
edit() - for editing
delete() - for deleting
add() - for adding
get() - for retrieving single person
We also have the following private methods:
init() - for initializing our database
getDBObject() - for retrieving a single Mongo object
The database is initialized once in the PersonService's constructor through the init() method:
private
void
init() {
// Populate our MongoDB database
logger.debug(
"Init MongoDB users"
);
// Drop existing collection
MongoDBFactory.getCollection(
"mydb"
,
"mycollection"
).drop();
// Retrieve collection. If not existing, create a new one
DBCollection coll = MongoDBFactory.getCollection(
"mydb"
,
"mycollection"
);
// Create new object
BasicDBObject doc =
new
BasicDBObject();
// Generate random id using UUID type 4
doc.put(
"id"
, UUID.randomUUID().toString());
doc.put(
"firstName"
,
"Naresh"
);
doc.put(
"lastName"
,
"Kancharla"
);
doc.put(
"money"
,
1000
);
coll.insert(doc);
// Create new object
doc =
new
BasicDBObject();
// Generate random id using UUID type 4
doc.put(
"id"
, UUID.randomUUID().toString());
doc.put(
"firstName"
,
"Keerthi"
);
doc.put(
"lastName"
,
"Kancharla"
);
doc.put(
"money"
,
2000
);
coll.insert(doc);
// Create new object
doc =
new
BasicDBObject();
// Generate random id using UUID type 4
doc.put(
"id"
, UUID.randomUUID().toString());
doc.put(
"firstName"
,
"Kanish"
);
doc.put(
"lastName"
,
"Gupta"
);
doc.put(
"money"
,
3000
);
coll.insert(doc);
}
If you Notice we're creating a dynamic JSON schema here with the following format:
{
id:'',
firstName:'',
lastName:'',
money:''
}
To run this flow we need Spring MVC Controller.
To view complete code below i provided GitHub source code link.
This controller declares the following mappings:
/persons - for retrieving all persons
/persons/add (GET) - displays the Add New form
/persons/add (POST) - saves the new person
/persons/delete - deletes an existing person
/persons/edit (GET) - displays the Edit form
/persons/edit (POST) - saves the edited person
/persons - for retrieving all persons
/persons/add (GET) - displays the Add New form
/persons/add (POST) - saves the new person
/persons/delete - deletes an existing person
/persons/edit (GET) - displays the Edit form
/persons/edit (POST) - saves the edited person
Each mapping delegates the call to the PersonService.
When the PersonService is done processing, the controller then forwards the request to a JSP page
that displays a confirmation message. Here are the JSP pages.
personpage.jsp
editpage.jsp
addpage.jsp
addedpage.jsp
deletedpage.jspSpring MVC Configuration xmls are defined below.
web.xml
Above web.xml we defined DispatcherServlet as "spring" so there should be a xml with name "spring-servlet.xml" for view resolution.
spring-servlet.xml
<!-- Declare a view resolver -->
<bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver"
p:prefix="/" p:suffix=".jsp" />
applicationContext.xml
<!-- Activates various annotations to be detected in bean classes -->
<context:annotation-config />
<!-- Scans the classpath for annotated components that will be auto-registered as Spring beans.
For example @Controller and @Service. Make sure to set the correct base-package-->
<context:component-scan base-package="*" />
<!-- Configures the annotation-driven Spring MVC Controller programming model.
Note that, with Spring 3.0, this tag works in Servlet MVC only! -->
<mvc:annotation-driven />
When the application is initially run, we mentioned that it will drop and create a new collection. So MongoDB's console should reflect this action as well.
Now let's access the main page that shows all registered persons. To access the main page, enter the following URL in your browser:
http://localhost:8080/SpringMongoDB/main/persons
Here's what you should see:
Here's the log from MongoDB's console: Fri Oct 26 20:32:08 [initandlisten] connection accepted from 127.0.0.1:51259 #6 (3 connections now open)
Let's edit the first record in our list by clicking the Edit link. We will be redirected to the Edit Person page:
Here's the log from MongoDB's console:
Fri Oct 26 20:34:40 [conn8] update mydb.mycollection query: { _id: ObjectId('508aa5ecad8450d889bc351a'), id: "1d11324e-ee91-4e50-84ef-b7e5e141
, firstName: "Keerthi", lastName: "K", money: 2500.0 } nscanned:1 nupdated:1 keyUpdates:0 locks(micros) w:107793 108ms
After editing the person and submitting the changes, we should see the following page:
That's it. We've managed to create a simple Spring MVC 3 application that uses MongoDB
for its database. We've also seen the benefits of a document-oriented database
when we created the schema dynamically.
Download the source code: