Spring学习笔记——事务

1.1事务中的基本概念

     事务是指逻辑上的一组操作要么全部成功,要么全部失败

      Spring学习笔记——事务_第1张图片

1.2 Spring管理事物的接口

PlatformTransactionManager:平台事务管理
*******真正管理事物的对象
    org.springframework.jdbc.datasource.DataSourceTransactionManager:使用JDBC或mybatis持久化数据时使用
     org.springframework.orm.hibernate 5 .HibernateTransactionManager:使用Hibernate持久化数据时使用

1.3 Spring声明式事务的实例    

(编程时事务省略)
dao层
package com.zhfy.project.dao.impl;

import org.springframework.jdbc.core.support.JdbcDaoSupport;

import com.zhfy.project.dao.IUserDao;

public class UserDaoImpl extends JdbcDaoSupport implements IUserDao{

	public void increaseMoney(Integer id, Double money) {
		
		getJdbcTemplate().update("update t_user set money = money+? where id = ? ", money,id);
		
	}

	public void decreaseMoney(Integer id, Double money) {

		getJdbcTemplate().update("update t_user set money = money-? where id = ? ", money,id);
	}
	
}

service层
package com.zhfy.project.service.impl;


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

import com.zhfy.project.dao.IUserDao;
import com.zhfy.project.service.IUserService;

public class UserServiceImpl implements IUserService {
	
	private IUserDao userDao;
	private TransactionTemplate tt;
	
	public void transfer(final Integer from,final Integer to,final Double money) {
		tt.execute(new TransactionCallbackWithoutResult() {
			@Override
			protected void doInTransactionWithoutResult(TransactionStatus arg0) {
				//减钱
				userDao.decreaseMoney(from, money);
				int i = 1/0;
				//加钱
				userDao.increaseMoney(to, money);
			}
		});
		
	}

	public IUserDao getUserDao() {
		return userDao;
	}

	public void setUserDao(IUserDao userDao) {
		this.userDao = userDao;
	}

	public TransactionTemplate getTt() {
		return tt;
	}

	public void setTt(TransactionTemplate tt) {
		this.tt = tt;
	}
}
配置文件


	
	
	  
	
		
	
	
	
		
	
	
	
		
			
			
			
			
			
			
			
			
			
			
		
	
	
	
		
		
		
		
	
	 
	 
	 			   
	 
	    
     
	 
	 
	   
	   
	 
	 
	 
	 
     
	 
	 
	    
		
		
		
	 
测试
package com.zhfy.project.test;

import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import com.zhfy.project.service.IUserService;

public class myProjectTest {
	@Test
	public void test(){
		//加载配置文件,创建容器
		ApplicationContext ac = new ClassPathXmlApplicationContext("applicationContextTX.xml");
		//想容器中要对象
        IUserService userService = (IUserService)ac.getBean("userservice");
        userService.transfer(1, 2, 10.0);
	  
	}
	
	 
}

1.4注解方式的声明试事务

service层
package com.zhfy.project.service.impl;


import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.transaction.support.TransactionTemplate;

import com.zhfy.project.dao.IUserDao;
import com.zhfy.project.service.IUserService;

public class UserServiceImpl implements IUserService {
	@Autowired
	private IUserDao userDao;
	@Autowired
	private TransactionTemplate tt;
	@Transactional
	public void transfer( Integer from,Integer to, Double money) {
			//减钱
			userDao.decreaseMoney(from, money);
			int i = 1/0;
			//加钱
			userDao.increaseMoney(to, money);
		
	}

	public IUserDao getUserDao() {
		return userDao;
	}

	public void setUserDao(IUserDao userDao) {
		this.userDao = userDao;
	}

	public TransactionTemplate getTt() {
		return tt;
	}

	public void setTt(TransactionTemplate tt) {
		this.tt = tt;
	}
}

配置文件


	
	
	  
	
		
	
	
	
		
	
	 
	 
     
	 			   
	 
	    
     
	 
	 
	   
	   
	 
	 
	 
	 
     
	 
	 
	    
		
		
		
	 






你可能感兴趣的:(spring)