使用spring的聲明式事務(wù)管理來(lái)搭建一個(gè)轉(zhuǎn)賬項(xiàng)目。
事務(wù)的介紹:事務(wù)是一系列動(dòng)作的組合操作,這個(gè)組合里面的所有動(dòng)作要么都完成要么都不完成。
事務(wù)的特性(ASID):
1.原子性:事務(wù)中的所有操作要么都完成要么都不完成(如果有一個(gè)動(dòng)作失敗會(huì)進(jìn)行回滾操作)。
2.一致性:事務(wù)開(kāi)始和結(jié)束之間的中間狀態(tài)不會(huì)被其他事務(wù)看到。
3.隔離性:多個(gè)事務(wù)同時(shí)運(yùn)行時(shí)不會(huì)相互影響。
4.持久性:一旦事務(wù)結(jié)束(無(wú)論成功與否),不管系統(tǒng)遇到什么錯(cuò)誤都不會(huì)改變結(jié)果。
1、導(dǎo)入相關(guān)jar包
image.png
2、編寫(xiě)spring的配置文件
<?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:aop="http://www.springframework.org/schema/aop"
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
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd">
<!-- 配置開(kāi)啟注解 -->
<context:component-scan base-package="com.lj"/>
<!-- 配置開(kāi)啟aop -->
<aop:aspectj-autoproxy ></aop:aspectj-autoproxy>
<!-- 配置數(shù)據(jù)庫(kù)連接池 -->
<bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
<property name="driverClass" value="com.mysql.jdbc.Driver"/>
<property name="jdbcUrl" value="jdbc:mysql://localhost:3306/spring?characterEncoding=utf-8"/>
<property name="user" value="root"/>
<property name="password" value="1234"/>
</bean>
<!-- 配置jdbcTemplate -->
<bean id="jdbcTemplate" 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>
<!-- 開(kāi)啟事務(wù)注解 -->
<tx:annotation-driven transaction-manager="transactionManager"/>
</beans>
3、編寫(xiě)實(shí)現(xiàn)類(lèi)
dao層:
package com.lj.tx;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Component;
@Component
public class AccountDao {
@Autowired
private JdbcTemplate jdbcTemplate;
public void moreMoney(int money,String name){
String sql = "update account set money=money+? where name=?";
jdbcTemplate.update(sql,money,name);
}
public void lessMoney(int money,String name){
String sql = "update account set money=money-? where name=?";
jdbcTemplate.update(sql,money,name);
}
}
service層
package com.lj.tx;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
@Transactional
@Component
public class AccountService {
@Autowired
private AccountDao accountDao;
public void transfer(String man1,String man2,int money){
accountDao.lessMoney(money, man1);
accountDao.moreMoney(money, man2);
}
}
可以使用spring的aop編寫(xiě)一個(gè)轉(zhuǎn)賬日志記錄類(lèi)
package com.lj.tx;
import java.util.Date;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.After;
import org.aspectj.lang.annotation.Aspect;
import org.springframework.stereotype.Component;
@Aspect
@Component
public class LogAccountService {
@After(value="execution(* com.lj.tx.AccountService.*(..))")
public void log(JoinPoint jp){
System.out.println("轉(zhuǎn)賬記錄"+new Date());
Object args[] = jp.getArgs();
System.out.println("轉(zhuǎn)賬人:"+args[0]+" 收賬人:"+args[1]+" 金額:"+args[2]);
}
}
測(cè)試類(lèi):
package com.lj.tx;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class Test {
@org.junit.Test
public void txTest(){
ApplicationContext context = new ClassPathXmlApplicationContext("springTX.xml");
AccountService service = (AccountService) context.getBean("accountService");
service.transfer("張三", "李四", 100);
}
}