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 org.springframework.transaction.annotation.Isolation;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
import cn.zj.spring.dao.UserDao;
import cn.zj.spring.service.UserService;
@Service
@Transactional(readOnly = false,isolation = Isolation.REPEATABLE_READ,propagation = Propagation.REQUIRED,timeout = 5)
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);
}
@Transactional(readOnly = true)//readOnly默认为false
public Object selectByPrimaryKey(Integer id) {
return null;
}
}
第五步:编写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>
<!-- JdbcTemplate -->
<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>
<!-- 设置事务的注解配置 -->
<tx:annotation-driven transaction-manager="transactionManager"/>
</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);
}
}