一、Spring管理事务的对象类型
- org.springframework.jdbc.datasource.DataSourceTransactionManager 使用Spring JDBC或iBatis进行持久化数据时使用
- org.springframework.orm.hibernate5.HibernateTranactionManager 使用Hibernate版本进行持久化数据时使用
二、Spring中基于XML的声明式事务控制配置步骤
源码git地址:https://github.com/Viviennechenwei/Learning_Spring/tree/master/ch03-spring-transaction-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:context="http://www.springframework.org/schema/context"
xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="http://www.springframework.org/schema/beans
https://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/tx
https://www.springframework.org/schema/tx/spring-tx.xsd
http://www.springframework.org/schema/context
https://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/aop
https://www.springframework.org/schema/aop/spring-aop.xsd">
<!--声明配置文件-->
<context:property-placeholder location="classpath:application.yml"/>
<bean id="accountService" class="com.springlearning.service.impl.AccountServiceImpl">
<property name="accountDao" ref="accountDao"></property>
</bean>
<bean id="accountDao" class="com.springlearning.dao.impl.AccountDaoImpl">
<property name="jdbcTemplate" ref="jdbcTemplate"></property>
</bean>
<bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate">
<property name="dataSource" ref="dataSource"></property>
</bean>
<!--配置文件的使用-->
<bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="driverClassName" value="${driverClassName}"></property>
<property name="url" value="${url}"></property>
<property name="username" value="${spring.datasource.username}"></property>
<property name="password" value="${spring.datasource.password}"></property>
</bean>
<!--Spring中基于XML的声明式事务控制配置步骤
1. 配置事务管理器
2. 配置事务的通知
此时需要导入事务的约束 tx名称空间和约束,同时也需要aop的
使用tx:advice标签配置事务通知
属性:
id: 给事务通知起一个唯一标识
transaction-manager: 给事务通知提供一个事务管理器的引用
3. 配置AOP的通用切入点表达式
4. 建立事务通知和切入点表达式的对应关系
5. 配置事务的属性
在事务通知tx:advice标签的内部
-->
<!--配置事务管理器-->
<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>
<!--配置事务的属性
isolation:用于指定事务的隔离级别。默认值是DEFAULT,表示使用数据库的默认隔离级别
propagation:用于指定事务的传播行为。默认值是REQUITED,表示一定会有事务,增删改的选择。查询方法可以选择SUPPORTS
read-only: 用于指定事务是否只读。只有查询方法才能设置为true。默认职位false, 表示读写
timeout:用于指定事务的超时时间,默认值是-1,表示永不超时。如果指定了数值,以秒为单位。
rollback-for: 用于指定一个异常,当产生该异常时,事务回滚,产生其他异常时事务不会滚。没有默认值。表示任何异常都回滚
no-rollback-for:用于指定一个异常,当产生该异常时,事务步回滚,产生其他异常时事务会滚。没有默认值。表示任何异常都回滚
-->
<tx:method name="*" propagation="REQUIRED" read-only="false"/> <!--除find开头以外的所有方法-->
<tx:method name="find*" propagation="SUPPORTS" read-only="true"/><!--所有以find开头的方法-->
</tx:attributes>
</tx:advice>
<!--配置AOP-->
<aop:config>
<!--配置切入点表达式-->
<aop:pointcut id="pt1" expression="execution(* *.*.*.*.AccountServiceImpl.*(..))"></aop:pointcut>
<!--建立事务通知和切入点表达式的对应关系-->
<aop:advisor advice-ref="txAdvice" pointcut-ref="pt1"></aop:advisor>
</aop:config>
</beans>
Dao
package com.springlearning.dao.impl;
import com.springlearning.dao.IAccountDao;
import com.springlearning.entity.Account;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.RowMapper;
import java.util.List;
public class AccountDaoImpl implements IAccountDao {
JdbcTemplate jdbcTemplate;
public void setJdbcTemplate(JdbcTemplate jdbcTemplate) {
this.jdbcTemplate = jdbcTemplate;
}
//保存
public void saveAccount(Account account) {
String saveSql = "insert into accounts(id, name, money) value(?,?,?)";
jdbcTemplate.update(saveSql, account.getId(), account.getName(), account.getMoney());
}
//删除
public void deleteAccount(int id) {
String deleteSql = "delete from accounts where id = ?";
jdbcTemplate.update(deleteSql, id);
}
//更新
public void updateAccount(Account account) {
String updateSql = "update accounts set money = ? where id = ?";
jdbcTemplate.update(updateSql, account.getMoney(), account.getId());
}
//查询多个
public List<Account> searchAccounts(float money) {
String serchAccountsSql = "select * from accounts where money > ?";
RowMapper<Account> rowMapper = new BeanPropertyRowMapper<Account>(Account.class);
return jdbcTemplate.query(serchAccountsSql, rowMapper, money);
}
//查询一个
public Account searchAccountByName(String name) {
String searchAccountByName = "select * from accounts where name = ?";
RowMapper<Account> rowMapper = new BeanPropertyRowMapper<Account>(Account.class);
return jdbcTemplate.queryForObject(searchAccountByName, rowMapper, name);
}
//查询一行或一列
public Integer serchCount(float money) {
String searchCount = "select count(*) from accounts where money > ?";
return jdbcTemplate.queryForObject(searchCount, Integer.TYPE, money);
}
public void transfer(String sourceName, String targetName, float money) {
Account source = searchAccountByName(sourceName);
Account target = searchAccountByName(targetName);
source.setMoney(source.getMoney() - money);
target.setMoney(target.getMoney() + money);
updateAccount(source);
int i = 1/0;
updateAccount(target);
}
}
pom
<dependencies>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>5.0.7.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-jdbc</artifactId>
<version>5.0.7.RELEASE</version>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>6.0.6</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-test</artifactId>
<version>5.0.7.RELEASE</version>
</dependency>
<dependency>
<groupId>org.aspectj</groupId>
<artifactId>aspectjweaver</artifactId>
<version>1.9.1</version>
</dependency>
<!--spring 事务管理-->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-tx</artifactId>
<version>5.0.7.RELEASE</version>
</dependency>
</dependencies>