spring事务系列(1)--编程式事务管理

对于事务的测试,先大家一个转账的环境

创建数据表account 

<span style="font-size:18px;">创建数据表account 
CREATE TABLE `account` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `name` varchar(20) NOT NULL,
  `money` double DEFAULT NULL,
  PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=4 DEFAULT CHARSET=utf8;
INSERT INTO `account` VALUES ('1', 'aaa', '1000');
INSERT INTO `account` VALUES ('2', 'bbb', '1000');
INSERT INTO `account` VALUES ('3', 'ccc', '1000');</span>

新建一个web工程,引入spring的基本开发包  jar包下载链接:http://pan.baidu.com/s/1gekOoWb

在src 下加入log4j.properties 和spring开发最全的约束文档applicationContext.xml

<span style="font-size:18px;"><?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
     xmlns:context="http://www.springframework.org/schema/context"
     xmlns:aop="http://www.springframework.org/schema/aop"
     xmlns:tx="http://www.springframework.org/schema/tx"
     xmlns:task="http://www.springframework.org/schema/task"
     xsi:schemaLocation="http://www.springframework.org/schema/beans
         http://www.springframework.org/schema/beans/spring-beans-3.1.xsd
         http://www.springframework.org/schema/context
         http://www.springframework.org/schema/context/spring-context-3.1.xsd
         http://www.springframework.org/schema/aop
         http://www.springframework.org/schema/aop/spring-aop-3.1.xsd
         http://www.springframework.org/schema/tx
         http://www.springframework.org/schema/tx/spring-tx-3.1.xsd
         http://www.springframework.org/schema/task
		 http://www.springframework.org/schema/task/spring-task-3.1.xsd"></span>
<span style="font-size:18px;"></beans></span>

再引入jdbc.properties


在src下建立包,然后编写AccountService、AccountServiceImpl、AccountDao、AccountDaoImpl

<span style="font-size:18px;">package com.yeshuai.spring.demo1;

public interface AccountService {

	/**
	 * @param out	:转出账号
	 * @param in	:转入账号
	 * @param money	:转账金额
	 */
	public void transfer(String out,String in,Double money);
}
</span>


<span style="font-size:18px;">public class AccountServiceImpl implements AccountService {
	
	//注入转账的DAO
	private AccountDao accountDao;
	
	
	//注入事务管理的模板
	private TransactionTemplate transactionTemplate;

	/**
	 * @param out	:转出账号
	 * @param in	:转入账号
	 * @param money	:转账金额
	 */
	@Override
	public void transfer(final String out, final String in, final Double money) {
		/*accountDao.outMoney(out, money);
		//int i = 1/0;
		accountDao.inMoney(in, money);*/
		
		
		transactionTemplate.execute(new TransactionCallbackWithoutResult() {

			@Override
			protected void doInTransactionWithoutResult(TransactionStatus transactionStatus) {
				accountDao.outMoney(out, money);
				//int i = 1/0;
				accountDao.inMoney(in, money);
			}
		});
	}

	public void setAccountDao(AccountDao accountDao) {
		this.accountDao = accountDao;
	}

	public void setTransactionTemplate(TransactionTemplate transactionTemplate) {
		this.transactionTemplate = transactionTemplate;
	}

}
</span>

<span style="font-size:18px;">package com.yeshuai.spring.demo1;

public interface AccountDao {

	/**
	 * @param out	:转出账号
	 * @param money	:转账金额
	 */
	public void outMoney(String out,Double money);
	
	/**
	 * 
	 * @param in	:转入账号
	 * @param money	:转账金额
	 */
	public void inMoney(String in,Double money);
}
</span>

编程是事务的实现: dao层访问数据库可以继承JdbcDaoSupport(看这个类的源码,可以看见,里面有jdbcTemplate和DataSource两个属性)

在配置文件中注入这两个类都是可以获得JdbcTemplate的,然后就可以写sql了

<span style="font-size:18px;">package com.yeshuai.spring.demo1;

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

public class AccountDaoImpl extends JdbcDaoSupport implements AccountDao {

	/**
	 * @param out	:转出账号
	 * @param money	:转账金额
	 */
	@Override
	public void outMoney(String out, Double money) {
		String sql = "update account set money = money=? where name = ?";
		this.getJdbcTemplate().update(sql, money, out);
	}

	/**
	 * @param in	:转入账号
	 * @param money	:转账金额
	 */
	@Override
	public void inMoney(String in, Double money) {
		String sql = "update account set money = money+? where name = ?";
		this.getJdbcTemplate().update(sql,money,in);
	}

}
</span>

测试:

<span style="font-size:18px;">package com.yeshuai.spring.demo1;

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;

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration("classpath:applicationContext1.xml")
public class TransactionTest {

	@Resource(name="accountService")
	private AccountService accountService;
	
	@Test
	public void demo1(){
		accountService.transfer("aaa", "bbb", 200d);
	}
}
</span>

编程式事务完整xml

<span style="font-size:18px;"><?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
     xmlns:context="http://www.springframework.org/schema/context"
     xmlns:aop="http://www.springframework.org/schema/aop"
     xmlns:tx="http://www.springframework.org/schema/tx"
     xmlns:task="http://www.springframework.org/schema/task"
     xsi:schemaLocation="http://www.springframework.org/schema/beans
         http://www.springframework.org/schema/beans/spring-beans-3.1.xsd
         http://www.springframework.org/schema/context
         http://www.springframework.org/schema/context/spring-context-3.1.xsd
         http://www.springframework.org/schema/aop
         http://www.springframework.org/schema/aop/spring-aop-3.1.xsd
         http://www.springframework.org/schema/tx
         http://www.springframework.org/schema/tx/spring-tx-3.1.xsd
         http://www.springframework.org/schema/task
		 http://www.springframework.org/schema/task/spring-task-3.1.xsd">

	<!-- 引入外部的属性文件 -->
	<context:property-placeholder location="classpath:jdbc.properties"/>
	
	<!-- 配置c3p0连接池 -->
	<bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
		<property name="driverClass" value="${jdbc.driverClass}" />
		<property name="jdbcUrl" value="${jdbc.url}" />
		<property name="user" value="${jdbc.username}" />
		<property name="password" value="${jdbc.password}" />
	</bean>
	
	<!-- 配置业务层类 -->
	<bean id="accountService" class="com.yeshuai.spring.demo1.AccountServiceImpl">
		<property name="accountDao" ref="accountDao" />
		<!-- 注入事务管理的模板 -->
		<property name="transactionTemplate" ref="transactionTemplate" />
	</bean>
	
	<!-- 配置DAO类(简化,会自动配置JdbcTemplate) -->
	<bean id="accountDao" class="com.yeshuai.spring.demo1.AccountDaoImpl">
		<property name="dataSource" ref="dataSource" />
	</bean>
	
	<!-- 配置DAO类(未简化) --> 一般选择上面简化的
	<!-- <bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate">
		<property name="dataSource" ref="dataSource" />
	</bean>
	<bean id="accountDao" class="com.yeshuai.spring.demo1.AccountDaoImpl">
		<property name="jdbcTemplate" ref="jdbcTemplate" />
	</bean> -->
	
	<!-- ==================================1.编程式的事务管理=============================================== -->
	<!-- 配置事务管理器 -->
	<bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
		<property name="dataSource" ref="dataSource" />
	</bean>
	
	<!-- 配置事务管理的模板:Spring为了简化事务管理的代码而提供的类 -->
	<bean id="transactionTemplate" class="org.springframework.transaction.support.TransactionTemplate">
		<property name="transactionManager" ref="transactionManager"/>
	</bean>
	
</beans>
</span>



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值