web.xml File
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
id="WebApp_ID" version="2.5">
<display-name>Spring3MVC</display-name>
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
<servlet>
<servlet-name>spring</servlet-name>
<servlet-class>
org.springframework.web.servlet.DispatcherServlet
</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>spring</servlet-name>
<url-pattern>*.htm</url-pattern>
</servlet-mapping>
</web-app>
spring-servlet.xml File
<?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:tx="http://www.springframework.org/schema/tx"
xmlns:context="http://www.springframework.org/schema/context"
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.xsd">
<context:annotation-config />
<context:component-scan
base-package="com.employee" />
<bean id="viewResolver"
class="org.springframework.web.servlet.view.UrlBasedViewResolver">
<property name="viewClass"
value="org.springframework.web.servlet.view.JstlView" />
<property name="prefix" value="/WEB-INF/jsp/" />
<property name="suffix" value=".jsp" />
</bean>
<bean id="propertyConfigurer"
class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer"
p:location="/WEB-INF/jdbc.properties" />
<bean id="dataSource"
class="oracle.jdbc.pool.OracleDataSource" destroy-method="close"
p:URL="${jdbc.databaseurl}" p:user="${jdbc.username}"
p:password="${jdbc.password}" />
<bean id="sessionFactory"
class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
<property name="dataSource" ref="dataSource" />
<property name="configLocation">
<value>classpath:hibernate.cfg.xml</value>
</property>
<property name="configurationClass">
<value>org.hibernate.cfg.AnnotationConfiguration</value>
</property>
<property name="hibernateProperties">
<props>
<prop key="hibernate.dialect">${jdbc.dialect}</prop>
<prop key="hibernate.show_sql">true</prop>
</props>
</property>
</bean>
<tx:annotation-driven />
<bean id="transactionManager"
class="org.springframework.orm.hibernate3.HibernateTransactionManager">
<property name="sessionFactory" ref="sessionFactory" />
</bean>
</beans>
jdbc.properties
jdbc.driverClassName= oracle.jdbc.driver.OracleDriver
jdbc.dialect=org.hibernate.dialect.OracleDialect
jdbc.databaseurl= jdbc:oracle:thin:@192.168.25.178:1521:xe
jdbc.username=litmus
jdbc.password=litmus
hibernate-cfg.xml
<?xml version='1.0' encoding='utf-8'?>
<!DOCTYPE hibernate-configuration PUBLIC
"-//Hibernate/Hibernate Configuration DTD//EN"
"http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
<session-factory>
<mapping class="com.employee.Models.EmployeeModel" />
</session-factory>
</hibernate-configuration>
EmployeeController.java
/**
*
*/
package com.employee.Controller;
import java.util.ArrayList;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import com.employee.Models.EmployeeModel;
import com.employee.Services.EmployeeService;
/**
* @author A-5518
*
*/
@Controller
public class EmployeeController {
@Autowired
private EmployeeService employeeService;
@RequestMapping("/registerEmployee.htm")
public String showRegistration(ModelMap map){
System.out.println("Inside showRegistration Method");
map.addAttribute("EmployeeModel", new EmployeeModel());
return "newEmployee";
}
@RequestMapping("/addEmployee.htm")
public String addEmployee(ModelMap map, @ModelAttribute EmployeeModel employeeModel){
try{
System.out.println("Inside addEmployeee Method");
employeeService.SaveUsertoDatabase(employeeModel);
map.addAttribute("EmployeeModel", new EmployeeModel());
return "success";
}catch(Exception e){
System.out.println("Primary key violation error occured.");
return "error";
}
}
@RequestMapping("/listEmployee.htm")
public String listEmployee(ModelMap map){
System.out.println("Inside listEmployeee Method");
List<EmployeeModel> users = employeeService.getUserDetails();
map.addAttribute("users", users);
map.addAttribute("EmployeeModel", new EmployeeModel());
return "listEmployee";
}
@RequestMapping("/editEmployee.htm")
public String editEmployee(ModelMap map, @ModelAttribute EmployeeModel employeeModel){
System.out.println("Inside editEmployeee Method");
List<EmployeeModel> users = employeeService.getUserDetails();
EmployeeModel em=(EmployeeModel)users.get(users.indexOf(employeeModel));
ArrayList<EmployeeModel> singleUser=new ArrayList<EmployeeModel>();
singleUser.add(em);
map.addAttribute("singleUser", singleUser);
map.addAttribute("EmployeeModel", new EmployeeModel());
return "editEmployee";
}
@RequestMapping("/updateEmployee.htm")
public String updateEmployee(ModelMap map, @ModelAttribute EmployeeModel employeeModel){
System.out.println("Inside updateEmployeee Method");
employeeService.updateUserFromDatabase(employeeModel);
map.addAttribute("EmployeeModel", new EmployeeModel());
return "update";
}
@RequestMapping("/deleteEmployee.htm")
public String deleteEmployee(ModelMap map, @ModelAttribute EmployeeModel employeeModel){
System.out.println("Inside deleteEmployeee Method");
employeeService.deleteUserFromDatabase(employeeModel);
List<EmployeeModel> users = employeeService.getUserDetails();
map.addAttribute("users", users);
map.addAttribute("EmployeeModel", new EmployeeModel());
return "listEmployee";
}
}
EmployeeDao.java
/**
*
*/
package com.employee.Dao;
import java.util.List;
import com.employee.Models.EmployeeModel;
/**
* @author A-5518
*
*/
public interface EmployeeDao {
public void saveUser(EmployeeModel loginModel);
public void updateUser(EmployeeModel loginModel);
public void deleteUser(EmployeeModel loginModel);
public List<EmployeeModel> getUserDetails();
}
EmployeeDaoImpl.java
/**
*
*/
package com.employee.Dao;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.List;
import javax.sql.DataSource;
import org.hibernate.SessionFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.RowMapper;
import org.springframework.stereotype.Repository;
import org.springframework.transaction.annotation.Transactional;
import com.employee.Models.EmployeeModel;
/**
* @author A-5518
*
*/
@Repository
public class EmployeeDaoImpl implements EmployeeDao {
@Autowired
private SessionFactory sessionFactory;
@Autowired
private DataSource dataSource;
@Transactional
public void saveUser(EmployeeModel loginModel) {
// TODO Auto-generated method stub
System.out.println("Saving into Database");
sessionFactory.getCurrentSession().save(loginModel);
}
@Transactional
public void deleteUser(EmployeeModel loginModel) {
// TODO Auto-generated method stub
System.out.println("Deleting from Database");
sessionFactory.getCurrentSession().delete(loginModel);
}
@Transactional
public void updateUser(EmployeeModel loginModel) {
// TODO Auto-generated method stub
System.out.println("Update from Database");
sessionFactory.getCurrentSession().update(loginModel);
}
@Transactional
public List<EmployeeModel> getUserDetails() {
// TODO Auto-generated method stub
JdbcTemplate jdbcTemplate = new JdbcTemplate();
jdbcTemplate.setDataSource(dataSource);
String sql ="select * from employee_details_a5518";
RowMapper<EmployeeModel> mapper = new RowMapper<EmployeeModel>(){
@Override
public EmployeeModel mapRow(ResultSet rs, int arg1)
throws SQLException {
// TODO Auto-generated method stub
EmployeeModel employeeModel = new EmployeeModel();
employeeModel.setEmployeeId(rs.getString(1));
employeeModel.setName(rs.getString(2));
employeeModel.setAddress(rs.getString(3));
return employeeModel;
}
};
List<EmployeeModel> users = jdbcTemplate.query(sql, mapper);
return users;
}
}
EmployeeModel.java
/**
*
*/
package com.employee.Models;
import javax.persistence.Entity;
import javax.persistence.Id;
import javax.persistence.Table;
import javax.persistence.Column;
/**
* @author A-5518
*
*/
@Entity
@Table(name="employee_details_a5518")
public class EmployeeModel {
private String employeeId;
private String name;
private String address;
@Id
@Column(name="employeeid")
public String getEmployeeId() {
return employeeId;
}
public void setEmployeeId(String employeeId) {
this.employeeId = employeeId;
}
@Column(name="name")
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
@Column(name="address")
public String getAddress() {
return address;
}
public void setAddress(String address) {
this.address = address;
}
/* (non-Javadoc)
* @see java.lang.Object#hashCode()
*/
@Override
public int hashCode() {
final int prime = 31;
int result = 1;
result = prime * result
+ ((employeeId == null) ? 0 : employeeId.hashCode());
return result;
}
/* (non-Javadoc)
* @see java.lang.Object#equals(java.lang.Object)
*/
@Override
public boolean equals(Object obj) {
if (this == obj)
return true;
if (obj == null)
return false;
if (getClass() != obj.getClass())
return false;
EmployeeModel other = (EmployeeModel) obj;
if (employeeId == null) {
if (other.employeeId != null)
return false;
} else if (!employeeId.equals(other.employeeId))
return false;
return true;
}
}
EmployeeService.java
/**
*
*/
package com.employee.Services;
import java.util.List;
import com.employee.Models.EmployeeModel;
/**
* @author A-5518
*
*/
public interface EmployeeService {
public void SaveUsertoDatabase(EmployeeModel loginModel);
public void deleteUserFromDatabase(EmployeeModel loginModel);
public void updateUserFromDatabase(EmployeeModel loginModel);
public List<EmployeeModel> getUserDetails();
}
EmployeeServiceImpl.java
/**
*
*/
package com.employee.Services;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import com.employee.Dao.EmployeeDao;
import com.employee.Models.EmployeeModel;
/**
* @author A-5518
*
*/
@Service
public class EmployeeServiceImpl implements EmployeeService {
@Autowired
private EmployeeDao employeeDao;
@Transactional
public void SaveUsertoDatabase(EmployeeModel loginModel) {
// TODO Auto-generated method stub
employeeDao.saveUser(loginModel);
}
@Transactional
public void deleteUserFromDatabase(EmployeeModel loginModel) {
// TODO Auto-generated method stub
employeeDao.deleteUser(loginModel);
}
@Transactional
public void updateUserFromDatabase(EmployeeModel loginModel) {
// TODO Auto-generated method stub
employeeDao.updateUser(loginModel);
}
@Transactional
public List<EmployeeModel> getUserDetails() {
// TODO Auto-generated method stub
return employeeDao.getUserDetails();
}
}