- 搭建转账业务环境
- 创建数据库表account
- 创建AccountDao接口
public interface AccountDao {
//转出
public void outMoney(String to,Double money);
//转入
public void inMoney(String from,Double money);
}
- 实现AccountDao接口得到AccountDaoImpl
public class AccountDaoImpl extends JdbcDaoSupport implements AccountDao {
/* //属性注入的方式获取jdbcTemplate
//也可以通过继承 JdbcDaoSupport获取jdbcTemplate
private JdbcTemplate jdbcTemplate;
public void setJdbcTemplate(JdbcTemplate jdbcTemplate) {
this.jdbcTemplate = jdbcTemplate;
}*/
//转出
@Override
public void outMoney(String name, Double money) {
this.getJdbcTemplate().update("update account set money = money-? where name=?", money, name);
}
//转入
@Override
public void inMoney(String name, Double money) {
this.getJdbcTemplate().update("update account set money = money+? where name=?", money, name);
}
}
- 将创建转账业务接口
public interface AccountService {
public void transferMoney(String from,String to,Double money);
}
- 实现转账业务接口得到AccountServiceImpl
public class AccountServiceImpl implements AccountService {
private AccountDao accountDao;
public void setAccountDao(AccountDao accountDao) {
this.accountDao = accountDao;
}
@Override
public void transferMoney(String from, String to, Double money) {
accountDao.outMoney(from, money);
accountDao.inMoney(to,money);
}
}
- 编写属性文件property.property
driverClass=com.mysql.jdbc.Driver
jdbcUrl=jdbc:mysql:///Spring
user=root
password=123456
- 将AccountDaoImpl与AccountServiceImpl交给Spring管理
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:p="http://www.springframework.org/schema/p"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
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
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop.xsd">
<!--开启IOC注解-->
<context:annotation-config/>
<!--开启AOP注解-->
<aop:aspectj-autoproxy/>
<!--配置文件配置Bean-->
<!--加载属性文件-->
<context:property-placeholder location="property"/>
<bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource">
<!--注入属性,引用属性名称的key值-->
<property name="driverClassName" value="${driverClass}"/>
<property name="Url" value="${jdbcUrl}"/>
<property name="username" value="${user}"/>
<property name="password" value="${password}"/>
</bean>
<!--JdbcTemplate注入连接池-->
<bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate">
<property name="dataSource" ref="dataSource"/>
</bean>
<!--配置accountDaoImpl,并注入数据源-->
<bean id="accountDao" class="com.ph.demo3.AccountDaoImpl">
<property name="dataSource" ref="dataSource"/>
</bean>
<!--配置Service,并注入accountDao-->
<bean id="accountService" class="com.ph.demo3.AccountServiceImpl">
<property name="accountDao" ref="accountDao"/>
</bean>
</beans>
- 编写测试类
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration("classpath:applicationContext.xml")
public class test {
@Resource
private AccountService accountService;
@Test
public void testTransfer() {
accountService.transferMoney("张三", "李四", 100.00);
System.out.println("转账成功");
}
}
运行结果
此时我们可以看到,转账成功后,张三的账户余额少了100,而李四的账户余额多了100,这是没有问题的。但是如果在转账的过程出现了错误,导致了一方转出,而另一方没有收到转入,那么就是一个问题了。我们可以模拟一下:我们将转账业务注入一个异常
public class AccountServiceImpl implements AccountService {
private AccountDao accountDao;
public void setAccountDao(AccountDao accountDao) {
this.accountDao = accountDao;
}
@Override
public void transferMoney(String from, String to, Double money) {
accountDao.outMoney(from, money);
int i=10/0;//注入异常
accountDao.inMoney(to,money);
}
}
再次运行测试类
我们可以看到,张三扣减了100,但是李四并没有增加100。这是非常致命的缺陷,因此我们需要做一个事务管理来解决这个问题,事务保证要么不做,要么全做,不会出现一方扣