1.配置步骤
- 1 配置三层架构之间的Bean标签及互相之间的依赖关系
- 2.配置事务管理器
- 3.配置事务通知并与事务管理器联系起来
- 4.配置切入点表达式,并于事务通知连接起来
- 事务通知的目的:通过动态代理的方式对方法进行增强,对异常事务可以进行回滚等操作
- 切入点表达式的目的:是为了确定哪个类的哪个方法需要进行事务通知
2.配置参数
- 配置事务的属性
isolation:用于指定事务的隔离级别。默认值是DEFAULT,表示使用数据库的默认隔离级别。
propagation:用于指定事务的传播行为。默认值是REQUIRED,表示一定会有事务,增删改的选择。查询方法可以选择SUPPORTS。
read-only:用于指定事务是否只读。只有查询方法才能设置为true。默认值是false,表示读写。
timeout:用于指定事务的超时时间,默认值是-1,表示永不超时。如果指定了数值,以秒为单位。
rollback-for:用于指定一个异常,当产生该异常时,事务回滚,产生其他异常时,事务不回滚。没有默认值。表示任何异常都回滚。
no-rollback-for:用于指定一个异常,当产生该异常时,事务不回滚,产生其他异常时事务回滚。没有默认值。表示任何异常都回滚。
3.配置文件
<?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: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/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">
<!--通过xml的方式来配置Spring框架中的事物-->
<!--1.配置控制层对象-->
<bean id="AccountControl" class="com.itheima.Control.AccountControl">
<property name="accountService" ref="AccountService"></property>
</bean>
<!--2.配置业务层 -->
<bean id="AccountService" class="com.itheima.Service.AccountService">
<property name="accountDao" ref="AccountDao"></property>
</bean>
<!--3.配置dao层-->
<bean id="AccountDao" class="com.itheima.Dao.AccountDao">
<property name="jdbcTemplate" ref="Jdbctemple"></property>
</bean>
<bean id="Jdbctemple" class="org.springframework.jdbc.core.JdbcTemplate">
<property name="dataSource" ref="DataSource"></property>
</bean>
<bean id="DataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="url" value="jdbc:mysql://localhost:3306/spring"></property>
<property name="driverClassName" value="com.mysql.jdbc.Driver"></property>
<property name="username" value="root"></property>
<property name="password" value="root"></property>
</bean>
<!--4.配置事务管理器-->
<bean id="transationManage" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="DataSource"></property>
</bean>
<!--5.配置事务的通知-->
<!--在通知中配置事务的属性-->
<tx:advice id="Txadvice" transaction-manager="transationManage">
<tx:attributes>
<!--配置方法名称 及对应的事务控制属性
1、PROPAGATION_REQUIRED:如果当前没有事务,就创建一个新事务,如果当前存在事务,就加入该事务,该设置是最常用的设置。
2、PROPAGATION_SUPPORTS:支持当前事务,如果当前存在事务,就加入该事务,如果当前不存在事务,就以非事务执行。‘
在Service的方法进行增删改 用PROPAGATION_REQUIRED
查询的时候,用PROPAGATION_SUPPORTS
-->
<tx:method name="transform" propagation="REQUIRED" read-only="false"/>
</tx:attributes>
</tx:advice>
<!--6.配置切入点表达式-->
<aop:config>
<aop:pointcut id="pt1" expression="execution(* com.itheima.Service.*.*(..))"></aop:pointcut>
<!--7.在切面中配置切入点表达式和通知的关系-->
<aop:advisor advice-ref="Txadvice" pointcut-ref="pt1"></aop:advisor>
</aop:config>
</beans>
4.其他代码
public class AccountControl {
private AccountService accountService = null;
public void setAccountService(AccountService accountService) {
this.accountService = accountService;
}
public void transform(String resource, String target, float money){
accountService.transform(resource,target,money);
}
}
public class AccountService {
private AccountDao accountDao = null;
public void setAccountDao(AccountDao accountDao) {
this.accountDao = accountDao;
}
public void transform(String resource, String target, float money) {
try {
accountDao.transform(resource,target,money);
int i = 1/0;
}catch (Exception e){
throw new RuntimeException(e);
}
}
}
public class AccountDao implements IAccountDao {
private JdbcTemplate jdbcTemplate = null;
public void setJdbcTemplate(JdbcTemplate jdbcTemplate) {
this.jdbcTemplate = jdbcTemplate;
}
public void updateAccount(Account account) {
jdbcTemplate.update("update account set name = ?,money = ? where id = ? ",account.getName(),account.getMoney(),account.getId());
}
public void transform(String resource,String target,float money) throws SQLException {
List<Account> res = jdbcTemplate.query("select * from account where name = ? ",new BeanPropertyRowMapper<Account>(Account.class),resource);
List<Account> tar = jdbcTemplate.query("select * from account where name = ? ", new BeanPropertyRowMapper<Account>(Account.class),target);
System.out.println("res = " + res);
System.out.println("tar = " + tar);
if(res==null||tar==null||res.size()>1||tar.size()>1){
throw new RuntimeException("用户信息错误");
}
Account re = res.get(0);
Account ta = tar.get(0);
re.setMoney(re.getMoney()-money);
ta.setMoney(ta.getMoney()+money);
updateAccount(re);
updateAccount(ta);
}
}
public class Account {
private Integer id;
private String name;
private float money;
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 float getMoney() {
return money;
}
public void setMoney(float money) {
this.money = money;
}
}