zoukankan      html  css  js  c++  java
  • 五、配置事务管理器

    1.在applicationContext.xml中配置事务管理器,统一管理sessionFactory的事务,这样EmployeeService中的函数就不用每次都beginTransaction()了

      1.1配置事务管理器

    <!-- 配置事物管理器,统一管理sessionFactory的事务 -->
        <bean id="txManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager">
            <property name="sessionFactory" ref="sessionFactory"></property>
        </bean>
        <!-- 启用事务注解扫描 -->
        <tx:annotation-driven transaction-manager="txManager"/>

      1.2EmployeeService中添加注解

    //这里配置注解用处是让spring的事务管理器接管该Service的事务(所有函数),要是放在函数头上就是管理一个函数
    @Transactional
    public class EmployeeService implements EmployeeServiceInterface {

    2.修改EmployeeService中函数代码,删除beginTransaction()和openSession,改为getCurrentSession()

    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 org.springframework.transaction.annotation.Transactional;
    
    import com.myz.domain.Employee;
    import com.myz.service.interfaces.EmployeeServiceInterface;
    
    //这里配置注解用处是让spring的事务管理器接管该Service的事务(所有函数),要是放在函数头上就是管理一个函数
    @Transactional
    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.getCurrentSession();
            openSession.save(e);
        }
        
        //根据id删除雇员
        public void delEmployee(Serializable id) {
            // TODO Auto-generated method stub
            Session openSession = sessionFactory.getCurrentSession();
            Employee e=(Employee) openSession.load(Employee.class, id);
            openSession.delete(e);
        }
    
        //显示所有雇员
        public List<Employee> showEmployee() {
            // TODO Auto-generated method stub
            Session openSession = sessionFactory.getCurrentSession();
            List<Employee> list = openSession.createQuery("from Employee").list();
            return list;
        }
    
        //更新雇员信息
        public void updEmployee(Employee e) {
            // TODO Auto-generated method stub
            Session openSession = sessionFactory.getCurrentSession();
            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());
        }
    
    }

    3.测试,虽然EmployeeService的函数中没有事务,但是依然能够执行增删改的操作(因为配置了事务管理器),成功!

    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;
    import com.myz.service.interfaces.EmployeeServiceInterface;
    
    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(7, "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);
        }
    }
  • 相关阅读:
    Flink-v1.12官方网站翻译-P029-User-Defined Functions
    Flink-v1.12官方网站翻译-P028-Custom Serialization for Managed State
    Flink-v1.12官方网站翻译-P027-State Schema Evolution
    Flink-v1.12官方网站翻译-P026-State Backends
    Flink-v1.12官方网站翻译-P025-Queryable State Beta
    Flink-v1.12官方网站翻译-P024-Checkpointing
    Flink-v1.12官方网站翻译-P023-The Broadcast State Pattern
    Flink-v1.12官方网站翻译-P022-Working with State
    java笔记五——数组
    java笔记四——java基础知识(二)
  • 原文地址:https://www.cnblogs.com/myz666/p/8434842.html
Copyright © 2011-2022 走看看