1.将SessionFactory做成EmployeeService的成员属性
1.1在EmployeeService中添加私有属性
package com.myz.service.imps; import java.io.Serializable; import java.util.Date; import java.util.List; import org.hibernate.Session; import org.hibernate.SessionFactory; import org.hibernate.Transaction; import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; import com.myz.domain.Employee; import com.myz.service.interfaces.EmployeeServiceInterface; public class EmployeeService implements EmployeeServiceInterface { private SessionFactory sessionFactory; public SessionFactory getSessionFactory() { return sessionFactory; } public void setSessionFactory(SessionFactory sessionFactory) { this.sessionFactory = sessionFactory; } //增加雇员 public void addEmployee(Employee e) { // TODO Auto-generated method stub Session openSession = sessionFactory.openSession(); Transaction ts=openSession.beginTransaction(); openSession.save(e); ts.commit(); } //根据id删除雇员 public void delEmployee(Serializable id) { // TODO Auto-generated method stub Session openSession = sessionFactory.openSession(); Transaction ts=openSession.beginTransaction(); Employee e=(Employee) openSession.load(Employee.class, id); openSession.delete(e); ts.commit(); } //显示所有雇员 public List<Employee> showEmployee() { // TODO Auto-generated method stub Session openSession = sessionFactory.openSession(); List<Employee> list = openSession.createQuery("from Employee").list(); return list; } //更新雇员信息 public void updEmployee(Employee e) { // TODO Auto-generated method stub Session openSession = sessionFactory.openSession(); Transaction ts=openSession.beginTransaction(); Employee ee=(Employee) openSession.load(Employee.class, e.getId()); ee.setEmail(e.getEmail()); ee.setGrade(e.getGrade()); ee.setHiredate(e.getHiredate()); ee.setName(e.getName()); ee.setPassword(e.getPassword()); ee.setSalary(e.getSalary()); ts.commit(); } }
1.2在applicationContext.xml文件中配置EmployeeService,并注入属性
<bean id="employeeService" class="com.myz.service.imps.EmployeeService"> <property name="sessionFactory" ref="sessionFactory"></property> </bean>
2.测试,在com.myz.test.Test.java中对增删查改函数进行测试,成功!
package com.myz.test; import java.util.Date; import java.util.List; import org.hibernate.Session; import org.hibernate.SessionFactory; import org.hibernate.Transaction; import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; import com.myz.domain.Employee; import com.myz.service.imps.EmployeeService; public class Test { public static void main(String[] args) { ApplicationContext ac=new ClassPathXmlApplicationContext("applicationContext.xml"); EmployeeServiceInterface es=(EmployeeServiceInterface) ac.getBean("employeeService");// 增加一个雇员 // Employee e1=new Employee(1, "marry", "2466@qq.com", new Date(), 4500f, "123456", 1); // es.addEmployee(e1); //根据id删除一个雇员 // es.delEmployee(3); //显示所有雇员 // List<Employee> employees = es.showEmployee(); // for(Employee e:employees){ // System.out.println(e.getId()+" "+e.getName()); // } //更新3号雇员的信息 // Employee e3=new Employee(3, "rose", "24666@163.com", new Date(), 6000f, "123456", 2); // es.updEmployee(e3); } }