Quantcast
Channel: Java tutorial and Java Interview Questions and Answers
Viewing all articles
Browse latest Browse all 154

Spring MVC with Hibernate CRUD Example

$
0
0
      In this post, We can write a simple Spring MVC web application i.e CRUD operation using annotation based Hibernate ORM.  The CRUD operation means Add, Edit, Select and Delete. We can create web application using Spring and Hibernate as follows.

First step is to create the Dynamic Web Project in Eclipse , then configure the web.xml as follows,
The project structure i.e folder structure shown in the below image.

 Project Structure:--

Spring Crud operation project structure

Deployment Descriptor(web.xml)
 

<?xml version="1.0" encoding="UTF-8"?>
<web-appversion="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>appln</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/appln-servlet.xml</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>

<servlet-mapping>
<servlet-name>appln</servlet-name>
<url-pattern>*.html</url-pattern>
</servlet-mapping>

<welcome-file-list>
<welcome-file>add.html</welcome-file>
</welcome-file-list>

</web-app>

       In the above configuration, most important is to give the correct spring bean configuration location otherwise it won't take configuration file. In this example, appln-servlet.xml is the bean configuration file kept inside WEB-INF folder.


Hibernate Entity Bean:--

 In the example, used annotation based Hibernate not xml configuration. The Annotation based Employee bean is as follows,


package com.adnblog.model;

importjava.io.Serializable;

importjavax.persistence.Column;
importjavax.persistence.Entity;
importjavax.persistence.GeneratedValue;
importjavax.persistence.GenerationType;
importjavax.persistence.Id;
importjavax.persistence.Table;

/**
* @author Anil N
*
*/
@Entity
@Table(name="Employee")
publicclassEmployeeimplements Serializable{

privatestaticfinallong serialVersionUID =-723583058586873479L;

@Id
@GeneratedValue(strategy=GenerationType.AUTO)
@Column(name ="emp_id")
private Integer empId;

@Column(name="emp_name")
private String empName;

@Column(name="emp_address")
private String empAddress;

@Column(name="emp_salary")
private Long salary;

@Column(name="emp_age")
private Integer empAge;

public Integer getEmpId(){
return empId;
}

publicvoidsetEmpId(Integer empId){
this.empId= empId;
}

public String getEmpName(){
return empName;
}

publicvoidsetEmpName(String empName){
this.empName= empName;
}

public String getEmpAddress(){
return empAddress;
}

publicvoidsetEmpAddress(String empAddress){
this.empAddress= empAddress;
}

public Long getSalary(){
return salary;
}

publicvoidsetSalary(Long salary){
this.salary= salary;
}

public Integer getEmpAge(){
return empAge;
}

publicvoidsetEmpAge(Integer empAge){
this.empAge= empAge;
}
}

The entity bean Employee maps with database employee table. So I'm creating an employee table is as follows.

employee table - Using PostgreSQL database.

CREATETABLE employee (
emp_id INTEGERPRIMARYKEY,
emp_address CHARACTERVARYING(40),
emp_salary BIGINT,
emp_age INTEGER,
emp_name CHARACTERVARYING(25)
);

create one properties file under resource folder i.e database.properties, there keep all database information as a key value pairs, and this information you can access in the bean configuration file.

database.properties

database.driver=org.postgresql.Driver
database.url=jdbc:postgresql://localhost:5432/hms
database.user=postgres
database.password=admin
hibernate.dialect=org.hibernate.dialect.PostgreSQLDialect
hibernate.show_sql=true
hibernate.hbm2ddl.auto=update

using PostgreSQL as database, you can use any database and configure here properly.


Hibernate DAO Class: - 

          We will create the EmployeeDao interface to declare the methods that are used in the CRUD example. Next we can implement those methods in another class.

EmployeeDao.java


package com.adnblog.dao;

importjava.util.List;

importcom.adnblog.model.Employee;

/**
* @author Anil N
*
*/
publicinterfaceEmployeeDao{

public Integer addEmployee(Employee employee);

public List<Employee>listEmployeess();

public Employee getEmployee(int empid);

publicvoidupdateEmployee(Employee employee);

publicvoiddeleteEmployee(Employee emp);
}

Implementation class i.e EmployeeDaoImpl as follows,

EmployeeDaoImpl.java
 
package com.adnblog.dao;

importjava.util.List;

importorg.hibernate.SessionFactory;
importorg.springframework.beans.factory.annotation.Autowired;
importorg.springframework.stereotype.Repository;

importcom.adnblog.model.Employee;

/**
* @author Anil N
*
*/
@Repository
publicclassEmployeeDaoImplimplements EmployeeDao {

@Autowired
private SessionFactory sessionFactory;

public Integer addEmployee(Employee employee){
return(Integer)sessionFactory.getCurrentSession().save(employee);
}

publicvoidupdateEmployee(Employee employee){
sessionFactory.getCurrentSession().saveOrUpdate(employee);
}
@SuppressWarnings("unchecked")
public List<Employee>listEmployeess(){
return(List<Employee>) sessionFactory.getCurrentSession().createCriteria(Employee.class).list();
}

public Employee getEmployee(int empid){
return(Employee) sessionFactory.getCurrentSession().get(Employee.class, empid);
}

publicvoiddeleteEmployee(Employee emp){
sessionFactory.getCurrentSession().createQuery("DELETE FROM Employee WHERE emp_id = "+emp.getEmpId()).executeUpdate();
}

}

We are using Spring transaction so Hibernate transaction not required.


Service Classes :--

       In Service also I created one interface, there I declared all CRUD methods. Next in implemented class implemented all methods and used Spring transaction.


EmployeeService.java


package com.adnblog.service;

importjava.util.List;

importcom.adnblog.model.Employee;

/**
* @author Anil N
*
*/
publicinterfaceEmployeeService{

public Integer addEmployee(Employee employee);

public List<Employee>listEmployeess();

public Employee getEmployee(int empid);

publicvoidupdateEmployee(Employee employee);

publicvoiddeleteEmployee(Employee emp);
}

The implemented Service class i.e EmployeeServiceImpl is as follows, (@Transactional annotation is using for declarative transaction)

EmployeeServiceImpl.java

package com.adnblog.service;

importjava.util.List;

importorg.springframework.beans.factory.annotation.Autowired;
importorg.springframework.stereotype.Service;
importorg.springframework.transaction.annotation.Propagation;
importorg.springframework.transaction.annotation.Transactional;

importcom.adnblog.dao.EmployeeDao;
importcom.adnblog.model.Employee;

/**
* @author Anil N
*
*/
@Service
@Transactional(propagation = Propagation.SUPPORTS)
publicclassEmployeeServiceImplimplements EmployeeService {

@Autowired
private EmployeeDao employeeDao;

@Transactional(propagation = Propagation.REQUIRED)
public Integer addEmployee(Employee employee){
return employeeDao.addEmployee(employee);
}

public List<Employee>listEmployeess(){
return employeeDao.listEmployeess();
}

public Employee getEmployee(int empid){
return employeeDao.getEmployee(empid);
}
@Transactional(propagation = Propagation.REQUIRED)
publicvoidupdateEmployee(Employee employee){
employeeDao.updateEmployee(employee);
}

@Transactional(propagation = Propagation.REQUIRED)
publicvoiddeleteEmployee(Employee emp){
employeeDao.deleteEmployee(emp);
}

}



Controller class:--
       The Controller class is a heart of MVC pattern, using @Controller annoation Front controller finds the appropriate controller using HandlerMapping.  It will take the request, process it and send model data to the appropriate view.

EmployeeController.java  


package com.adnblog.controller;

importjava.util.ArrayList;
importjava.util.HashMap;
importjava.util.List;
importjava.util.Map;

importorg.springframework.beans.factory.annotation.Autowired;
importorg.springframework.stereotype.Controller;
importorg.springframework.validation.BindingResult;
importorg.springframework.web.bind.annotation.ModelAttribute;
importorg.springframework.web.bind.annotation.RequestMapping;
importorg.springframework.web.bind.annotation.RequestMethod;
importorg.springframework.web.bind.annotation.RequestParam;
importorg.springframework.web.servlet.ModelAndView;

importcom.adnblog.bean.EmployeeBean;
importcom.adnblog.model.Employee;
importcom.adnblog.service.EmployeeService;

/**
* @author Anil N
*
*/
@Controller
publicclassEmployeeController{

@Autowired
private EmployeeService employeeService;

@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("employee",new EmployeeBean());
returnnewModelAndView("addEmployee", model);
}

@RequestMapping(value ="/edit", method = RequestMethod.GET)
public ModelAndView editEmployee(@RequestParam("id") Integer id,
@ModelAttribute("command") EmployeeBean employeeBean){
Map<String, Object> model =new HashMap<String, Object>();
model.put("employee",new EmployeeBean());
model.put("employees", employeeService.getEmployee(id));
returnnewModelAndView("addEmployee", model);
}


@RequestMapping(value ="/save", method = RequestMethod.POST)
public ModelAndView saveEmployee(@RequestParam("id") String id,
@ModelAttribute("command") EmployeeBean employeeBean){

Integer empId =0;
Employee employee = prepareModel(employeeBean, id);
if(id !=null&&!id.isEmpty()){
employeeService.updateEmployee(employee);
empId = Integer.parseInt(id);
}else{
empId = employeeService.addEmployee(employee);
}
ModelAndView mv =new ModelAndView("redirect:/edit.html?id="+empId);
return mv;
}

@RequestMapping(value="/list", method = RequestMethod.GET)
public ModelAndView listEmployees(){
Map<String, Object> model =new HashMap<String, Object>();
model.put("employees", prepareListofBean(employeeService.listEmployeess()));
returnnewModelAndView("employeesList", model);
}

@RequestMapping(value ="/delete", method = RequestMethod.GET)
public ModelAndView deleteEmployee(@RequestParam("id") String id){
Employee emp =new Employee();
emp.setEmpId(Integer.parseInt(id));
employeeService.deleteEmployee(emp);
returnnewModelAndView("redirect:/list.html");
}

private Employee prepareModel(EmployeeBean employeeBean, String id){
Employee employee =new Employee();
employee.setEmpAddress(employeeBean.getEmpAddress());
employee.setEmpAge(employeeBean.getEmpAge());
employee.setEmpName(employeeBean.getEmpName());
employee.setSalary(employeeBean.getSalary());
if(id !=null&&!id.isEmpty()){
employee.setEmpId(Integer.parseInt(id));
}else{
employee.setEmpId(employeeBean.getEmpId());
}
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.setEmpName(employee.getEmpName());
bean.setEmpId(employee.getEmpId());
bean.setEmpAddress(employee.getEmpAddress());
bean.setSalary(employee.getSalary());
bean.setEmpAge(employee.getEmpAge());
beans.add(bean);
}
}
return beans;
}
}



Spring Bean Configuration (appln-servlet.xml)
         Next create one xml file under WEB-INF folder, give same file name whatever in the web.xml.
This configuration file config the bean and view using some tags. The appln-servlet.xml file is as follows.

appln-servlet.xml

<?xml version="1.0" encoding="UTF-8"?>
<beansxmlns="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">

<context:property-placeholderlocation="classpath:resources/database.properties"/>
<context:component-scanbase-package="com.adnblog"/>

<tx:annotation-driventransaction-manager="hibernateTransactionManager"/>

<beanid="dataSource"
class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<propertyname="driverClassName"value="${database.driver}"/>
<propertyname="url"value="${database.url}"/>
<propertyname="username"value="${database.user}"/>
<propertyname="password"value="${database.password}"/>
</bean>

<beanid="sessionFactory"
class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean">
<propertyname="dataSource"ref="dataSource"/>
<propertyname="annotatedClasses">
<list>
<value>com.adnblog.model.Employee</value>
</list>
</property>
<propertyname="hibernateProperties">
<props>
<propkey="hibernate.dialect">${hibernate.dialect}</prop>
<propkey="hibernate.show_sql">${hibernate.show_sql}</prop>
<propkey="hibernate.hbm2ddl.auto">${hibernate.hbm2ddl.auto}</prop>
</props>
</property>
</bean>

<beanid="hibernateTransactionManager"
class="org.springframework.orm.hibernate3.HibernateTransactionManager">
<propertyname="sessionFactory"ref="sessionFactory"/>
</bean>

<beanid="jspViewResolver"
class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<propertyname="viewClass"
value="org.springframework.web.servlet.view.JstlView"/>
<propertyname="prefix"value="/WEB-INF/pages/"/>
<propertyname="suffix"value=".jsp"/>
</bean>
</beans>



View(Jsp page): - 
         I have created two JSP here, addEmployee.jsp and employeeList.jsp. The addEmployee.jsp is using for adding and editing the employee details, employeeList.jsp is for displaying the employee list and delete the particular employee from the list.
created under pages folder of WEB-INF,   

addEmployee.jsp


<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<%@taglib uri="http://www.springframework.org/tags/form" prefix="form"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN""http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<metahttp-equiv="Content-Type"content="text/html; charset=ISO-8859-1">
<c:setvar="addOrEdit"value="Add"/>
<c:setvar="submit"value="Submit"/>
<c:iftest="${employees.empId != null}">
<c:setvar="addOrEdit"value="Edit"/>
<c:setvar="submit"value="Update"/>
</c:if>
<title>Spring MVC Add/Edit Operation</title>
</head>
<body>
<h2>${addOrEdit} Employee Data</h2>
<form:formmethod="POST"action="/WebAppln/save.html?id=${employees.empId}"commandName="command">
<table>
<tr>
<td><form:labelpath="empId">Employee ID:</form:label></td>
<td><c:outvalue="${employees.empId}"/>
<form:hiddenpath="empId"/></td>
</tr>
<tr>
<td><form:labelpath="empName">Employee Name:</form:label></td>
<td><form:inputpath="empName"value="${employees.empName}"/></td>
</tr>
<tr>
<td><form:labelpath="empAge">Employee Age:</form:label></td>
<td><form:inputpath="empAge"value="${employees.empAge}"/></td>
</tr>
<tr>
<td><form:labelpath="salary">Employee Salary:</form:label></td>
<td><form:inputpath="salary"value="${employees.salary}"/></td>
</tr>

<tr>
<td><form:labelpath="empAddress">Employee Address:</form:label></td>
<td><form:inputpath="empAddress"value="${employees.empAddress}"/></td>
</tr>
<tr><td></td>
<tdcolspan="2"><inputtype="submit"value="${submit}"/>&nbsp;&nbsp;&nbsp;<b><ahref="/WebAppln/list.html">List Of Employee</a></b></td>
</tr>
</table>
</form:form>
</body>
</html>

employeeList.jsp
 
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN""http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<title>All Employees</title>
<style>
table, th, td {
border: 1px solid black;
border-collapse: collapse;
}
th, td {
padding: 5px;
text-align: left;
}
</style>
</head>
<body>
<h1>List Employees</h1>
<c:iftest="${empty employees}">
<h3>No Records found!</h3>
</c:if>

<c:iftest="${!empty employees}">
<tablealign="left"width="100%">
<tr>
<th>Employee ID</th>
<th>Employee Name</th>
<th>Employee Age</th>
<th>Employee Salary</th>
<th>Employee Address</th> 
                        <th></th>
<th></th>
</tr>

<c:forEachitems="${employees}"var="employee">
<tr>
<td><c:outvalue="${employee.empId}"/></td>
<td><c:outvalue="${employee.empName}"/></td>
<td><c:outvalue="${employee.empAge}"/></td>
<td><c:outvalue="${employee.salary}"/></td>
<td><c:outvalue="${employee.empAddress}"/></td> 
                                <td><b><a href="/WebAppln/edit.html?id=${employee.empId}">Edit</a></b></td>
 <td><b><ahref="/WebAppln/delete.html?id=${employee.empId}">Delete</a></b></td>
</tr>
</c:forEach>
</table><br/>
</c:if>
<tablestyle="border: none;">
<tr>
<tdstyle="padding : none; border: none;"><h3><ahref="/WebAppln/add.html">Add More Employee</a></h3></td>
</tr>
</table>
</body>
</html>



Running the application using Tomcat Server or any Server:--
        If you run the given application without spcifying the URL, it will execute the welcome file i.e add.html so by default add employee will call.



List of Employee : -


Edit screen:--

 Thank you for visiting blog...



Related post :
Spring Annotations
Spring MVC workflow with example  

Viewing all articles
Browse latest Browse all 154

Trending Articles