1、在Spring配置文件配置事物管理器
<!--创建事物管理器-->
<bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="dataSource"/>
</bean>
2、在spring配置文件,开启事务注解
(1)在spring配置文件引入命名空间tx
<?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">
(2)开启事务
<!--开启事务-->
<tx:annotation-driven transaction-manager="transactionManager"></tx:annotation-driven>
3、对类添加@Transcation注解
(1)如果该注解添加在类上,会对该类中所有方法都开启事务
(2)如果注解添加在方法上,对具体的方法开启事务
package com.daijunyi.service;
import com.daijunyi.dao.AccountDao;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
@Service
public class AccountService {
@Autowired
private AccountDao accountDao;
@Transactional
public void changeMoney(Integer fromUserId,Integer toUserId,Double changeMoney){
//第一步 开启事物
//第二步 进行业务操作
accountDao.changeMoneyById(fromUserId,-changeMoney);
int i=1/0;
accountDao.changeMoneyById(toUserId,changeMoney);
}
}
4、测试会发现即使代码报错 数据库中数据没有发生变化。
最终的配置文件
<?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:component-scan base-package="com.daijunyi"/>
<!--引入外部属性文件-->
<context:property-placeholder location="classpath:db.properties"/>
<!--配置datasource 数据库连接池-->
<bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource">
<property name="driverClassName" value="${druid.driver}"/>
<property name="url" value="${druid.url}"/>
<property name="username" value="${druid.username}"/>
<property name="password" value="${druid.password}"/>
</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>
<!--开启事务-->
<tx:annotation-driven transaction-manager="transactionManager"></tx:annotation-driven>
</beans>