Spring支持编程式事务管理和声明式事务管理。
-
编程式事务管理:事务和业务代码耦合度太高。
-
声明式事务管理:侵入性小,把事务从业务代码中抽离出来,使用AOP配置到配置文件中,提高维护性。
第一步:项目结构图
导入相关依赖包,并且在applicationContext.xml配置文件引入tx的命名空间第二步:编写pojo
package cn.zj.spring.pojo;
public class User {
private Integer id;
private String name;
private String email;
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
@Override
public String toString() {
return "User [id=" + id + ", name=" + name + ", email=" + email + "]";
}
public User(Integer id, String name, String email) {
super();
this.id = id;
this.name = name;
this.email = email;
}
public User() {
super();
// TODO Auto-generated constructor stub
}
}
第三步:编写持久层(dao)的代码
package cn.zj.spring.dao.impl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Repository;
import cn.zj.spring.dao.UserDao;
@Repository
public class UserDaoImpl implements UserDao {
@Autowired
private JdbcTemplate jdbcTemplate;
@Override
public void transOut(Integer outId, Integer money) {
this.jdbcTemplate.update("update t_account set balance = balance - ? where id = ?",money,outId);
}
@Override
public void transIn(Integer inId, Integer money) {
this.jdbcTemplate.update("update t_account set balance = balance + ? where id = ?",money,inId);
}
}
第四步:编写业务层接口和实现类
package cn.zj.spring.service.impl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import cn.zj.spring.dao.UserDao;
import cn.zj.spring.service.UserService;
@Service
public class UserServiceImpl implements UserService {
@Autowired
private UserDao userDao;
@Override
public void trans(Integer outId, Integer inId, Integer money) {
//扣钱
userDao.transOut(outId, money);
System.out.println(1/0);
//打钱
userDao.transIn(inId, money);
}
}
第五步:编写db.properties配置文件
#批量修改 alt+shift +a 修改完毕以后 ,alt+shirf+a 还原
jdbc.driverClassName=com.mysql.jdbc.Driver
jdbc.url=jdbc:mysql://localhost:3306/gj1?characterEncoding=utf-8
jdbc.username=root
jdbc.password=123456
jdbc.maxActive=10
第六步:编写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"
xmlns:aop="http://www.springframework.org/schema/aop"
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-4.3.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.3.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.3.xsd">
<context:component-scan base-package="cn.zj.spring"/>
<context:property-placeholder location="classpath:db.properties"/>
<!-- 创建连接池(数据源) -->
<bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource"
init-method="init" destroy-method="close">
<!-- set/属性注入 -->
<!--
使用SpringEL表达式可以读取配置文件的内容
#{} : #{3600 * 24 * 7}
${} : ${xx.properties配置文件中的key}
druid 连接池的 配置的的key 不能是username
因为默认读取的时候当前操作系统登录的账号,不是 配置文件中username的值
-->
<property name="driverClassName" value="${jdbc.driverClassName}"/>
<property name="url" value="${jdbc.url}"/>
<property name="username" value="${jdbc.username}"/>
<property name="password" value="${jdbc.password}"/>
<property name="maxActive" value="${jdbc.maxActive}"/>
</bean>
<bean scope="prototype" id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate">
<constructor-arg name="dataSource" ref="dataSource"/>
</bean>
<!-- ========================================== -->
<!-- 事务配置
1,配置事务管理器 (what?)
2,配置事务通知/增强 (when ?)
3,使用AOP将事务切入Service层 (where?)
-->
<!-- 1,配置事务管理器 (what?) -->
<bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<!-- 注入数据源 -->
<property name="dataSource" ref="dataSource"/>
</bean>
<!-- 2,配置事务通知/增强 (when ?)
<tx:advice id="txAdvice" transaction-manager="transactionManager">
id : 唯一标示
transaction-manager :事务管理器
-->
<tx:advice id="txAdvice" transaction-manager="transactionManager">
<!-- 配置事务属性 -->
<tx:attributes>
<!-- 配置事务要具体处理的方法
<tx:method name="" isolation="DEFAULT" propagation="REQUIRED" read-only="false" timeout="-1" />
name : 要处理事务的方法名 ,(当前案例的场景方法就是 Service层 trans)
针对DML,DQL 不同的多个方法,可以使用通配符 : *
isolation : 配置事务的隔离级别
propagation :传播规则
read-only : 配置是否是只读事务,默认是false
true :是只读事务,DQL ,性能高一些
false :非只读事务,一定要处理事务 , DML
timeout : 事务等待超时管理 5
-->
<!-- <tx:method name="trans" isolation="REPEATABLE_READ" propagation="REQUIRED" read-only="false" timeout="5" /> -->
<!-- DQL :查询 -->
<tx:method name="select*" read-only="true" isolation="REPEATABLE_READ" propagation="REQUIRED" timeout="5" />
<tx:method name="get*" read-only="true" isolation="REPEATABLE_READ" propagation="REQUIRED" timeout="5" />
<tx:method name="query*" read-only="true" isolation="REPEATABLE_READ" propagation="REQUIRED" timeout="5" />
<tx:method name="find*" read-only="true" isolation="REPEATABLE_READ" propagation="REQUIRED" timeout="5" />
<!-- DML : 增删改 -->
<tx:method name="*" read-only="false" isolation="REPEATABLE_READ" propagation="REQUIRED" timeout="5" />
</tx:attributes>
</tx:advice>
<!-- 3,使用AOP将事务切入Service层 (where?) -->
<aop:config>
<!-- 配置切入点 -->
<aop:pointcut expression="execution(* cn.zj.spring.service..*.*(..))" id="pt"/>
<!-- 切面 = 切入点 +通知
<aop:aspect></aop:aspect> : 配置开发自定义的切面
<aop:advisor advice-ref=""/> : 配置事务管理的切面,默认使用环绕通知
advice-ref : 事务通知的引用
pointcut-ref :切入点引用
-->
<aop:advisor advice-ref="txAdvice" pointcut-ref="pt" />
<!-- <aop:advisor advice-ref="txAdvice" pointcut="execution(* cn.zj.spring.service..*.*(..))"/> -->
</aop:config>
</beans>
第七步:测试代码
package cn.zj.spring.test;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import cn.zj.spring.service.UserService;
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration("classpath:applicationContext.xml")
public class UserServiceTest {
@Autowired
UserService userService;
@Test
public void testInsert() {
userService.trans(10086, 10010, 1000);
}
}