MongoDB (from "humongous") is a scalable, high-performance, open source, document-oriented database. Written in C++, MongoDB features:
- 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.
package com.dineshonjava.model; import java.io.Serializable; import org.springframework.data.mongodb.core.mapping.Document; /** * A simple POJO representing a Employee * @author Dinesh Rajput * */ @Document(collection="employee") public class Employee implements Serializable{ private static final long serialVersionUID = -723583058586873479L; private String empId; private String empName; private String empAddress; private String salary; private String empAge; public String getEmpId() { return empId; } public void setEmpId(String empId) { this.empId = empId; } public String getEmpName() { return empName; } public void setEmpName(String empName) { this.empName = empName; } public String getEmpAddress() { return empAddress; } public void setEmpAddress(String empAddress) { this.empAddress = empAddress; } public String getSalary() { return salary; } public void setSalary(String salary) { this.salary = salary; } public String getEmpAge() { return empAge; } public void setEmpAge(String empAge) { this.empAge = empAge; } }We use the @Document and @Id annotation to indicate any object that we want to treat as a Document, and the ID.
package com.dineshonjava.bean; /** * @author Dinesh Rajput * */ public class EmployeeBean { private String id; private String name; private String age; private String salary; private String address; 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; } public String getAge() { return age; } public void setAge(String age) { this.age = age; } public String getSalary() { return salary; } public void setSalary(String salary) { this.salary = salary; } public String getAddress() { return address; } public void setAddress(String address) { this.address = address; } }Data Access Layer-
package com.dineshonjava.service; import java.util.List; import com.dineshonjava.model.Employee; /** * @author Dinesh Rajput * */ public interface EmployeeService { public Boolean addEmployee(Employee employee); public List<Employee> listEmployeess(); public Employee getEmployee(String empid); public Boolean deleteEmployee(String empid); }
package com.dineshonjava.service; import java.util.List; import java.util.UUID; import org.apache.log4j.Logger; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.data.document.mongodb.MongoTemplate; import org.springframework.stereotype.Service; import org.springframework.transaction.annotation.Transactional; import com.dineshonjava.model.Employee; import com.mongodb.BasicDBObject; import com.mongodb.DBObject; /** * Service for processing {@link Employee} objects. * Uses Spring's {@link MongoTemplate} to perform CRUD operations. * * For a complete reference to MongoDB * see http://www.mongodb.org/ * * For a complete reference to Spring Data MongoDB * see http://www.springsource.org/spring-data * * @author Dinesh Rajput * */ @Service("employeeService") @Transactional public class EmployeeServiceImpl implements EmployeeService { @Autowired(required=true) private MongoTemplate mongoTemplate; // // @Autowired // private MongoOperations mongoOperations; protected static Logger logger = Logger.getLogger("service"); /** * Retrieves all Employees */ public List<Employee> listEmployeess() { logger.debug("Retrieving all Employees"); // Execute the query and find all matching entries List<Employee> employees = mongoTemplate.findAll(Employee.class); return employees; } /** * Retrieves a single Employee */ public Employee getEmployee(String empid ) { logger.debug("Retrieving an existing Employee"); Employee employee = new Employee(); // Find an entry where empid matches the id DBObject query = new BasicDBObject(); query.put("empId", empid); DBObject cursor = mongoTemplate.getDb().getCollection("employee").findOne(query); employee.setEmpId(cursor.get("empId").toString()); employee.setEmpName(cursor.get("empName").toString()); employee.setEmpAge(cursor.get("empAge").toString()); employee.setSalary(cursor.get("salary").toString()); employee.setEmpAddress(cursor.get("empAddress").toString()); return employee; } /** * Adds a new Employee */ public Boolean addEmployee(Employee employee) { logger.debug("Adding a new employee"); try { if(employee.getEmpId() != null && employee.getEmpId() != ""){ // Find an entry where empid matches the id DBObject query1 = new BasicDBObject(); query1.put("empId", employee.getEmpId()); DBObject query = new BasicDBObject(); query.put("empId", employee.getEmpId()); query.put("empName", employee.getEmpName()); query.put("empAge", employee.getEmpAge()); query.put("salary", employee.getSalary()); query.put("empAddress", employee.getEmpAddress()); mongoTemplate.getDb().getCollection("employee").update(query1, query); }else{ // Set a new value to the empid property first since it's blank employee.setEmpId(UUID.randomUUID().toString()); // Insert to db mongoTemplate.save(employee); } return true; } catch (Exception e) { logger.error("An error has occurred while trying to add new employee", e); return false; } } /** * Deletes an existing employee */ public Boolean deleteEmployee(String empid) { logger.debug("Deleting existing employee"); try { // Find an entry where empid matches the id DBObject query = new BasicDBObject(); query.put("empId", empid); // Run the query and delete the entry mongoTemplate.getDb().getCollection("employee").findAndRemove(query); return true; } catch (Exception e) { logger.error("An error has occurred while trying to delete new employee", e); return false; } } }
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:p="http://www.springframework.org/schema/p" xmlns:mongo="http://www.springframework.org/schema/data/mongo" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd http://www.springframework.org/schema/data/mongo http://www.springframework.org/schema/data/mongo/spring-mongo-1.0.xsd"> <!-- Default bean name is 'mongo' --> <mongo:mongo host="localhost" port="27017"/> <!-- Default bean name is 'mongo' --> <mongo:mongo> <mongo:options connections-per-host="100" threads-allowed-to-block-for-connection-multiplier="5" max-wait-time="120000000" connect-timeout="10000000" socket-keep-alive="true" socket-timeout="15000000" auto-connect-retry="true"/> </mongo:mongo> <!-- Offers convenience methods and automatic mapping between MongoDB JSON documents and your domain classes. --> <bean id="mongoTemplate" class="org.springframework.data.mongodb.core.MongoTemplate"> <constructor-arg ref="mongo"/> <constructor-arg name="databaseName" value="mydb"/> </bean> </beans>Notice we're using the mongo namespace:
<mongo:mongo host="localhost" port="27017"/>Then we declared a MongoTemplate that references a MongoDB database (mongo), a database (dineshonjavadb):
The Controller Layer-<constructor-arg ref="mongo"/> <constructor-arg name="databaseName" value="dineshonjavadb"/>
package com.dineshonjava.controller; import java.util.ArrayList; import java.util.HashMap; import java.util.List; import java.util.Map; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Controller; import org.springframework.validation.BindingResult; import org.springframework.web.bind.annotation.ModelAttribute; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestMethod; import org.springframework.web.servlet.ModelAndView; import com.dineshonjava.bean.EmployeeBean; import com.dineshonjava.model.Employee; import com.dineshonjava.service.EmployeeService; /** * @author Dinesh Rajput * */ @Controller public class EmployeeController { @Autowired private EmployeeService employeeService; @RequestMapping(value = "/save", method = RequestMethod.POST) public ModelAndView saveEmployee(@ModelAttribute("command") EmployeeBean employeeBean, BindingResult result) { Employee employee = prepareModel(employeeBean); employeeService.addEmployee(employee); return new ModelAndView("redirect:/add.html"); } @RequestMapping(value="/employees", method = RequestMethod.GET) public ModelAndView listEmployees() { Map<String, Object> model = new HashMap<String, Object>(); model.put("employees", prepareListofBean(employeeService.listEmployeess())); return new ModelAndView("employeesList", model); } @RequestMapping(value = "/add", method = RequestMethod.GET) public ModelAndView addEmployee(@ModelAttribute("command") EmployeeBean employeeBean,BindingResult result) { Map<String, Object> model = new HashMap<String, Object>(); model.put("employees", prepareListofBean(employeeService.listEmployeess())); return new ModelAndView("addEmployee", model); } @RequestMapping(value = "/index", method = RequestMethod.GET) public ModelAndView welcome() { return new ModelAndView("index"); } @RequestMapping(value = "/delete", method = RequestMethod.GET) public ModelAndView editEmployee(@ModelAttribute("command") EmployeeBean employeeBean, BindingResult result) { employeeService.deleteEmployee(employeeBean.getId()); Map<String, Object> model = new HashMap<String, Object>(); model.put("employee", null); model.put("employees", prepareListofBean(employeeService.listEmployeess())); return new ModelAndView("addEmployee", model); } @RequestMapping(value = "/edit", method = RequestMethod.GET) public ModelAndView deleteEmployee(@ModelAttribute("command") EmployeeBean employeeBean, BindingResult result) { Map<String, Object> model = new HashMap<String, Object>(); model.put("employee", prepareEmployeeBean(employeeService.getEmployee(employeeBean.getId()))); model.put("employees", prepareListofBean(employeeService.listEmployeess())); return new ModelAndView("addEmployee", model); } private Employee prepareModel(EmployeeBean employeeBean){ Employee employee = new Employee(); employee.setEmpAddress(employeeBean.getAddress()); employee.setEmpAge(employeeBean.getAge().toString()); employee.setEmpName(employeeBean.getName()); employee.setSalary(employeeBean.getSalary().toString()); employee.setEmpId(employeeBean.getId().toString()); employeeBean.setId(null); return employee; } private List<EmployeeBean> prepareListofBean(List<Employee> employees){ List<EmployeeBean> beans = null; if(employees != null && !employees.isEmpty()){ beans = new ArrayList<EmployeeBean>(); EmployeeBean bean = null; for(Employee employee : employees){ bean = new EmployeeBean(); bean.setName(employee.getEmpName()); bean.setId(employee.getEmpId()); bean.setAddress(employee.getEmpAddress()); bean.setSalary(employee.getSalary().toString()); bean.setAge(employee.getEmpAge()); beans.add(bean); } } return beans; } private EmployeeBean prepareEmployeeBean(Employee employee){ EmployeeBean bean = new EmployeeBean(); bean.setAddress(employee.getEmpAddress()); bean.setAge(employee.getEmpAge()); bean.setName(employee.getEmpName()); bean.setSalary(employee.getSalary()); bean.setId(employee.getEmpId()); return bean; } }Other Configurations and Files-
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context" xmlns:tx="http://www.springframework.org/schema/tx" xsi:schemaLocation=" http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.0.xsd"> <import resource="mongo-config.xml"/> <context:component-scan base-package="com.dineshonjava" /> <!-- <tx:annotation-driven transaction-manager="hibernateTransactionManager"/> --> <bean id="jspViewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver"> <property name="viewClass" value="org.springframework.web.servlet.view.JstlView" /> <property name="prefix" value="/WEB-INF/views/" /> <property name="suffix" value=".jsp" /> </bean> </beans>web.xml
<?xml version="1.0" encoding="UTF-8"?> <web-app version="2.5" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"> <servlet> <servlet-name>sdnext</servlet-name> <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> <init-param> <param-name>contextConfigLocation</param-name> <param-value>/WEB-INF/config/sdnext-servlet.xml</param-value> </init-param> <load-on-startup>1</load-on-startup> </servlet> <servlet-mapping> <servlet-name>sdnext</servlet-name> <url-pattern>*.html</url-pattern> </servlet-mapping> <welcome-file-list> <welcome-file>index.html</welcome-file> </welcome-file-list> </web-app>
Labels: Spring MVC, Spring3.0