介绍&步骤
视频教程: https://www.bilibili.com/video/BV1WZ4y1P7Bp?p=136
官方笔记链接:https://pan.baidu.com/s/1dnL5hwOPHPMNgb81yzQIOQ
提取码:2022
目录:
项目目录结构:
1. 编程式事务控制三大对象(了解即可)
- PlatformTransactionManager (平台事务管理器)
- TransactionDefinition (事务定义)
- TransactionStatu(事务状态)
1.1 PlatformTransactionManager (平台事务管理器)
PlatformTransactionManager 接口是 spring 的事务管理器,它里面提供了我们常用的操作事务的方法。
1.2 TransactionDefinition (事务定义)
TransactionDefinition 是事务的定义信息对象,里面有如下方法:
1.2.1 事务隔离级别
设置隔离级别,可以解决事务并发产生的问题,如脏读、不可重复读和虚读。
- ISOLATION_DEFAULT
- ISOLATION_READ_UNCOMMITTED
- ISOLATION_READ_COMMITTED
- ISOLATION_REPEATABLE_READ
- ISOLATION_SERIALIZABLE
1.2.2 事务传播行为
1.3 TransactionStatus (事务状态)
TransactionStatus 接口提供的是事务具体的运行状态,方法介绍如下。
2. 准备工作
什么是声明式事务控制
Spring 的声明式事务顾名思义就是采用声明的方式来处理事务。这里所说的声明,就是指在配置文件中声明
,用在 Spring 配置文件中声明式的处理事务来代替代码式的处理事务。
声明式事务处理的作用
简单理解就是代码不变,事务控制是事务控制,配置的时候就修改文件就行了不用修改代码
注意:Spring 声明式事务控制底层就是AOP
- 切点 被事务控制的方法
- 通知 事务控制配置
2.1 导入坐标
<dependencies>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>5.0.5.RELEASE</version>
</dependency>
<dependency>
<groupId>org.aspectj</groupId>
<artifactId>aspectjweaver</artifactId>
<version>1.8.4</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-jdbc</artifactId>
<version>5.0.5.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-tx</artifactId>
<version>5.0.5.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-test</artifactId>
<version>5.0.5.RELEASE</version>
</dependency>
<dependency>
<groupId>c3p0</groupId>
<artifactId>c3p0</artifactId>
<version>0.9.1.1</version>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.32</version>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
</dependency>
</dependencies>
2.2 实体类&数据库表
2.2.1构造数据库的account表
drop table if exists account;
create table account(
name varchar(20) primary key,
money DOUBLE
);
insert into account values("lucy", 5000);
insert into account values("tom", 5000);
select * from account;
2.2.2实体类
public class Account {
private String name;
private double money;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public double getMoney() {
return money;
}
public void setMoney(double money) {
this.money = money;
}
}
2.3 Dao层&Service层的接口和实现
Dao接口
public interface AccountDao {
// 转出
void out(String outMan,double money);
// 转入
void in(String inMan,double money);
}
Dao实现类
public class AccountDaoImpl implements AccountDao {
private JdbcTemplate jdbcTemplate;
// set注入
public void setJdbcTemplate(JdbcTemplate jdbcTemplate) {
this.jdbcTemplate = jdbcTemplate;
}
public void out(String outMan, double money) {
jdbcTemplate.update("update account set money=money-? where name=?",money,outMan);
}
public void in(String inMan, double money) {
jdbcTemplate.update("update account set money=money+? where name=?",money,inMan);
}
}
service 接口
public interface AccountService {
void transfer(String outMan,String inMan,double money);
}
service实现类
public class AccountServiceImpl implements AccountService {
private AccountDao accountDao;
// set注入
public void setAccountDao(AccountDao accountDao) {
this.accountDao = accountDao;
}
//转账操作
public void transfer(String outMan, String inMan, double money) {
accountDao.out(outMan,money);
// int i = 1/0;
accountDao.in(inMan,money);
}
}
2.4 applicationContext.xml文件配置
applicationContext.xml
文件配置
<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/test"/>
<property name="user" value="root"/>
<property name="password" value="123456"/>
</bean>
<bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate">
<property name="dataSource" ref="dataSource"/>
</bean>
2.5 Controller层的执行业务操作
main方法简化
public class AccountController {
public static void main(String[] args) {
ApplicationContext app = new ClassPathXmlApplicationContext("applicationContext.xml");
AccountService accountService = app.getBean(AccountService.class);
// 案例: tom给lucy转账500
accountService.transfer("tom","lucy",500);
}
}
前提准备
3. 基于XML 的声明式事务控制(快速入门)
主要是配置applicaitonContext.xml文件 找到切点 然后配置事务
切点 transfer方法 转账方法
通知 就是事务控制的功能
① 引入tx命名空间
② 配置事务增强
③ 配置事务 AOP 织入
④ 测试事务控制转账业务代码
3.1 容器中配置依赖注入
applicationContext.xml
文件配置Bean 依赖注入
<!-- 目标对象 内部的方法就是切点 -->
<bean id="accountDao" class="review.dao.impl.AccountDaoImpl">
<property name="jdbcTemplate" ref="jdbcTemplate"/>
</bean>
<bean id="accountService" class="review.service.impl.AccountServiceImpl">
<property name="accountDao" ref="accountDao"/>
</bean>
3.2 配置事务增强
引入tx命名空间
xmlns:tx="http://www.springframework.org/schema/tx"
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd
applicationContext.xml
文件配置事务增强
<!-- 平台事务管理器 -->
<bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="dataSource"></property>
</bean>
<!-- 通知 事务的增强 -->
<tx:advice id="txAdvice" transaction-manager="transactionManager">
<tx:attributes>
<!--对不同的切点进行特别的配置-->
<tx:method name="transfer" isolation="REPEATABLE_READ" propagation="REQUIRED"/>
<tx:method name="*"/>
</tx:attributes>
</tx:advice>
3.3 配置事务 AOP 织入
这里表示切点就是service.impl.
下所有的方法
xmlns:aop="http://www.springframework.org/schema/aop"
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd
applicationContext.xml
文件中配置事务 AOP 织入
<!-- 配置aop事务的织入 不配置织入不算是事务管理 -->
<aop:config>
<!-- 切点表达式的抽取 -->
<aop:pointcut id="myPointcut" expression="execution(* review.service.impl.*.*(..))"/>
<!-- 表示只有一个增强(通知) 可以直接用advisor -->
<aop:advisor advice-ref="txAdvice" pointcut-ref="myPointcut"></aop:advisor>
</aop:config>
3.4 测试事务控制转账业务代码
public class AccountController {
public static void main(String[] args) {
ApplicationContext app = new ClassPathXmlApplicationContext("applicationContext.xml");
AccountService accountService = app.getBean(AccountService.class);
// 案例: tom给lucy转账500
accountService.transfer("tom","lucy",500);
}
}
serviceDaoImpl中的int i = 1/0
不能注释
配置成功之后遇到报错 tom 和 lucy的钱都不会变化
3.5 总结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:aop="http://www.springframework.org/schema/aop"
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/aop http://www.springframework.org/schema/aop/spring-aop.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd
">
<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/test"/>
<property name="user" value="root"/>
<property name="password" value="123456"/>
</bean>
<bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate">
<property name="dataSource" ref="dataSource"/>
</bean>
<bean id="accountDao" class="review.dao.impl.AccountDaoImpl">
<property name="jdbcTemplate" ref="jdbcTemplate"/>
</bean>
<!-- 目标对象 内部的方法就是切点 -->
<bean id="accountService" class="review.service.impl.AccountServiceImpl">
<property name="accountDao" ref="accountDao"/>
</bean>
<!-- 平台事务管理器 -->
<bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="dataSource"></property>
</bean>
<!-- 通知 事务的增强 -->
<tx:advice id="txAdvice" transaction-manager="transactionManager">
<tx:attributes>
<!--对不同的切点进行特别的配置-->
<tx:method name="transfer" isolation="REPEATABLE_READ" propagation="REQUIRED"/>
<tx:method name="*"/>
</tx:attributes>
</tx:advice>
<!-- 配置aop事务的织入 不配置织入不算是事务管理 -->
<aop:config>
<!-- 切点表达式的抽取 -->
<aop:pointcut id="myPointcut" expression="execution(* review.service.impl.*.*(..))"/>
<!-- 表示只有一个增强(通知) 可以直接用advisor -->
<aop:advisor advice-ref="txAdvice" pointcut-ref="myPointcut"></aop:advisor>
</aop:config>
</beans>
4. 基于注解的声明式事务控制
- 编写 AccountDaoImpl
- 编写 AccountServiceImpl
- 编写 applicationContext.xml 配置文件
4.1 编写 AccountDaoImpl
代替了之前的accountDao的配置
@Repository("accountDao") // id 为accountDao 的bean 放在spring容器中
public class AccountDaoImpl implements AccountDao {
@Autowired
private JdbcTemplate jdbcTemplate;
public void out(String outMan, double money) {
jdbcTemplate.update("update account set money=money-? where name=?",money,outMan);
}
public void in(String inMan, double money) {
jdbcTemplate.update("update account set money=money+? where name=?",money,inMan);
}
}
4.2 编写 AccountServiceImpl
@Transactional注解也可以放在类上
@Service("accountService") // id为accountService的bean 放在spring容器中
public class AccountServiceImpl implements AccountService {
@Autowired
private AccountDao accountDao;
@Transactional(isolation = Isolation.DEFAULT) // 相当于织入 切点为transfer 也可以直接放在类上
public void transfer(String outMan, String inMan, double money) {
accountDao.out(outMan,money);
// int i = 1/0;
accountDao.in(inMan,money);
}
}
4.3 编写 applicationContext.xml 配置文件
<!--之前省略datsSource、jdbcTemplate、平台事务管理器的配置-->
<!-- 进行组件扫描 配置注解 -->
<context:component-scan base-package="review"/>
<!-- 平台事务管理器 -->
<bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="dataSource"></property>
</bean>
<!-- 事务的注解驱动 -->
<tx:annotation-driven transaction-manager="transactionManager"/>
applicaitonContext.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:aop="http://www.springframework.org/schema/aop"
xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd
">
<!-- 进行组件扫描 配置注解 -->
<context:component-scan base-package="review"/>
<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/test"/>
<property name="user" value="root"/>
<property name="password" value="123456"/>
</bean>
<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"></property>
</bean>
<!-- 事务的注解驱动 -->
<tx:annotation-driven transaction-manager="transactionManager"/>
</beans>
4.4 总结