美文网首页
spring的事务管理(九)

spring的事务管理(九)

作者: 梦捷者 | 来源:发表于2019-09-29 22:38 被阅读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("转账成功");
    }

}

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

网上查找

相关文章

  • Spring中的AOP事务

    【目录】1 Spring的事务管理机制2 Spring事务管理两种方式 1 Spring的事务管理机制 Sprin...

  • Spring事务管理只对出现运行期异常进行回滚

    使用spring难免要用到spring的事务管理,要用事务管理又会很自然的选择声明式的事务管理,在spring的文...

  • spring事务

    1、spring事务管理器PlatformTransactionManager 1.1、没有spring事务管理器...

  • Spring之事务管理

    Spring事务管理(详解+实例)Spring详解(八)------事务管理 一. 概念 事务(Transacti...

  • Spring-事务机制

    一、Spring事务 事务管理概述 Spring事务管理分为编程式事务管理和声明式事务管理两种 编程式事务:允许用...

  • 19、Spring-事务机制-使用

    一、简介 Spring事务管理分为编程式事务管理和声明式事务管理两种, 声明式事务管理:底层是建立在Spring ...

  • Spring声明式事务管理之一:五大属性分析

    1.Spring事务管理概述 Spring事务管理分为编程式事务管理和声明式事务管理两种。编程式事务允许用户在实现...

  • Spring基础(三)

    11. 事务管理 11.1 Spring Framework事务管理介绍 广泛的事务支持是Spring Frame...

  • 【济南中心】 Spring事务管理的方式

    【济南中心】 Spring事务管理的方式 1.spring支持编程式事务管理和声明式事务管理两种方式 编程...

  • Spring事务的种类

    Spring事务的种类: spring支持编程式事务管理和声明式事务管理两种方式: ①编程式事务管理使用Trans...

网友评论

      本文标题:spring的事务管理(九)

      本文链接:https://www.haomeiwen.com/subject/cdvnyctx.html