0% found this document useful (0 votes)
6 views

Spring Hibernate Intigration

The document defines an Employee model class with ID, name, and salary fields and corresponding getters and setters. It then defines a controller and DAO interface to handle employee registration and data access. Finally, it configures the Spring application context to wire these components together.

Uploaded by

Raghu Gowda
Copyright
© © All Rights Reserved
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
6 views

Spring Hibernate Intigration

The document defines an Employee model class with ID, name, and salary fields and corresponding getters and setters. It then defines a controller and DAO interface to handle employee registration and data access. Finally, it configures the Spring application context to wire these components together.

Uploaded by

Raghu Gowda
Copyright
© © All Rights Reserved
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
You are on page 1/ 9

package com.app.

model;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.Id;
import javax.persistence.Table;

@Entity
@Table(name="emp")
public class Employee {
@Id
@Column(name="eid")
private int empId;
@Column(name="ename")
private String empName;
@Column(name="esal")
private double empSal;

public int getEmpId() {


return empId;
}
public void setEmpId(int empId) {
this.empId = empId;
}
public String getEmpName() {
return empName;
}
public void setEmpName(String empName) {
this.empName = empName;
}
public double getEmpSal() {
return empSal;
}
public void setEmpSal(double empSal) {
this.empSal = empSal;
}
@Override
public String toString() {
return "Employee [empId=" + empId + ", empName=" +
empName
+ ", empSal=" + empSal + "]";
}
}
package com.app.controller;

import
org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
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.app.model.Employee;
import com.app.service.IEmployeeService;

@Controller
public class EmployeeController {

@Autowired
private IEmployeeService empService;

@RequestMapping(value="/regiterpage")
public ModelAndView getPage(){
ModelAndView mav=new ModelAndView("Register");
return mav;
}

@RequestMapping(value="/showData",method=RequestMethod.POST)
public ModelAndView
showDetails(@ModelAttribute("employee")Employee emp){

//logic to call service layer


Integer
empId=empService.insertEmployeeDetails(emp);
ModelAndView mav=new ModelAndView("Details");
mav.addObject("id",empId);
return mav;

}
package com.app.dao;

import com.app.model.Employee;

public interface IEmployeeDao {

public Integer insertEmployeeDetails(Employee emp);


}
package com.app.dao.impl;

import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;

import com.app.dao.IEmployeeDao;
import com.app.model.Employee;

public class EmployeeDaoImplHibernate implements


IEmployeeDao {

private SessionFactory sessionFacorty;

public void setSessionFacorty(SessionFactory


sessionFacorty) {
this.sessionFacorty = sessionFacorty;
}

@Override
public Integer insertEmployeeDetails(Employee emp) {
Session session=null;
Transaction tx=null;
Integer empId=null;
try {
session=sessionFacorty.openSession();
tx=session.beginTransaction();
tx.begin();

empId=(Integer)session.save(emp);

tx.commit();
} catch (Exception e) {
tx.rollback();
}finally{
if(session!=null){
session.flush();
session.close();
}
}

return empId;
}

}
package com.app.service;

import com.app.model.Employee;

public interface IEmployeeService {


public Integer insertEmployeeDetails(Employee emp);

}
package com.app.service.impl;

import com.app.dao.IEmployeeDao;
import com.app.model.Employee;
import com.app.service.IEmployeeService;

public class EmployeeServiceImpl implements


IEmployeeService{
private IEmployeeDao empDao;

public void setEmpDao(IEmployeeDao empDao) {


this.empDao = empDao;
}

@Override
public Integer insertEmployeeDetails(Employee emp) {
Integer empId=empDao.insertEmployeeDetails(emp);
return empId;
}

}
<?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
"

xsi:schemaLocation="http://www.springframework.org/schema/be
ans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-
context.xsd
">
<context:annotation-config/>
<context:component-scan base-package="com.app"/>
<!-- View Resolver Configuration -->
<bean
class="org.springframework.web.servlet.view.InternalResource
ViewResolver">
<property name="prefix" value="/WEB-INF/jsps/"/>
<property name="suffix" value=".jsp"/>
</bean>
<!-- Data Source Configuration -->
<bean
class="org.springframework.jdbc.datasource.DriverManagerData
Source" name="datasourceObj">
<property name="driverClassName"
value="com.mysql.jdbc.Driver"/>
<property name="url"
value="jdbc:mysql://localhost:3306/test"/>
<property name="username" value="root"/>
<property name="password" value="root"/>
</bean>

<bean
class="org.springframework.orm.hibernate3.annotation.Annotat
ionSessionFactoryBean" name="sessionFactoryObj">
<property name="dataSource" ref="datasourceObj"/>
<property name="hibernateProperties">
<props>
<prop
key="hibernate.dialect">org.hibernate.dialect.MySQLDialect</
prop>
<prop key="hibernate.show_sql">true</prop>
<prop
key="hibernate.hbm2ddl.auto">update</prop>
</props>
</property>
<property name="annotatedClasses">
<list>
<value>com.app.model.Employee</value>
</list>
</property>
</bean>

<bean class="com.app.dao.impl.EmployeeDaoImplHibernate"
name="empDaoObj">
<property name="sessionFacorty">
<ref bean="sessionFactoryObj"/>
</property>
</bean>

<bean class="com.app.service.impl.EmployeeServiceImpl"
name="empServiceObj">
<property name="empDao">
<ref bean="empDaoObj"/>
</property>
</bean>

</beans>
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-
instance"
xmlns="http://java.sun.com/xml/ns/javaee"
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">

<servlet>
<servlet-name>project</servlet-name>
<servlet-
class>org.springframework.web.servlet.DispatcherServlet</
servlet-class>
</servlet>

<servlet-mapping>
<servlet-name>project</servlet-name>
<url-pattern>/app/*</url-pattern>
</servlet-mapping>

</web-app>

<%@ page language="java" contentType="text/html;


charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01
Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html;
charset=ISO-8859-1">
<title>Insert title here</title>
</head>
<body>
<form action="showData" method="POST">
<pre>
Enter Id : <input type="text" name="empId"/>
Enter Name : <input type="text" name="empName"/>
Enter Sal :<input type="text" name="empSal"/>
<input type="submit" value="Register">
<input type="reset" value="Clear">

</pre>
</form>
</body>
</html>

<%@ page language="java" contentType="text/html;


charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01
Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html;
charset=ISO-8859-1">
<title>Insert title here</title>
</head>
<body>
<pre>
Inserted Details for Emp Id: ${id}
</pre>

</body>
</html>

You might also like