Spring MVC 注解事务回滚

Spring MVC + mybatis + mysql 事务回滚服务层,用@Transactional方式注解事务。

 

1、mysql建表设置引擎为INNODB,如:

 ALTER TABLE goods_order ENGINE = INNODB; 

 

2、服务层

public interface IGoodsOrderService {
	
	/**
	 * 添加订单
	 * @param order
	 * @return
	 */
	boolean addOrder(GoodsOrder order) throws Exception;

}

 

@Service
public class GoodsOrderServiceImpl implements IGoodsOrderService {
	@Autowired
	private IGoodsOrderDao orderDao;
	@Autowired
	private IGoodsOrderDetailDao orderDetailDao;
	@Autowired
	private IGoodsOrderStatusDao orderStatusDao;
	
	@Override
	@Transactional(readOnly=false,propagation=Propagation.REQUIRED,rollbackFor=Exception.class)
	public boolean addOrder(GoodsOrder order) throws Exception {
		boolean result = false;
		try {
			if (null != order.getDetails() && order.getDetails().size() > 0) {
				order.setOrderNum(order.getDetails().size());
				// 添加订单
				orderDao.addGoodsOrder(order);
				// 添加订单详细
				for (GoodsOrderDetail orderDetail : order.getDetails()) {
//					orderDetail.setOrderNo(order.getOrderNo());
					orderDetailDao.addGoodsOrderDetail(orderDetail);
				}
				result = true;
			}
		} catch (Exception e) {
                        //事务回滚异常需要抛出来
			throw e;
		}
		return result;
	}
}

 

3、配置applicationContext.xml




	
	
	
	
	

	
	
		
		
		
		
		  
          
         
	
	

	
	
		
		
		
	

	
	
		
		
	

	
	
		
	

	
	

 

4、spring-servlet.xml配置



	  
    
    
    
    
    	
    	  
    
	
	
	 
	 
	  
	  
	  
	  
	 
	 

 

你可能感兴趣的:(Spring)