spring 与事务相关代码

package cn.itcast.spring.hibernate;

import java.util.List;

import org.springframework.transaction.TransactionStatus;
import org.springframework.transaction.support.TransactionCallback;
import org.springframework.transaction.support.TransactionTemplate;

import cn.itcast.spring.domain.Customer;

public class CustomerServiceImpl implements CustomerService {
	//dao
	private CustomerDao dao ;
	
	//事务模板,封装事务管理的样板代码
	private TransactionTemplate tt ;
	//注入事务模板
	public void setTt(TransactionTemplate tt) {
		this.tt = tt;
	}

	//注入dao
	public void setDao(CustomerDao dao) {
		this.dao = dao;
	}

	public List findCustomerByName(String name) {
		return dao.findCustomerByName(name);
	}

	public void insertCustomer(Customer c) {
		dao.insertCustomer(c);
	}

	/**
	 * 批量保存, 编程式事务管理
	 */
	public void saveCustomers(final List list) {
//		for(Customer c : list){
//			dao.insertCustomer(c);
//		}
		tt.execute(new TransactionCallback(){
			public Object doInTransaction(TransactionStatus status) {
				try {
					for(Customer c : list){
						dao.insertCustomer(c);
					}
				} catch (RuntimeException e) {
					e.printStackTrace();
					//设置回滚
					status.setRollbackOnly();
				}
				return null;
			}});
	}

	public void updateCustomer(Customer c) {
		dao.updateCustomer(c);
	}
}


 

 

  声明式:

package cn.itcast.spring.hibernate;

import java.util.List;

import org.springframework.aop.framework.AopContext;
import org.springframework.aop.support.AopUtils;

import cn.itcast.spring.domain.Customer;

/**
 * 声明式事务管理,通过aop框架实现
 */
public class CustomerServiceDeclareImpl implements CustomerService {
	//dao
	private CustomerDao dao ;

	//注入dao
	public void setDao(CustomerDao dao) {
		this.dao = dao;
	}

	public List findCustomerByName(String name) {
		return dao.findCustomerByName(name);
	}

	public void insertCustomer(Customer c) {
		dao.insertCustomer(c);
	}

	/**
	 * 批量保存, 编程式事务管理
	 */
	public void saveCustomers(final List list) {
		CustomerService proxy = (CustomerService) AopContext.currentProxy();
		System.out.println(proxy);
		for(Customer c : list){
			proxy.insertCustomer(c);
		}
	}

	public void updateCustomer(Customer c) {
		dao.updateCustomer(c);
	}
}


 

  配置文件:

 



    
    
    
    
        
        
        
        

        
        
        
        
    
    
    
    
        
        
        
            
                ${hibernate.dialect}
                ${hibernate.show_sql}
                ${hibernate.hbm2ddl.auto}
            
        
        
        
            
                classpath:cn/itcast/spring/domain
            
        
        
    
    
    
    
        
    
    
    
    
        
    
    
    
    
        
    
    
    
    
        
    
    
    
    
        
        
        
        
            
                PROPAGATION_MANDATORY,ISOLATION_DEFAULT
                PROPAGATION_REQUIRED,ISOLATION_DEFAULT
                PROPAGATION_REQUIRED,ISOLATION_DEFAULT
                PROPAGATION_REQUIRED,ISOLATION_DEFAULT
                
                PROPAGATION_REQUIRED,ISOLATION_DEFAULT,readOnly
                PROPAGATION_REQUIRED,ISOLATION_DEFAULT,readOnly
                PROPAGATION_REQUIRED,ISOLATION_DEFAULT,readOnly
                
                PROPAGATION_REQUIRED,ISOLATION_DEFAULT,readOnly
            
        
    
    
    
    
        
            cn.itcast.spring.hibernate.CustomerService
        
        
        
        
    


 

你可能感兴趣的:(spring 与事务相关代码)