We used cookies to ensure that we give you the best experience on our website. If you continue to use this site we will assume that you are happy with it. What For?

« Back to Blogs

Liferay Spring MVC MongoDB Portlet

Nowadays MongoDB is most widely used NoSQL database. Like other database systems, MongoDB also have different drivers API for different platforms like java and php which allows application to communicate with MongoDB.

 

MongoDB provides Mongo Java Driver which allows java applications to communicate with MongoDB. This is pure java implementation driver we can use in any java application to communicate with MongoDB.

 

To perform database operations with MongoDB, Spring provides Spring Data MongoDB java library. Spring Data MongoDB uses Mongo Java Driver in their implementation.

 

Here, I am going to show how to establish a connection and perform database operation in MongoDB in a Spring MVC Liferay portlet. I have used liferay 6.2 CE GA4 Portal for this example.

 

Let me start with creating Spring MVC Liferay Portlet that uses a JAR File for database operation. This component contains the code that interacts with MongoDB. You can find the component JAR inside portlet WAR WEB-INF/lib directory.

 

Prerequisite :

  • Knowledge of Spring MVC Liferay portlet and basic knowledge of MongoDB is required to understand this blog.

  • I am assuming that MongoDB is running on the system and ready to use.


To install MongoDB refer : MongoDB installation guide

 

Step : 1 - Add dependency for MongoDB in your pom.xml.

 

<dependency>

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

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

<version>1.5.2.RELEASE</version>

</dependency>

 

Step : 2 - Create Entity Class.


In this example, I am going to persist Employee object to MongoDB.

 

Employee class should look like this :

 

@Document

public class Employee implements Serializable {

 

@Id

private String id;

private String name;

private String address;

private String age;

 

//getter, setter, toString, Constructors

}

 

Here you can see that, we used some Spring specific annotations like @Id and @Document.

@Id will be mapped to the '_id' column in database. A property or field without @Id annotation but named id will be mapped to the '_id' column in database. The @Document annotation identifies an object that is going to be persisted in MongoDB. Now, as we have a persistable object, we can move on to the real interaction.

Step : 3 - Load MongoDB configurations included in spring configuration file.

For connectivity with MongoDB, we can make use of Spring Data MongoTemplate class, for that load configuration file from web.xml

 

<context-param>

<param-name>contextConfigLocation</param-name>

<param-value>/WEB-INF/applicationContext.xml</param-value>

</context-param>

 

Now add mongodb configuration to applicationContext.xml.

<!-- configuration for connection to MongoDB -->

<mongo:mongo host="localhost" port="27017" />

<mongo:db-factory dbname="yourdbname" />

 

<!-- MongoTemplate for quering the documents in the database -->

<bean id="mongoTemplate" class="org.springframework.data.mongodb.core.MongoTemplate">

<constructor-arg name="mongoDbFactory" ref="mongoDbFactory" />

</bean>

 

Above mentioned lines will load MongoTemplate bean and we are able to use it by just autowiring it.

 

MongoTemplate is the central class of the Spring’s MongoDB support providing a rich feature set to interact with the database. The template offers a convenience operations to create, update, delete and query for MongoDB documents and provides a mapping between your domain objects and MongoDB documents.

 

Step : 4 - Perform Database operation using MongoTemplate.

 

Following is the EmployeeDAOImpl class from this component.

Here is how you can autowire it and use it for database operations :

public class EmployeeDAOImpl implements IEmployeeDAO {

 

@Autowired

private MongoTemplate mongoTemplate;

 

@Override

public void save(Employee employee) {

mongoTemplate.save(employee);

}

 

@Override

public Employee getById(String id) {

return mongoTemplate.findById(id, Employee.class);

}

 

@Override

public List<Employee> getAll() {

return mongoTemplate.findAll(Employee.class);

}

 

@Override

public void delete(Employee employee) {

mongoTemplate.remove(employee);

}

 

@Override

public void update(Employee employee) {

mongoTemplate.save(employee);

}

 

}

 

Download :

EmployeeMongoDB-portlet(WAR)

 

For more implementation details or support you may contact us at [email protected].

contact-us Request a callback WhatsApp