MySQL 5.1.10
Java 8
Eclipse
Tomcat 8
Maven 3
<hibernate.version>4.3.8.Final</hibernate.version>
<mysql.version>5.1.10</mysql.version>
<junit-version>4.11</junit-version>
<servlet-api-version>3.1.0</servlet-api-version>
<jsp-version>2.1</jsp-version>
<jstl-version>1.2</jstl-version>
<java.version>1.8</java.version>
</properties>
<!-- Specifying the Dependencies -->
<dependencies>
<!-- Spring 4 dependencies -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-core</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-web</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-orm</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-test</artifactId>
<version>${spring.version}</version>
<scope>test</scope>
</dependency>
<!-- Hibernate 4 dependencies -->
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-core</artifactId>
<version>${hibernate.version}</version>
</dependency>
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-c3p0</artifactId>
<version>${hibernate.version}</version>
</dependency>
<!--MYSQL Connector -->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>${mysql.version}</version>
</dependency>
<!-- Servlet and JSP -->
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>javax.servlet-api</artifactId>
<version>${servlet-api-version}</version>
</dependency>
<dependency>
<groupId>javax.servlet.jsp</groupId>
<artifactId>jsp-api</artifactId>
<version>${jsp-version}</version>
<scope>provided</scope>
</dependency>
<!-- JSTL dependency -->
<dependency>
<groupId>jstl</groupId>
<artifactId>jstl</artifactId>
<version>${jstl-version}</version>
</dependency>
<!-- JUnit -->
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>${junit-version}</version>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<finalName>Spring4Hiberante4Integration</finalName>
<plugins>
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<version>2.3.2</version>
<configuration>
<source>${java.version}</source>
<target>${java.version}</target>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-war-plugin</artifactId>
<version>2.3</version>
<executions>
<execution>
<phase>package</phase>
<configuration>
<webXml>src/main/webapp/WEB-INF/web.xml</webXml>
</configuration>
</execution>
</executions>
</plugin>
<plugin>
<artifactId>maven-surefire-plugin</artifactId>
<configuration>
<includes>
<include>**/*Tests.java</include>
</includes>
</configuration>
</plugin>
</plugins>
</build>
</project>
<servletclass>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>mvc-dispatcher</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
</web-app>
value="com.ranga.entity"></property>
</bean>
<!-- Transaction -->
<bean id="transactionManager"
class="org.springframework.orm.hibernate4.HibernateTransactionManager">
<property name="sessionFactory" ref="sessionFactory" />
</bean>
<tx:annotation-driven transaction-manager="transactionManager" />
</beans>
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.Table;
import java.io.Serializable;
/**
* @author Ranga Reddy
* @version 1.0
*
*/
@Entity
@Table(name = "Employees")
public class Employee implements Serializable {
private static final long serialVersionUID = -7988799579036225137L;
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private long id;
@Column
private String name;
@Column
private int age;
@Column
private float salary;
public Employee() {
}
public long getId() {
return id;
}
public void setId(long id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public float getSalary() {
return salary;
}
public void setSalary(float salary) {
this.salary = salary;
}
@Override
public String toString() {
return "Employee{" +
"id=" + id +
", name='" + name + '\'' +
", age=" + age +
", salary=" + salary +
'}';
}
}
/**
* @author Ranga Reddy
* @version 1.0
*/
@Repository
public class HibernateUtil {
@Autowired
private SessionFactory sessionFactory;
public <T> Serializable create(final T entity) {
return sessionFactory.getCurrentSession().save(entity);
}
public <T> T update(final T entity) {
sessionFactory.getCurrentSession().update(entity);
return entity;
}
public <T> void delete(final T entity) {
sessionFactory.getCurrentSession().delete(entity);
}
public <T> void delete(Serializable id, Class<T> entityClass) {
T entity = fetchById(id, entityClass);
delete(entity);
}
@SuppressWarnings("unchecked")
public <T> List<T> fetchAll(Class<T> entityClass) {
return sessionFactory.getCurrentSession().createQuery(" FROM
"+entityClass.getName()).list();
}
@SuppressWarnings("rawtypes")
public <T> List fetchAll(String query) {
return sessionFactory.getCurrentSession().createSQLQuery(query).list();
}
@SuppressWarnings("unchecked")
public <T> T fetchById(Serializable id, Class<T> entityClass) {
return (T)sessionFactory.getCurrentSession().get(entityClass, id);
}
}
EmployeeDAOImpl.java
package com.ranga.dao.impl;
import com.ranga.dao.EmployeeDAO;
import com.ranga.entity.Employee;
import com.ranga.util.HibernateUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;
import java.math.BigInteger;
import java.util.ArrayList;
import java.util.List;
/**
* @author Ranga Reddy
* @version 1.0
*/
@Repository
public class EmployeeDAOImpl implements EmployeeDAO {
public EmployeeDAOImpl() {
System.out.println("EmployeeDAOImpl");
}
@Autowired
private HibernateUtil hibernateUtil;
@Override
public long createEmployee(Employee employee) {
return (Long) hibernateUtil.create(employee);
}
@Override
public Employee updateEmployee(Employee employee) {
return hibernateUtil.update(employee);
}
@Override
public void deleteEmployee(long id) {
Employee employee = new Employee();
employee.setId(id);
hibernateUtil.delete(employee);
}
@Override
public List<Employee> getAllEmployees() {
return hibernateUtil.fetchAll(Employee.class);
}
@Override
public Employee getEmployee(long id) {
return hibernateUtil.fetchById(id, Employee.class);
}
@SuppressWarnings("unchecked")
@Override
public List<Employee> getAllEmployees(String employeeName) {
String query = "SELECT e.* FROM Employees e WHERE e.name like '%"+
employeeName +"%'";
List<Object[]> employeeObjects = hibernateUtil.fetchAll(query);
List<Employee> employees = new ArrayList<Employee>();
for(Object[] employeeObject: employeeObjects) {
Employee employee = new Employee();
long id = ((BigInteger) employeeObject[0]).longValue();
int age = (int) employeeObject[1];
String name = (String) employeeObject[2];
float salary = (float) employeeObject[3];
employee.setId(id);
employee.setName(name);
employee.setAge(age);
employee.setSalary(salary);
employees.add(employee);
}
System.out.println(employees);
return employees;
}
}
/**
* @author Ranga Reddy
* @version 1.0
*/
public interface EmployeeService {
public long createEmployee(Employee employee);
public Employee updateEmployee(Employee employee);
public void deleteEmployee(long id);
public List<Employee> getAllEmployees();
public Employee getEmployee(long id);
public List<Employee> getAllEmployees(String employeeName);
}
EmployeeServiceImpl.java
package com.ranga.service.impl;
import com.ranga.dao.EmployeeDAO;
import com.ranga.entity.Employee;
import com.ranga.service.EmployeeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import java.util.List;
/**
* @author Ranga Reddy
* @version 1.0
*/
@Service
@Transactional
public class EmployeeServiceImpl implements EmployeeService {
public EmployeeServiceImpl() {
System.out.println("EmployeeServiceImpl()");
}
@Autowired
private EmployeeDAO employeeDAO;
@Override
import org.springframework.web.servlet.ModelAndView;
/**
* @author Ranga Reddy
* @version 1.0
*/
@Controller
public class EmployeeController {
private static final Logger logger =
Logger.getLogger(EmployeeController.class);
public EmployeeController() {
System.out.println("EmployeeController()");
}
@Autowired
private EmployeeService employeeService;
@RequestMapping("createEmployee")
public ModelAndView createEmployee(@ModelAttribute Employee employee)
{
logger.info("Creating Employee. Data: "+employee);
return new ModelAndView("employeeForm");
}
@RequestMapping("editEmployee")
public ModelAndView editEmployee(@RequestParam long id, @ModelAttribute
Employee employee) {
logger.info("Updating the Employee for the Id "+id);
employee = employeeService.getEmployee(id);
return new ModelAndView("employeeForm", "employeeObject", employee);
}
@RequestMapping("saveEmployee")
public ModelAndView saveEmployee(@ModelAttribute Employee employee) {
logger.info("Saving the Employee. Data : "+employee);
if(employee.getId() == 0){ // if employee id is 0 then creating the employee
src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.3/jquery.min.js"></script>
<script
src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.5/js/bootstrap.min.js"></sc
ript>
<%-- <script src="<c:url value="/resources/js/jquery-2.1.3.js"/>"></script>
<script src="<c:url value="/resources/js/bootstrap.min.js"/>"></script>
--%>
</body>
</html>
employeeForm.jsp
<%-User: Ranga Reddy
Date: 09/05/2015
Time: 6:52 PM
--%>
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8" %>
<%@ 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>
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta name="author" content="Ranga Reddy">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Employee Information</title>
<!-- Bootstrap CSS -->
<%-- <link href="<c:url value="/resources/css/bootstrap.min.css" />"
rel="stylesheet"> --%>
<link rel="stylesheet"
href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.5/css/bootstrap.min.css">
<style type="text/css">
.myrow-container{
margin: 20px;
}
</style>
</head>
<body class=".container-fluid">
<div class="container myrow-container">
<div class="panel panel-success">
<div class="panel-heading">
<h3 class="panel-title">
Employee Details
</h3>
</div>
<div class="panel-body">
<form:form id="employeeRegisterForm" cssClass="form-horizontal"
modelAttribute="employee" method="post" action="saveEmployee">
<div class="form-group">
<div class="control-label col-xs-3"> <form:label path="name"
>Name</form:label> </div>
<div class="col-xs-6">
<form:hidden path="id" value="${employeeObject.id}"/>
<form:input cssClass="form-control" path="name" value="$
{employeeObject.name}"/>
</div>
</div>
<div class="form-group">
<form:label path="age" cssClass="control-label col-xs3">Age</form:label>
<div class="col-xs-6">
<form:input cssClass="form-control" path="age" value="$
{employeeObject.age}"/>
</div>
</div>
<div class="form-group">
<div class="control-label col-xs-3"><form:label
path="salary">Salary</form:label></div>
<div class="col-xs-6">
<form:input cssClass="form-control" path="salary" value="$
{employeeObject.salary}"/>
</div>
</div>
<div class="form-group">
<div class="row">
<div class="col-xs-4">
</div>
<div class="col-xs-4">
<input type="submit" id="saveEmployee" class="btn btnprimary" value="Save" onclick="return submitEmployeeForm();"/>
</div>
<div class="col-xs-4">
</div>
</div>
</div>
</form:form>
</div>
</div>
</div>
<script
src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.3/jquery.min.js"></script>
<script
src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.5/js/bootstrap.min.js"></sc
ript>
<script type="text/javascript">
function submitEmployeeForm() {
// getting the employee form values
var name = $('#name').val().trim();
var age = $('#age').val();
var salary = $('#salary').val();
if(name.length ==0) {