spring_事务管理 TransactionManager

1.spring中的TransactionManager接口

DateSourceTransactionManager 用于JDBC的事务管理

HibernateTransactionManager用于Hibernate的事务管理

JpaTransactionManager 用于Jpa的事物管理

2.spring中TransactionManager接口的定义(源码)

事务的属性介绍:这里定义了传播行为、隔离级别、超时时间、是否只读

3.转账案例

(1)项目结构

spring_事务管理 TransactionManager_第1张图片

(2)导入jar

spring_事务管理 TransactionManager_第2张图片

(3)建立数据库测试数据

CREATE DATABASE springjdbc
USE springjdbc
CREATE TABLE `ar_account` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `username` varchar(255) DEFAULT NULL,
  `money` double(255,0) DEFAULT NULL,
  PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=3 DEFAULT CHARSET=utf8;

-- ----------------------------
-- Records of ar_account
-- ----------------------------
INSERT INTO `ar_account` VALUES ('1', '张三', '60');
INSERT INTO `ar_account` VALUES ('2', '李四', '40');

(4)建立日志配置(log4j.properties)

# Global logging configuration
log4j.rootLogger=info, stdout
# Console output...
log4j.appender.stdout=org.apache.log4j.ConsoleAppender
log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
log4j.appender.stdout.layout.ConversionPattern=%5p [%t] - %m%n

(5)建立数据库配置(db.properties)

jdbc.driver=com.mysql.jdbc.Driver
jdbc.url=jdbc:mysql://localhost:3306/springjdbc
jdbc.username=root
jdbc.password=root

(6)建立接口 AccountDao类与实现AccountDaoImpl类

package com.linxin.spring.dao;

public interface AccountDao {
	//加钱
	void addMoney(Integer id,Double money);
	//减钱
	void subMoney(Integer id,Double money);

}
package com.linxin.spring.dao;

import javax.annotation.Resource;

import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Repository;

@Repository("accountDao")
public class AccountDaoImpl implements AccountDao {
	@Resource(name="jdbctemplate")
	private JdbcTemplate jt;
	
	@Override
	public void addMoney(Integer id, Double money) {
		String sql= "update ar_account set money = money + ? where id= ?";
		jt.update(sql,money,id);
	}

	@Override
	public void subMoney(Integer id, Double money) {
		String sql= "update ar_account set money = money - ? where id= ?";
		jt.update(sql,money,id);

	}

}

(7)创建AccountService接口类与实现类AccountServiceImpl

package com.linxin.spring.service;

public interface AccountService {
	void transfer(Integer from, Integer to,Double money);
}
package com.linxin.spring.service;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;

import com.linxin.spring.dao.AccountDao;

@Service("accountService")
public class AccountServiceImpl implements AccountService {
	@Resource(name="accountDao")
	private AccountDao accountDao;
	@Override
	public void transfer(Integer from, Integer to, Double money) {
		accountDao.addMoney(to, money);
		int a=1/0;
		accountDao.subMoney(from, money);
	}

}

(8)创建spring配置文件application.xml



		
		
		
		
		
		
		
		
			
			
			
			
		
		
		
			
		
		
		
			
			
		
		
		
			
				
				
				

			
		
		
		
			
			
			
		

(9)测试类

package com.linxin.spring.Testtx;

import javax.annotation.Resource;

import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;

import com.linxin.spring.service.AccountService;

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration("classpath:applicationContext.xml")
public class TestTx {
	@Resource(name="accountService")
	private AccountService accountService;
	@Test
	public void testTransfer() {
		accountService.transfer(2, 1, 30.0);
	}

}

 

你可能感兴趣的:(Spring,MySQL)