• 四、将SessionFactory做成成员属性


    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);
        }
    }
  • 相关阅读:
    maya绝招(1-20)
    maya 操作自我整理(二)
    maya 操作自我整理(一)
    让IE6 IE7 IE8 IE9 IE10 IE11支持Bootstrap的解决方法
    SAP MM Consignment 寄售库存
    java或者jsp中修复会话标识未更新漏洞
    强大!基于拖放布局的 Twitter Bootstrap 网站生成器
    mysql 2003 10038 连接不上的解决
    STRUTS2 标签 循环次数
    tomcat 启用Gzip 压缩进行优化
  • 原文地址:https://www.cnblogs.com/myz666/p/8434734.html
Copyright © 2020-2023  润新知