AccountDaoImpl.java
public class AccountDaoImpl implements AccountDao{
public JdbcTemplate temp;
public void setTemp(JdbcTemplate temp) {
this.temp = temp;
}
@Override//转入账号
public void increaseMoney(Integer id, Integer money) {
String sql = "update test set money = money + ? where id = ?";
temp.update(sql,money,id);
}
@Override//转出账号
public void decreaseMoney(Integer id, Integer money) {
String sql = "update test set money = money - ? where id = ?";
temp.update(sql,money,id);
}
}
AccountServiceImpl.java
public class AccountServiceImpl implements AccountService {
public AccountDao dao;
//事务管理类
public TransactionTemplate trans;
public void setDao(AccountDao dao) {
this.dao = dao;
}
public void setTrans(TransactionTemplate trans) {
this.trans = trans;
}
@Override
public void transfer(Integer from, Integer to, Integer money) {
//调用事务管理对象的execute
trans.execute(new TransactionCallbackWithoutResult() {
@Override
protected void doInTransactionWithoutResult(TransactionStatus transactionStatus) {
dao.decreaseMoney(from,money);
dao.increaseMoney(to,money);
}
});
}
}
事务管理.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: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/context http://www.springframework.org/schema/context/spring-context.xsd">
<!--引入properties文件-->
<context:property-placeholder location="classpath:jdbc.properties"/>
<!--注册数据源-->
<bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
<property name="driverClass" value="${jdbc.Driver}"/>
<property name="jdbcUrl" value="${jdbc.url}"/>
<property name="user" value="${jdbc.username}"/>
<property name="password" value="${jdbc.password}"/>
</bean>
<!--注册JDBCTemplate-->
<bean id="temp" class="org.springframework.jdbc.core.JdbcTemplate">
<property name="dataSource" ref="dataSource"/>
</bean>
<!--注册事务管理器-->
<bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="dataSource"/>
</bean>
<bean id="transactionTemplate" class="org.springframework.transaction.support.TransactionTemplate">
<property name="transactionManager" ref="transactionManager"/>
</bean>
<!--注册Dao-->
<bean id="accountDao" class="banana.事务管理.dao.AccountDaoImpl">
<property name="temp" ref="temp"/>
</bean>
<!--注册service-->
<bean id="service" class="banana.事务管理.service.AccountServiceImpl">
<property name="dao" ref="accountDao"/>
<property name="trans" ref="transactionTemplate"/>
</bean>
</beans>
test.java 测试类
public class test {
public static void main(String[] args) {
ApplicationContext applicationContext = new ClassPathXmlApplicationContext("事务管理.xml");
AccountService service = applicationContext.getBean("service", AccountServiceImpl.class);
service.transfer(1,3,1000);
System.out.println("转账完成");
}
}