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

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

完成!

posted @ 2019-11-04 14:10  林染plus  阅读(348)  评论(0编辑  收藏  举报