前言:
- 本文讲解Spring的事务管理(注解方式)实现
- 本文是通过一步步创建项目的方式讲解
- 本文主要是讲具体的操作,对于事务管理的原理并不会深入讲解
环境:
- Intellij IDEA 2017 CI
具体步骤
1、创建Spring项目
如果不清楚怎么创建项目,请移步Spring入门之事务管理【一】XML配置文件方式
OrdersDao类:
public class OrdersDao {
// 注入jdbcTemplate
private JdbcTemplate jdbcTemplate;
public void setJdbcTemplate(JdbcTemplate jdbcTemplate) {
this.jdbcTemplate = jdbcTemplate;
}
/*
做对数据库的操作,不写业务操作
*/
//减少金额
public void lessMoney(){
String sql = "update account set salary=salary-? where name=?";
jdbcTemplate.update(sql,100,"Tom");
}
//增加金额
public void moreMoney(){
String sql = "update account set salary=salary+? where name=?";
jdbcTemplate.update(sql,100,"John");
}
}
OrdersService类:
public class OrdersService {
private OrdersDao ordersDao;
public void setOrdersDao(OrdersDao ordersDao) {
this.ordersDao = ordersDao;
}
//调用dao方法
/*
业务逻辑层
*/
//转账业务
public void accountMonry(){
ordersDao.lessMoney(); //减少金额
int i = 10/0; //模拟出现异常
ordersDao.moreMoney(); //增加金额
}
}
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: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/cache http://www.springframework.org/schema/cache/spring-cache.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">
<!--配置C3P0连接池-->
<bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
<!--注入属性值-->
<property name="driverClass" value="com.mysql.jdbc.Driver"></property>
<property name="jdbcUrl" value="jdbc:mysql:///test"></property>
<property name="user" value="root"></property>
<property name="password" value="root"></property>
</bean>
<!--service注入dao-->
<bean class="spring.service.OrdersService" id="ordersService">
<property name="ordersDao" ref="ordersDao"></property>
</bean>
<!--dao注入jdbcTemplate-->
<bean class="spring.dao.OrdersDao" id="ordersDao">
<property name="jdbcTemplate" ref="jdbcTemplate"></property>
</bean>
<!--jdbcTemplate注入数据源-->
<bean class="org.springframework.jdbc.core.JdbcTemplate" id="jdbcTemplate">
<property name="dataSource" ref="dataSource"></property>
</bean>
</beans>
如果不清楚上面类中方法的内容的,或者对Spring中的Bean管理,注入属性不是很明白的,可以看下博主的这几篇文章
- Spring的Bean管理(XML配置文件方式)
- Spring属性注入【一】 注入方式介绍
- Spring属性注入【二】 注入对象类型属性
- Spring属性注入【三】 复杂类型注入
- Spring入门之C3P0连接池
2、配置事务管理器
<!--第一步 配置事务管理器-->
<bean id="dataSourceTransactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<!--注入数据源,指定对哪个数据库进行操作-->
<property name="dataSource" ref="dataSource"></property>
</bean>
3、开启事务注解
<!--第二步 开启事务注解-->
<tx:annotation-driven transaction-manager="dataSourceTransactionManager"></tx:annotation-driven>
4、在事务所在类上加注解
@Transactional
public class OrdersService {
5、测试
TestService类:
package spring.test;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import spring.service.OrdersService;
public class TestService {
@Test
public void testService(){
ApplicationContext applicationContext = new ClassPathXmlApplicationContext("applicationContext.xml");
OrdersService ordersService = (OrdersService) applicationContext.getBean("ordersService");
ordersService.accountMonry();
}
}
测试前数据库表:
测试结果:
测试失败:因为我们在Service类中进行了手动模拟异常
测试后数据库:
数据库中表的数据并没有变化,说明在异常发生之后,进行了事务回滚,事务管理成功。