zoukankan      html  css  js  c++  java
  • Spring事务经典案例-银行转账

    1.entity实体类

    2.dao层

     3.dao实现类

     4.service层

     5.serviceimpl层

     6.大配置.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:p="http://www.springframework.org/schema/p"
           xmlns:context="http://www.springframework.org/schema/context" 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/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 http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd">
        <context:property-placeholder location="jdbc.properties.properties"/>
        <bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
            <property name="driverClassName" value="${jdbc.driver}"></property>
            <property name="url" value="${jdbc.url}"></property>
            <property name="username" value="${jdbc.username}"></property>
            <property name="password" value="${jdbc.password}"></property>
        </bean>
        <!--.构建jdbcTemplate-->
        <bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate">
            <property name="dataSource" ref="dataSource"></property>
        </bean>
        <bean id="TransferMoney" class="com.spring.dao.AddMoneyImpl">
            <property name="jdbcTemplate" ref="jdbcTemplate"></property>
        </bean>
        <bean id="transferMoneyImpl" class="com.spring.service.TransferMoneyImpl">
            <property name="dao" ref="TransferMoney"></property>
        </bean>
    
        <!--事务代理工厂Bean-->
        <bean id="transactionProxyFactoryBean" class="org.springframework.transaction.interceptor.TransactionProxyFactoryBean">
            <!--事务管理器-->
            <property name="transactionManager" ref="transactionManager"></property>
            <!--目标对象-->
            <property name="target" ref="transferMoneyImpl"></property>
            <!--设置方法-->
            <property name="transactionAttributes">
                <props>
                    <!--方法对应的隔离级别和传播行为-->
                    <prop key="transfer">PROPAGATION_REQUIRED,ISOLATION_DEFAULT</prop>
                </props>
            </property>
        </bean>
        <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
            <property name="dataSource" ref="dataSource"/>
        </bean>
        <!--&lt;!&ndash;配置Spring的事务管理器,默认在发生异常的情况下回滚,否则提交&ndash;&gt;
        &lt;!&ndash; 通知 &ndash;&gt;
        <tx:advice transaction-manager="transactionManager" id="txAdvice">
            <tx:attributes>
                <tx:method name="transferMoney" propagation="REQUIRED"
                           isolation="READ_COMMITTED" />&lt;!&ndash; transferMoney的事务隔离级别和传播行为 &ndash;&gt;
            </tx:attributes>
        </tx:advice>
        &lt;!&ndash; 切面 &ndash;&gt;
        <aop:config>
            <aop:pointcut expression="execution(* com.spring.service.*.*(..))"
                          id="myCut" />
            <aop:advisor advice-ref="txAdvice" pointcut-ref="myCut" />
        </aop:config>-->
    </beans>
    View Code

    7.jdbc.properties配置

     8.测试类

     结果:

    控制台报:by zero

    数据库里的数据没有改变!

    完成!

  • 相关阅读:
    MySQL 之 索引原理与慢查询优化
    MySQL 之【视图】【触发器】【存储过程】【函数】【事物】【数据库锁】【数据库备份】
    MySQL 之 数据操作
    MySQL 之 表操作
    MySQL 之 库操作
    MySQL 之 基本概念
    MySQL 练习2
    MySQL 练习
    MySQL 环境搭建之解压方式安装
    python3 下载必应每日壁纸(三)
  • 原文地址:https://www.cnblogs.com/mayuan01/p/11791763.html
Copyright © 2011-2022 走看看