Beruflich Dokumente
Kultur Dokumente
0 CRUD Example
In this example show how to write a simple web based application with CRUD
operation using Spring3 MVC Framwork with Hibernate3 using Annotation,
which can handle CRUD inside its controllers. To start with it, let us have
working STS IDE in place and follow the following steps to develop a Dynamic
Form based Web Application using Spring Web Framework:
Related Tutorials
Spring Tutorial
);
database.driver=com.mysql.jdbc.Driver
database.url=jdbc:mysql://localhost:3306/DAVDB
database.user=root
database.password=root
hibernate.dialect=org.hibernate.dialect.MySQLDialect
hibernate.show_sql=true
hibernate.hbm2ddl.auto=update
Step 4: Add below mentioned Spring 3.0 and Hibernate 3.0 related libraries
and other libraries into the folder WebRoot/WEB-INF/lib.
Step 8: The final step is to create the content of all the source and
configuration files name sdnext-servlet.xml under the sub-folder
/WebRoot/WEB-INF/config and export the application as explained below.
We will have a layered architecture for our demo application. The database
will be accessed by a Data Access layer popularly called as DAO Layer. This
layer will use Hibernate API to interact with database. The DAO layer will be
invoked by a service layer. In our application we will have a Service interface
called EmployeeService.
EmployeeBean.java
package com.dineshonjava.bean;
/**
*/
return salary;
this.salary = salary;
}
return id;
this.id = id;
return name;
this.name = name;
return age;
this.age = age;
return address;
this.address = address;
}
Employee.java
package com.dineshonjava.model;
import java.io.Serializable;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.Table;
/**
*/
@Entity
@Table(name="Employee")
@Id
@GeneratedValue(strategy=GenerationType.AUTO)
@Column(name = "empid")
@Column(name="empaddress")
@Column(name="salary")
@Column(name="empAge")
return empId;
this.empId = empId;
return empName;
return empAddress;
this.empAddress = empAddress;
return salary;
this.salary = salary;
return empAge;
this.empAge = empAge;
}
}
EmployeeDao.java
package com.dineshonjava.dao;
import java.util.List;
import com.dineshonjava.model.Employee;
/**
*/
EmployeeDaoImpl.java
package com.dineshonjava.dao;
import java.util.List;
import org.hibernate.SessionFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;
import com.dineshonjava.model.Employee;
/**
*/
@Repository("employeeDao")
@Autowired
sessionFactory.getCurrentSession().saveOrUpdate(employee);
@SuppressWarnings("unchecked")
return (List<Employee>)
sessionFactory.getCurrentSession().createCriteria(Employee.class).list();
}
public Employee getEmployee(int empid) {
EmployeeService.java
package com.dineshonjava.service;
import java.util.List;
import com.dineshonjava.model.Employee;
/**
*/
EmployeeServiceImpl.java
package com.dineshonjava.service;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
import com.dineshonjava.dao.EmployeeDao;
import com.dineshonjava.model.Employee;
/**
*/
@Service("employeeService")
@Autowired
private EmployeeDao employeeDao;
employeeDao.addEmployee(employee);
return employeeDao.listEmployeess();
return employeeDao.getEmployee(empid);
employeeDao.deleteEmployee(employee);
EmployeeController.java
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;
/**
*/
@Controller
@Autowired
public ModelAndView
saveEmployee(@ModelAttribute("command")EmployeeBean employeeBean,
BindingResult result) {
employeeService.addEmployee(employee);
model.put("employees",
prepareListofBean(employeeService.listEmployeess()));
public ModelAndView
addEmployee(@ModelAttribute("command")EmployeeBean employeeBean,
BindingResult result) {
model.put("employees",
prepareListofBean(employeeService.listEmployeess()));
public ModelAndView
editEmployee(@ModelAttribute("command")EmployeeBean employeeBean,
BindingResult result) {
employeeService.deleteEmployee(prepareModel(employeeBean));
model.put("employee", null);
model.put("employees",
prepareListofBean(employeeService.listEmployeess()));
public ModelAndView
deleteEmployee(@ModelAttribute("command")EmployeeBean employeeBean,
BindingResult result) {
model.put("employee",
prepareEmployeeBean(employeeService.getEmployee(employeeBean.getId())
));
model.put("employees",
prepareListofBean(employeeService.listEmployeess()));
employee.setEmpAge(employeeBean.getAge());
employee.setEmpName(employeeBean.getName());
employee.setSalary(employeeBean.getSalary());
employee.setEmpId(employeeBean.getId());
employeeBean.setId(null);
return employee;
bean.setName(employee.getEmpName());
bean.setId(employee.getEmpId());
bean.setAddress(employee.getEmpAddress());
bean.setSalary(employee.getSalary());
bean.setAge(employee.getEmpAge());
beans.add(bean);
return beans;
}
private EmployeeBean prepareEmployeeBean(Employee employee){
bean.setAddress(employee.getEmpAddress());
bean.setAge(employee.getEmpAge());
bean.setName(employee.getEmpName());
bean.setSalary(employee.getSalary());
bean.setId(employee.getEmpId());
return bean;
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>
<beans xmlns:context="http://www.springframework.org/schema/context"
xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://www.springframework.org/schema/beans"
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-placeholder
location="classpath:resources/database.properties">
</context:property-placeholder>
<context:component-scan base-package="com.dineshonjava">
</context:component-scan>
<tx:annotation-driven transaction-
manager="hibernateTransactionManager">
</tx:annotation-driven>
<bean
class="org.springframework.web.servlet.view.InternalResourceViewResolver"
id="jspViewResolver">
<property name="viewClass"
value="org.springframework.web.servlet.view.JstlView"></property>
</bean>
<bean
class="org.springframework.jdbc.datasource.DriverManagerDataSource"
id="dataSource">
</bean>
<bean
class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFa
ctoryBean" id="sessionFactory">
<property name="annotatedClasses">
<list>
<value>com.dineshonjava.model.Employee</value>
</list>
</property>
<property name="hibernateProperties">
<props>
<prop key="hibernate.dialect">${hibernate.dialect}</prop>
<prop key="hibernate.show_sql">${hibernate.show_sql}</prop>
<prop key="hibernate.hbm2ddl.auto">${hibernate.hbm2ddl.auto}
</prop>
</props>
</property>
</bean>
<bean
class="org.springframework.orm.hibernate3.HibernateTransactionManager"
id="hibernateTransactionManager">
</bean>
</beans>
addEmployee.jsp
pageEncoding="ISO-8859-1"%>
<%@taglib uri="http://www.springframework.org/tags/form"
prefix="form"%>
<html>
<head>
</head>
<body>
<table>
<tr>
</tr>
<tr>
</tr>
<tr>
</tr>
<tr>
</tr>
<tr>
<td><form:label path="address">Employee
Address:</form:label></td>
</tr>
<tr>
</tr>
</table>
</form:form>
<h2>List Employees</h2>
<tr>
<th>Employee ID</th>
<th>Employee Name</th>
<th>Employee Age</th>
<th>Employee Salary</th>
<th>Employee Address</th>
<th>Actions on Row</th>
</tr>
<tr>
<td><c:out value="${employee.id}"/></td>
<td><c:out value="${employee.name}"/></td>
<td><c:out value="${employee.age}"/></td>
<td><c:out value="${employee.salary}"/></td>
<td><c:out value="${employee.address}"/></td>
</tr>
</c:forEach>
</table>
</c:if>
</body>
</html>
employeesList.jsp
pageEncoding="ISO-8859-1"%>
<html>
<head>
<title>All Employees</title>
</head>
<body>
<h1>List Employees</h1>
<tr>
<th>Employee ID</th>
<th>Employee Name</th>
<th>Employee Age</th>
<th>Employee Salary</th>
<th>Employee Address</th>
</tr>
<tr>
<td><c:out value="${employee.id}"/></td>
<td><c:out value="${employee.name}"/></td>
<td><c:out value="${employee.age}"/></td>
<td><c:out value="${employee.salary}"/></td>
<td><c:out value="${employee.address}"/></td>
</tr>
</c:forEach>
</table>
</c:if>
</body>
</html>
index.jsp
pageEncoding="ISO-8859-1"%>
<head>
</head>
<body>
</body>
</html>
Once you are done with creating source and configuration files, export your
application. Right click on your application and use Export-> WAR File option
and save your Spring3HibernateApp.war file in Tomcat's webapps folder.
Now start your Tomcat server and make sure you are able to access other
web pages from webapps folder using a standard browser. Now try a URL
http://localhost:8080/sdnext/ and you should see the following result if
everything is fine with your Spring Web Application:
1. CREATE EMPLOYEE: Now click on the Add Employee link we get the
following form for Create Employee.
Now click on the Submit button data are saved to the employee table on the
database. And we get the following
2 READ EMLOYEE : Now click on the List of Employee link we get the
following employee list.
3.UPDATE EMPLOYEE: for update the emloyee form we have to click on the
Edit link in the table of employees show on the broswer.
Now update the name field value from Sweety to Sweety Rajput and salary
filed value 35000 to 36000 and submit the data after submit we get the
following updaed table against fifth row of table.
Spring3 Hibernate
4.DELETE EMPLOYEE: Now we want to delete the one employee record from
the table the we click on the delete button. we want to delete fifth records of
the above table now click on the delete button of the corresponding records
and we get the final list as below.
In this example show how to write a simple web based application with CRUD
operation using Spring3 MVC Framwork with Hibernate3 using Annotation,
which can handle CRUD inside its controllers. To start with it, let us have
working STS IDE in place and follow the following steps to develop a Dynamic
Form based Web Application using Spring Web Framework:
Related Tutorials
Spring Tutorial
);
database.driver=com.mysql.jdbc.Driver
database.url=jdbc:mysql://localhost:3306/DAVDB
database.user=root
database.password=root
hibernate.dialect=org.hibernate.dialect.MySQLDialect
hibernate.show_sql=true
hibernate.hbm2ddl.auto=update
Step 4: Add below mentioned Spring 3.0 and Hibernate 3.0 related libraries
and other libraries into the folder WebRoot/WEB-INF/lib.
Step 8: The final step is to create the content of all the source and
configuration files name sdnext-servlet.xml under the sub-folder
/WebRoot/WEB-INF/config and export the application as explained below.
APPLICATION ARCHITECTURE
We will have a layered architecture for our demo application. The database
will be accessed by a Data Access layer popularly called as DAO Layer. This
layer will use Hibernate API to interact with database. The DAO layer will be
invoked by a service layer. In our application we will have a Service interface
called EmployeeService.
EmployeeBean.java
package com.dineshonjava.bean;
/**
*/
public class EmployeeBean {
return salary;
this.salary = salary;
return id;
this.id = id;
return name;
this.name = name;
this.age = age;
return address;
this.address = address;
Employee.java
package com.dineshonjava.model;
import java.io.Serializable;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.Table;
/**
*/
@Entity
@Table(name="Employee")
@Id
@GeneratedValue(strategy=GenerationType.AUTO)
@Column(name = "empid")
@Column(name="empname")
@Column(name="empaddress")
@Column(name="salary")
@Column(name="empAge")
this.empId = empId;
return empName;
this.empName = empName;
return empAddress;
this.empAddress = empAddress;
return salary;
}
public void setSalary(Long salary) {
this.salary = salary;
return empAge;
this.empAge = empAge;
EmployeeDao.java
package com.dineshonjava.dao;
import java.util.List;
import com.dineshonjava.model.Employee;
/**
*/
EmployeeDaoImpl.java
package com.dineshonjava.dao;
import java.util.List;
import org.hibernate.SessionFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;
import com.dineshonjava.model.Employee;
/**
*/
@Repository("employeeDao")
sessionFactory.getCurrentSession().saveOrUpdate(employee);
@SuppressWarnings("unchecked")
return (List<Employee>)
sessionFactory.getCurrentSession().createCriteria(Employee.class).list();
EmployeeService.java
package com.dineshonjava.service;
import java.util.List;
import com.dineshonjava.model.Employee;
/**
*/
EmployeeServiceImpl.java
package com.dineshonjava.service;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
import com.dineshonjava.dao.EmployeeDao;
import com.dineshonjava.model.Employee;
/**
*/
@Service("employeeService")
@Autowired
employeeDao.addEmployee(employee);
return employeeDao.listEmployeess();
return employeeDao.getEmployee(empid);
}
employeeDao.deleteEmployee(employee);
EmployeeController.java
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;
/**
*/
@Controller
@Autowired
public ModelAndView
saveEmployee(@ModelAttribute("command")EmployeeBean employeeBean,
BindingResult result) {
employeeService.addEmployee(employee);
model.put("employees",
prepareListofBean(employeeService.listEmployeess()));
}
@RequestMapping(value = "/add", method = RequestMethod.GET)
public ModelAndView
addEmployee(@ModelAttribute("command")EmployeeBean employeeBean,
BindingResult result) {
model.put("employees",
prepareListofBean(employeeService.listEmployeess()));
public ModelAndView
editEmployee(@ModelAttribute("command")EmployeeBean employeeBean,
BindingResult result) {
employeeService.deleteEmployee(prepareModel(employeeBean));
model.put("employee", null);
model.put("employees",
prepareListofBean(employeeService.listEmployeess()));
}
@RequestMapping(value = "/edit", method = RequestMethod.GET)
public ModelAndView
deleteEmployee(@ModelAttribute("command")EmployeeBean employeeBean,
BindingResult result) {
model.put("employee",
prepareEmployeeBean(employeeService.getEmployee(employeeBean.getId())
));
model.put("employees",
prepareListofBean(employeeService.listEmployeess()));
employee.setEmpAddress(employeeBean.getAddress());
employee.setEmpAge(employeeBean.getAge());
employee.setEmpName(employeeBean.getName());
employee.setSalary(employeeBean.getSalary());
employee.setEmpId(employeeBean.getId());
employeeBean.setId(null);
return employee;
bean.setName(employee.getEmpName());
bean.setId(employee.getEmpId());
bean.setAddress(employee.getEmpAddress());
bean.setSalary(employee.getSalary());
bean.setAge(employee.getEmpAge());
beans.add(bean);
return beans;
bean.setAddress(employee.getEmpAddress());
bean.setAge(employee.getEmpAge());
bean.setName(employee.getEmpName());
bean.setSalary(employee.getSalary());
bean.setId(employee.getEmpId());
return bean;
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>
<beans xmlns:context="http://www.springframework.org/schema/context"
xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://www.springframework.org/schema/beans"
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-placeholder
location="classpath:resources/database.properties">
</context:property-placeholder>
<context:component-scan base-package="com.dineshonjava">
</context:component-scan>
<tx:annotation-driven transaction-
manager="hibernateTransactionManager">
</tx:annotation-driven>
<bean
class="org.springframework.web.servlet.view.InternalResourceViewResolver"
id="jspViewResolver">
<property name="viewClass"
value="org.springframework.web.servlet.view.JstlView"></property>
</bean>
<bean
class="org.springframework.jdbc.datasource.DriverManagerDataSource"
id="dataSource">
</bean>
<bean
class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFa
ctoryBean" id="sessionFactory">
<property name="annotatedClasses">
<list>
<value>com.dineshonjava.model.Employee</value>
</list>
</property>
<property name="hibernateProperties">
<props>
<prop key="hibernate.dialect">${hibernate.dialect}</prop>
<prop key="hibernate.show_sql">${hibernate.show_sql}</prop>
<prop key="hibernate.hbm2ddl.auto">${hibernate.hbm2ddl.auto}
</prop>
</props>
</property>
</bean>
<bean
class="org.springframework.orm.hibernate3.HibernateTransactionManager"
id="hibernateTransactionManager">
</bean>
</beans>
addEmployee.jsp
pageEncoding="ISO-8859-1"%>
<%@taglib uri="http://www.springframework.org/tags/form"
prefix="form"%>
<html>
<head>
</head>
<body>
<table>
<tr>
<tr>
</tr>
<tr>
</tr>
<tr>
</tr>
<tr>
<td><form:label path="address">Employee
Address:</form:label></td>
</tr>
<tr>
</tr>
</table>
</form:form>
<tr>
<th>Employee ID</th>
<th>Employee Name</th>
<th>Employee Age</th>
<th>Employee Salary</th>
<th>Employee Address</th>
<th>Actions on Row</th>
</tr>
<tr>
<td><c:out value="${employee.id}"/></td>
<td><c:out value="${employee.name}"/></td>
<td><c:out value="${employee.age}"/></td>
<td><c:out value="${employee.salary}"/></td>
<td><c:out value="${employee.address}"/></td>
</tr>
</c:forEach>
</table>
</c:if>
</body>
</html>
employeesList.jsp
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<html>
<head>
<title>All Employees</title>
</head>
<body>
<h1>List Employees</h1>
<tr>
<th>Employee ID</th>
<th>Employee Name</th>
<th>Employee Age</th>
<th>Employee Salary</th>
<th>Employee Address</th>
</tr>
<tr>
<td><c:out value="${employee.id}"/></td>
<td><c:out value="${employee.name}"/></td>
<td><c:out value="${employee.age}"/></td>
<td><c:out value="${employee.salary}"/></td>
<td><c:out value="${employee.address}"/></td>
</tr>
</c:forEach>
</table>
</c:if>
</body>
</html>
index.jsp
pageEncoding="ISO-8859-1"%>
<html>
<head>
</head>
<body>
</body>
</html>
Once you are done with creating source and configuration files, export your
application. Right click on your application and use Export-> WAR File option
and save your Spring3HibernateApp.war file in Tomcat's webapps folder.
Now start your Tomcat server and make sure you are able to access other
web pages from webapps folder using a standard browser. Now try a URL
http://localhost:8080/sdnext/ and you should see the following result if
everything is fine with your Spring Web Application:
1. CREATE EMPLOYEE: Now click on the Add Employee link we get the
following form for Create Employee.
Now click on the Submit button data are saved to the employee table on the
database. And we get the following
2 READ EMLOYEE : Now click on the List of Employee link we get the
following employee list.
3.UPDATE EMPLOYEE: for update the emloyee form we have to click on the
Edit link in the table of employees show on the broswer.
Now update the name field value from Sweety to Sweety Rajput and salary
filed value 35000 to 36000 and submit the data after submit we get the
following updaed table against fifth row of table.
Spring3 Hibernate
4.DELETE EMPLOYEE: Now we want to delete the one employee record from
the table the we click on the delete button. we want to delete fifth records of
the above table now click on the delete button of the corresponding records
and we get the final list as below.
In this example show how to write a simple web based application with CRUD
operation using Spring3 MVC Framwork with Hibernate3 using Annotation,
which can handle CRUD inside its controllers. To start with it, let us have
working STS IDE in place and follow the following steps to develop a Dynamic
Form based Web Application using Spring Web Framework:
Related Tutorials
Spring Tutorial
);
database.driver=com.mysql.jdbc.Driver
database.url=jdbc:mysql://localhost:3306/DAVDB
database.user=root
database.password=root
hibernate.dialect=org.hibernate.dialect.MySQLDialect
hibernate.show_sql=true
hibernate.hbm2ddl.auto=update
Step 4: Add below mentioned Spring 3.0 and Hibernate 3.0 related libraries
and other libraries into the folder WebRoot/WEB-INF/lib.
Step 8: The final step is to create the content of all the source and
configuration files name sdnext-servlet.xml under the sub-folder
/WebRoot/WEB-INF/config and export the application as explained below.
APPLICATION ARCHITECTURE
We will have a layered architecture for our demo application. The database
will be accessed by a Data Access layer popularly called as DAO Layer. This
layer will use Hibernate API to interact with database. The DAO layer will be
invoked by a service layer. In our application we will have a Service interface
called EmployeeService.
EmployeeBean.java
package com.dineshonjava.bean;
/**
*/
return salary;
this.salary = salary;
}
public Integer getId() {
return id;
this.id = id;
return name;
this.name = name;
return age;
this.age = age;
return address;
this.address = address;
Employee.java
package com.dineshonjava.model;
import java.io.Serializable;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.Table;
/**
*/
@Entity
@Table(name="Employee")
@Id
@GeneratedValue(strategy=GenerationType.AUTO)
@Column(name = "empid")
@Column(name="empaddress")
@Column(name="salary")
@Column(name="empAge")
return empId;
this.empId = empId;
return empName;
this.empName = empName;
}
return empAddress;
this.empAddress = empAddress;
return salary;
this.salary = salary;
return empAge;
this.empAge = empAge;
}
EmployeeDao.java
package com.dineshonjava.dao;
import java.util.List;
import com.dineshonjava.model.Employee;
/**
*/
EmployeeDaoImpl.java
package com.dineshonjava.dao;
import java.util.List;
import org.hibernate.SessionFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;
import com.dineshonjava.model.Employee;
/**
*/
@Repository("employeeDao")
@Autowired
sessionFactory.getCurrentSession().saveOrUpdate(employee);
@SuppressWarnings("unchecked")
return (List<Employee>)
sessionFactory.getCurrentSession().createCriteria(Employee.class).list();
EmployeeService.java
package com.dineshonjava.service;
import java.util.List;
import com.dineshonjava.model.Employee;
/**
*/
EmployeeServiceImpl.java
package com.dineshonjava.service;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
import com.dineshonjava.dao.EmployeeDao;
import com.dineshonjava.model.Employee;
/**
*/
@Service("employeeService")
@Autowired
employeeDao.addEmployee(employee);
return employeeDao.listEmployeess();
return employeeDao.getEmployee(empid);
employeeDao.deleteEmployee(employee);
EmployeeController.java
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;
/**
*/
@Controller
@Autowired
public ModelAndView
saveEmployee(@ModelAttribute("command")EmployeeBean employeeBean,
BindingResult result) {
Employee employee = prepareModel(employeeBean);
employeeService.addEmployee(employee);
model.put("employees",
prepareListofBean(employeeService.listEmployeess()));
public ModelAndView
addEmployee(@ModelAttribute("command")EmployeeBean employeeBean,
BindingResult result) {
model.put("employees",
prepareListofBean(employeeService.listEmployeess()));
}
@RequestMapping(value = "/delete", method = RequestMethod.GET)
public ModelAndView
editEmployee(@ModelAttribute("command")EmployeeBean employeeBean,
BindingResult result) {
employeeService.deleteEmployee(prepareModel(employeeBean));
model.put("employee", null);
model.put("employees",
prepareListofBean(employeeService.listEmployeess()));
public ModelAndView
deleteEmployee(@ModelAttribute("command")EmployeeBean employeeBean,
BindingResult result) {
model.put("employee",
prepareEmployeeBean(employeeService.getEmployee(employeeBean.getId())
));
model.put("employees",
prepareListofBean(employeeService.listEmployeess()));
employee.setEmpAddress(employeeBean.getAddress());
employee.setEmpAge(employeeBean.getAge());
employee.setEmpName(employeeBean.getName());
employee.setSalary(employeeBean.getSalary());
employee.setEmpId(employeeBean.getId());
employeeBean.setId(null);
return employee;
bean.setName(employee.getEmpName());
bean.setId(employee.getEmpId());
bean.setAddress(employee.getEmpAddress());
bean.setSalary(employee.getSalary());
bean.setAge(employee.getEmpAge());
beans.add(bean);
return beans;
}
private EmployeeBean prepareEmployeeBean(Employee employee){
bean.setAddress(employee.getEmpAddress());
bean.setAge(employee.getEmpAge());
bean.setName(employee.getEmpName());
bean.setSalary(employee.getSalary());
bean.setId(employee.getEmpId());
return bean;
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>
<beans xmlns:context="http://www.springframework.org/schema/context"
xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://www.springframework.org/schema/beans"
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-placeholder
location="classpath:resources/database.properties">
</context:property-placeholder>
<context:component-scan base-package="com.dineshonjava">
</context:component-scan>
<tx:annotation-driven transaction-
manager="hibernateTransactionManager">
</tx:annotation-driven>
<bean
class="org.springframework.web.servlet.view.InternalResourceViewResolver"
id="jspViewResolver">
<property name="viewClass"
value="org.springframework.web.servlet.view.JstlView"></property>
</bean>
<bean
class="org.springframework.jdbc.datasource.DriverManagerDataSource"
id="dataSource">
</bean>
<bean
class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFa
ctoryBean" id="sessionFactory">
<property name="annotatedClasses">
<list>
<value>com.dineshonjava.model.Employee</value>
</list>
</property>
<property name="hibernateProperties">
<props>
<prop key="hibernate.dialect">${hibernate.dialect}</prop>
<prop key="hibernate.show_sql">${hibernate.show_sql}</prop>
<prop key="hibernate.hbm2ddl.auto">${hibernate.hbm2ddl.auto}
</prop>
</props>
</property>
</bean>
<bean
class="org.springframework.orm.hibernate3.HibernateTransactionManager"
id="hibernateTransactionManager">
</bean>
</beans>
addEmployee.jsp
pageEncoding="ISO-8859-1"%>
<%@taglib uri="http://www.springframework.org/tags/form"
prefix="form"%>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-
1">
</head>
<body>
<table>
<tr>
</tr>
<tr>
</tr>
<tr>
</tr>
<tr>
</tr>
<tr>
<td><form:label path="address">Employee
Address:</form:label></td>
</tr>
<tr>
</tr>
</table>
</form:form>
<h2>List Employees</h2>
<tr>
<th>Employee ID</th>
<th>Employee Name</th>
<th>Employee Age</th>
<th>Employee Salary</th>
<th>Employee Address</th>
<th>Actions on Row</th>
</tr>
<tr>
<td><c:out value="${employee.id}"/></td>
<td><c:out value="${employee.name}"/></td>
<td><c:out value="${employee.age}"/></td>
<td><c:out value="${employee.salary}"/></td>
<td><c:out value="${employee.address}"/></td>
</tr>
</c:forEach>
</table>
</c:if>
</body>
</html>
employeesList.jsp
pageEncoding="ISO-8859-1"%>
<html>
<head>
<title>All Employees</title>
</head>
<body>
<h1>List Employees</h1>
<th>Employee ID</th>
<th>Employee Name</th>
<th>Employee Age</th>
<th>Employee Salary</th>
<th>Employee Address</th>
</tr>
<tr>
<td><c:out value="${employee.id}"/></td>
<td><c:out value="${employee.name}"/></td>
<td><c:out value="${employee.age}"/></td>
<td><c:out value="${employee.salary}"/></td>
<td><c:out value="${employee.address}"/></td>
</tr>
</c:forEach>
</table>
</c:if>
</body>
</html>
index.jsp
pageEncoding="ISO-8859-1"%>
<html>
<head>
</head>
<body>
</body>
</html>
Once you are done with creating source and configuration files, export your
application. Right click on your application and use Export-> WAR File option
and save your Spring3HibernateApp.war file in Tomcat's webapps folder.
Now start your Tomcat server and make sure you are able to access other
web pages from webapps folder using a standard browser. Now try a URL
http://localhost:8080/sdnext/ and you should see the following result if
everything is fine with your Spring Web Application:
1. CREATE EMPLOYEE: Now click on the Add Employee link we get the
following form for Create Employee.
Now click on the Submit button data are saved to the employee table on the
database. And we get the following
2 READ EMLOYEE : Now click on the List of Employee link we get the
following employee list.
Spring and Hibernate Application
3.UPDATE EMPLOYEE: for update the emloyee form we have to click on the
Edit link in the table of employees show on the broswer.
Now update the name field value from Sweety to Sweety Rajput and salary
filed value 35000 to 36000 and submit the data after submit we get the
following updaed table against fifth row of table.
Spring3 Hibernate
4.DELETE EMPLOYEE: Now we want to delete the one employee record from
the table the we click on the delete button. we want to delete fifth records of
the above table now click on the delete button of the corresponding records
and we get the final list as below.
Spring Hibernate