事务的ACID原则
- 原子性
- 隔离性
- 一致性
- 持久性
转账案例实现
dao层接口编写转账方法
public interface AccountDao {
/**
* 转入金额
* in : 收款人姓名-----给谁转
* money : 转入金额
*/
void inMoney(String in , double money);
/**
* 转出金额
* out : 付款人姓名-----谁来转
* money : 转出金额
*/
void outMoney(String out , double money);
}
dao层实现类实现接口方法
import org.springframework.jdbc.core.support.JdbcDaoSupport;
public class AccountDaoImpl extends JdbcDaoSupport implements AccountDao{
/**
* 转入
* @param in
* @param money
*/
@Override
public void inMoney(String in, double money) {
String sql = "update account set money = money + ? where name = ?";
this.getJdbcTemplate().update(sql,money,in);
}
/**
* 转出
* @param out
* @param money
*/
@Override
public void outMoney(String out, double money) {
String sql = "update account set money = money - ? where name = ?";
this.getJdbcTemplate().update(sql,money,out);
}
}
Service接口编写转账方法
public interface AccountService {
/**
* 转账
* 转账人 in
* 收款人 out
* 转账金额 money
*/
void transfer(String in,String out,double money);
}
@Transactional
public class AccountServiceImpl implements AccountService{
private AccountDao accountDao;
public AccountServiceImpl(){
}
public AccountDao getAccountDao() {
return accountDao;
}
public void setAccountDao(AccountDao accountDao) {
this.accountDao = accountDao;
}
public AccountServiceImpl( AccountDao accountDao ) {
this.accountDao = accountDao;
}
/**\
*
* @param in
* @param out
* @param money
*/
@Override
public void transfer(String in, String out, double money) {
// 转出
accountDao.outMoney(out, money);
// 转入
accountDao.inMoney(in, money);
}
}
配置文件applicationContext.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" xmlns:tx="http://www.springframework.org/schema/tx"
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/tx http://www.springframework.org/schema/tx/spring-tx.xsd">
<!-- 外部资源的导入 -->
<context:property-placeholder location="classpath:db.properties"></context:property-placeholder>
<!-- 配置数据库 -->
<bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource">
<property name="url" value="${jdbc_url}"></property>
<property name="username" value="${jdbc_user}"></property>
<property name="password" value="${jdbc_password}"></property>
</bean>
<!-- 注册Dao -->
<bean id="accountDao" class="com.sf.dao.AccountDaoImpl">
<property name="dataSource" ref="dataSource"></property>
</bean>
<!-- 注册Service -->
<bean id="accountService" class="com.sf.service.AccountServiceImpl">
<property name="accountDao" ref="accountDao"></property>
</bean>
<!-- 配置事务管理器 -->
<bean id="dataSourceTransactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="dataSource"></property>
</bean>
<!-- 开启基于注解的事务管理 -->
<tx:annotation-driven transaction-manager="dataSourceTransactionManager"></tx:annotation-driven>
</beans>
进行测试Test
import com.sf.service.AccountService;
import com.sf.service.AccountServiceImpl;
import org.junit.jupiter.api.Test;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class TxTest{
@Test
public void t1(){
ClassPathXmlApplicationContext ioc =
new ClassPathXmlApplicationContext("applicationContext.xml");
AccountService accountService =
(AccountService) ioc.getBean("accountService");
accountService.transfer("z3","li4",1100.00);
}
}