- 本次采用的是使用注解这种方式,我感觉使用这种方式是最简单的。本次代码参考自慕课网
- 本事例是模仿银行转钱,中间发生异常,使用事务进行管理
- 定义接口
public interface AccountService {
public void transfer(String out,String in ,int money);
}
4.定义接口实现类
import org.springframework.transaction.annotation.Transactional;
//使用注解
@ransactional
public class AccontServiceImpl implements AccountService {
private AccountDao accountDao;
public void setAccountDao(AccountDao accountDao) {
this.accountDao = accountDao;
}
public void transfer(String out, String in, int money) {
// TODO Auto-generated method stub
accountDao.outMoney(out, money);
int i=1/0; //发生异常
accountDao.inMoney(in, money);
}
}
5.定义Dao接口
public interface AccountDao {
public void outMoney(String out,int money);
public void inMoney(String in,int money);
}
6.定义Dao接口实现类
import org.springframework.jdbc.core.support.JdbcDaoSupport;
public class AccountDaoImpl extends JdbcDaoSupport implements AccountDao {
@Override
public void outMoney(String out, int money) {
// TODO Auto-generated method stub
String sql="update account set money=money-? where name=?";
this.getJdbcTemplate().update(sql,money,out);
}
@Override
public void inMoney(String in, int money) {
// TODO Auto-generated method stub
String sql="update account set money=money+? where name=?";
this.getJdbcTemplate().update(sql,money,in);
}
}
7.测试类
import javax.annotation.Resource;
import org.junit.runner.RunWith;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration("classpath:applicationContext2.xml")
public class Test {
@Resource(name="accountService")
private AccountService accountservice;
@org.junit.Test
public void test() {
accountservice.transfer("aaa", "bbb", 100);
}
}
8.配制文件
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop.xsd">
<context:property-placeholder location="classpath:jdbc.properties"/>
<bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
<property name="driverClass" value="${jdbc.driverClassName}"></property>
<property name="jdbcUrl" value="${jdbc.url}"></property>
<property name="user" value="${jdbc.username}"></property>
<property name="password" value="${jdbc.password}"></property>
</bean>
<!-- 配置业务 -->
<bean id="accountService" class="com.transaction2.AccontServiceImpl">
<property name="accountDao" ref="accountDao"></property>
</bean>
<!-- 配置dao -->
<bean id="accountDao" class="com.transaction2.AccountDaoImpl">
<property name="dataSource" ref="dataSource"> </property>
</bean>
<!-- 配置事务管理类 -->
<bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="dataSource"></property>
</bean>
<!--配制注解 -->
<tx:annotation-driven transaction-manager="transactionManager"></tx:annotation-driven>
</beans>
9.项目结构