1.导入相关包
或许有些包用不着,但为了方便,直接全部导入
2.定义一个转账方法的接口
3.创建一个类实现转账方法
这里继承了JdbcDaoSupport类,直接使用getJdbcTemplate()来获取数据库的连接
4.定义Service层接口来获得转出对象,转入对象,转入金额
5.实现service接口
package cn.lpp.service;
import javax.annotation.Resource;
import org.springframework.transaction.TransactionStatus;
import org.springframework.transaction.support.TransactionCallback;
import org.springframework.transaction.support.TransactionCallbackWithoutResult;
import org.springframework.transaction.support.TransactionTemplate;
import cn.lpp.dao.Account;
public class AccountServiceImp implements AccountService{
private Account at;
private TransactionTemplate tt;
public void setAt(Account at) {
this.at = at;
}
public void setTt(TransactionTemplate tt) {
this.tt = tt;
}
@Override
public void transfer(Integer from, Integer to, Double money) {
// TODO Auto-generated method stub
//开启事务
tt.execute(new TransactionCallbackWithoutResult() {
@Override
protected void doInTransactionWithoutResult(TransactionStatus arg0) {
//减钱
at.decreaseMoney(from, money);
//加钱
at.addMoney(to, money);
}
});
}
}
6.配置xml文件
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://www.springframework.org/schema/beans" xmlns:context="http://www.springframework.org/schema/context" xmlns:aop="http://www.springframework.org/schema/aop" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.2.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.2.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.2.xsd ">
<!-- 指定spring去读取配置文件 -->
<context:property-placeholder location="db.properties"/>
<!-- 1.将连接池放入容器 -->
<bean name="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
<property name="jdbcUrl" value="${jdbc.jdbcUrl}"></property>
<property name="driverClass" value="${jdbc.driverClass}"></property>
<property name="user" value="${jdbc.user}"></property>
<property name="password" value="${jdbc.password}"></property>
</bean>
<!-- 事务 -->
<bean name="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="dataSource"></property>
</bean>
<!-- 事务模板对象 -->
<bean name="transactionTemplate" class="org.springframework.transaction.support.TransactionTemplate">
<property name="transactionManager" ref="transactionManager"></property>
</bean>
<!-- 2.dao-->
<bean name="account" class="cn.lpp.dao.AccountImpl">
<property name="dataSource" ref="dataSource"></property>
</bean>
<!-- 3. service-->
<bean name="accountService" class="cn.lpp.service.AccountServiceImp" >
<property name="at" ref="account"></property>
<property name="tt" ref="transactionTemplate"></property>
</bean>
</beans>
7.配置db.properties
jdbc.jdbcUrl=jdbc:mysql://localhost:3306/test?useUnicode=true&characterEncoding=UTF-8&serverTimezone=UTC
jdbc.driverClass=com.mysql.cj.jdbc.Driver
jdbc.user=root
jdbc.password=1234
8.demo测试
package cn.lpp.test;
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 cn.lpp.dao.Account;
import cn.lpp.service.AccountService;
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration("classpath:applicationContext.xml")
public class demo {
@Resource(name="accountService")
private AccountService as;
@Test
public void fun1(){
as.transfer(1, 2, 100d);
}
}
9.demo前
demo后