spring的事务管理(九)

2019-09-29  本文已影响0人  梦捷者

一、基于XML的方式声明事务

1、在上节的代码进行操作
2、导入相关的jar包

3、在AccountDao中加入转转方法transfer()和在其实现类中实现此方法

public void transfer(String outUser,String inUser,Double money);//outUser,收款人;inUser,汇款人;money,收款金额。
    @Override
    public void transfer(String outUser, String inUser, Double money) {
        // TODO Auto-generated method stub
        //收款时,收款用户的余额=现有余额+所汇金额
        this.jdbcTempalet.update("update account set balance=balance+?"+"where username=?",money,inUser);
        //模拟发生突发性问题
//      int i=1/0;
        //汇款时,汇款用户的余额=现有余额-所汇金额
        this.jdbcTempalet.update("update account set balance=balance-?"+"where username=?",money,outUser);
        
    }

4、在src目录下进行applicationContext.xml文件的编写

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:aop="http://www.springframework.org/schema/aop"
    xmlns:tx="http://www.springframework.org/schema/tx"
    xmlns:context="http://www.springframework.org/schema/context"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    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
        http://www.springframework.org/schema/context
        http://www.springframework.org/schema/context/spring-context.xsd">

<!--1、配置数据源 -->
<bean id="dataSource"
    class="org.springframework.jdbc.datasource.DriverManagerDataSource">
    <!-- 数据库驱动 -->
    <property name="driverClassName" value="com.mysql.jdbc.Driver">
    </property>
    <!-- 连接数据库的url -->
    <property name="url" value="jdbc:mysql://localhost:3306/hu?characterEncoding=UTF8" />
    <property name="username" value="root"></property>
    <property name="password" value="qwe123"></property>
</bean>
<!-- 2、配置JDBC模板 -->
<bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate">
<!-- 默认必须使用数据源 -->
<property name="dataSource" ref="dataSource"></property>
</bean>

<!-- 3、定义accountDao的bean -->
<bean id="accountDao" class="com.itheima.jdbc.AccountDaoImpl">
<!-- 将jdbcTemplate注入到accountDao实例中 -->
<property name="jdbcTempalet" ref="jdbcTemplate"></property>
</bean>

<!-- 4、事务管理器,依赖与数据源 -->
<bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="dataSource"></property>
</bean>

<!-- 5、编写通知,对事务进行增强(通知),需要编写对切入点和具体执行事务细节 -->
<tx:advice id="txAdvice" transaction-manager="transactionManager">
<tx:attributes>
<tx:method name="*" propagation="REQUIRED" isolation="DEFAULT" read-only="false"></tx:method>
</tx:attributes>
</tx:advice>

<!-- 编写dao,让Spring自动对目标生成代理,需要使用AspectJ的表达式 -->
<aop:config>
<aop:pointcut expression="execution(* com.itheima.jdbc.*.*(..))" id="txPointCut"/>
<!-- 切面:将切入点与通知整合 -->
<aop:advisor advice-ref="txAdvice" pointcut-ref="txPointCut"/>
</aop:config>

</beans>

5、在com.itheima.jdbc中创建TransactionTest.java,来进行当执行错误时会执行事务回滚

package com.itheima.jdbc;

import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class TransactionTest {
    @Test   
    public void  transferTest(){
        //加载配置文件
                ApplicationContext app2=new ClassPathXmlApplicationContext("applicationContext.xml");
                //获取JdbcTemplate实例
                AccountDao accountdao=(AccountDao)app2.getBean("accountDao");
                accountdao.transfer("jack", "tony", 100.0);
                System.out.print("转账成功");
    }

}

二、基于注解的方式声明事务

网上查找

上一篇 下一篇

猜你喜欢

热点阅读