xml 方式声明式事务案例
1. 创建数据库表
表名 tbl_account
mysql
create table tbl_account(
id int AUTO_INCREMENT PRIMARY KEY,
name varchar(10) NOT NULL,
money double NOT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8;

框架结构

2. jdbc.properties
文件,配置 JDBC
连接
java
jdbc.driver=com.mysql.cj.jdbc.Driver
jdbc.url=jdbc:mysql://localhost:3306/table_account
jdbc.username=root
jdbc.password=123456
3. xml文件配置
java
<?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: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/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
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx.xsd
">
<!-- 组件扫描-->
<context:component-scan base-package="com.itheima"></context:component-scan>
<!-- 加载properties文件-->
<context:property-placeholder location="classpath:jdbc.properties"></context:property-placeholder>
<!-- 配置数据源信息-->
<bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource">
<property name="driverClassName" value="${jdbc.driver}"></property>
<property name="url" value="${jdbc.url}"></property>
<property name="username" value="${jdbc.username}"></property>
<property name="password" value="${jdbc.password}"></property>
</bean>
<!--配置SqlSessionFactoryBean,作用将SqlSessionFactory存储到Spring容器-->
<bean class="org.mybatis.spring.SqlSessionFactoryBean">
<property name="dataSource" ref="dataSource"></property>
</bean>
<!-- MapperScannerConfigurer,作用扫描指定包,产生Mapper对象存储到Spring容器-->
<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<property name="basePackage" value="com.itheima.mapper"></property>
</bean>
<!--配置平台事务管理器-->
<bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="dataSource"></property>
</bean>
<!--配置Spring提供好的通知advice-->
<tx:advice id="txAdvice" transaction-manager="transactionManager">
<tx:attributes>
<!--
配置不同的方法的事务属性
name: 方法名称 * 代表通配符 添加操作addUser、addAccount、addOrder 可以采用 add*
isolation: 事务的隔离级别,解决事务并发问题
timeout: 超时时间 默认-1 单位 秒
read-only: 是否只读, 查询操作设置为只读
propagation: 事务的传播行为,解决事务嵌套问题
-->
<tx:method name="*"/>
</tx:attributes>
</tx:advice>
<!--事务增强的AOP-->
<aop:config>
<!-- 配置切入点表达式-->
<aop:pointcut id="txPointcut" expression="execution(* com.itheima.service.impl.*.*(..))"/>
<!-- 配置织入关系 通知advice-ref 引入Spring提供好的-->
<aop:advisor advice-ref="txAdvice" pointcut-ref="txPointcut"/>
</aop:config>
</beans>
4. AccountMapper
接口
java
public interface AccountMapper {
@Update("update tbl_account set money = money - #{money} where name = #{name}")
public void incrMoney(@Param("name") String name, @Param("money") Double money);
@Update("update tbl_account set money = money + #{money} where name = #{name}")
public void decrMoney(@Param("name") String name, @Param("money") Double money);
}
5. AccountService
接口
java
public interface AccountService {
void transfer(String outName, String inName, Double money);
}
6. AccountService
的实现类 AccountServiceImpl
java
@Service("accountService")
public class AccountServiceImpl implements AccountService {
@Autowired
private AccountMapper accountMapper;
@Override
public void transfer(String outName, String inName, Double money) {
accountMapper.decrMoney(outName, money);
accountMapper.incrMoney(inName, money);
}
}
7. 测试操作数据库 转账操作
java
public class AccountTest {
public static void main(String[] args) {
ApplicationContext applicationContext = new ClassPathXmlApplicationContext("beans.xml");
AccountService accountService = applicationContext.getBean(AccountService.class);
accountService.transfer("Tom", "Jerry", 500.0);
}
}
正常情况下,Tom
给 Jerry
转账成功

若给业务层加异常,在有事务的情况下,事务会回滚,否则不回滚(存在 Tom
只减钱,Jerry
不加钱);
java
@Service("accountService")
public class AccountServiceImpl implements AccountService {
@Autowired
private AccountMapper accountMapper;
@Override
public void transfer(String outName, String inName, Double money) {
accountMapper.decrMoney(outName, money);
int i = 1 / 0;
accountMapper.incrMoney(inName, money);
}
}