spring使用编程式事务管理实习银行中两个账户转账 (不推荐使用,事务操作侵入业务逻辑代码)
Spring 提供的事务模版类:org.springframework.transaction.support.TransactionTemplate
事务管理器:org.springframework.jdbc.datasource.DataSourceTransactionManager
BankDao接口
package com.java1234.dao;
public interface BankDao {
public void inMoney(int money,int userId);
public void outMoney(int money,int userId);
}
BankDaoImpl实现类
package com.java1234.dao.impl;
import org.springframework.jdbc.core.namedparam.MapSqlParameterSource;
import org.springframework.jdbc.core.namedparam.NamedParameterJdbcTemplate;
import com.java1234.dao.BankDao;
public class BankDaoImpl implements BankDao{
private NamedParameterJdbcTemplate namedParameterJdbcTemplate;
public void setNamedParameterJdbcTemplate(
NamedParameterJdbcTemplate namedParameterJdbcTemplate) {
this.namedParameterJdbcTemplate = namedParameterJdbcTemplate;
}
@Override
public void inMoney(int money, int userId) {
// TODO Auto-generated method stub
String sql="update t_count2 set count=count+:money where userId=:userId";
MapSqlParameterSource sps=new MapSqlParameterSource();
sps.addValue("money", money);
sps.addValue("userId", userId);
namedParameterJdbcTemplate.update(sql,sps);
}
@Override
public void outMoney(int money, int userId) {
// TODO Auto-generated method stub
String sql="update t_count set count=count-:money where userId=:userId";
MapSqlParameterSource sps=new MapSqlParameterSource();
sps.addValue("money", money);
sps.addValue("userId", userId);
namedParameterJdbcTemplate.update(sql,sps);
}
}
BankService 接口
package com.java1234.service;
public interface BankService {
/**
* A向B转账count元
* @param count
* @param userIdA
* @param userIdB
*/
public void transferAccounts(int count,int userIdA,int userIdB);
}
BankServiceImpl 实现类
package com.java1234.service.impl;
import org.springframework.transaction.TransactionStatus;
import org.springframework.transaction.support.TransactionCallbackWithoutResult;
import org.springframework.transaction.support.TransactionTemplate;
import com.java1234.dao.BankDao;
import com.java1234.service.BankService;
public class BankServiceImpl implements BankService{
private BankDao bankDao;
private TransactionTemplate transactionTemplate;
public void setBankDao(BankDao bankDao) {
this.bankDao = bankDao;
}
public void setTransactionTemplate(TransactionTemplate transactionTemplate) {
this.transactionTemplate = transactionTemplate;
}
@Override
public void transferAccounts(final int count, final int userIdA, final int userIdB) {
// TODO Auto-generated method stub
transactionTemplate.execute(new TransactionCallbackWithoutResult() {
@Override
protected void doInTransactionWithoutResult(TransactionStatus arg0) {
// TODO Auto-generated method stub
bankDao.outMoney(count, userIdA);
bankDao.inMoney(count, userIdB);
}
});
}
}
测试类
package com.java1234.test;
import org.junit.Before;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import com.java1234.service.BankService;
public class T {
private ApplicationContext ac;
@Before
public void setUp() throws Exception {
ac=new ClassPathXmlApplicationContext("beans.xml");
}
@Test
public void transferAccounts() {
BankService bankService=(BankService)ac.getBean("bankService");
bankService.transferAccounts(50, 1, 2);
}
}
beans.xml
<?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:aop="http://www.springframework.org/schema/aop"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd">
<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">
<property name="driverClassName" value="${jdbc.driverClassName}"/>
<property name="url" value="${jdbc.url}"/>
<property name="username" value="${jdbc.username}"/>
<property name="password" value="${jdbc.password}"/>
</bean>
<!-- jdbc事务管理器 -->
<bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="dataSource"></property>
</bean>
<bean id="transactionTemplate" class="org.springframework.transaction.support.TransactionTemplate">
<property name="transactionManager" ref="transactionManager"></property>
</bean>
<context:property-placeholder location="jdbc.properties"/>
<bean id="namedParameterJdbcTemplate" class="org.springframework.jdbc.core.namedparam.NamedParameterJdbcTemplate">
<constructor-arg ref="dataSource"></constructor-arg>
</bean>
<bean id="bankDao" class="com.java1234.dao.impl.BankDaoImpl">
<property name="namedParameterJdbcTemplate" ref="namedParameterJdbcTemplate"></property>
</bean>
<bean id="bankService" class="com.java1234.service.impl.BankServiceImpl">
<property name="bankDao" ref="bankDao"></property>
<property name="transactionTemplate" ref="transactionTemplate"></property>
</bean>
</beans>