Java教程

Spring事务管理

本文主要是介绍Spring事务管理,对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!

  一个使用 MyBatis-Spring 的其中一个主要原因是它允许 MyBatis 参与到 Spring 的事务管理中。而不是给 MyBatis 创建一个新的专用事务管理器,MyBatis-Spring 借助了 Spring 中的 DataSourceTransactionManager 来实现事务管理。

  

  Spring中的事务管理

  1.声明式事务:AOP,将代码横切进去【一般用这种方式】

    注意导入AOP的依赖!!!

    <!--开始声明事务-->
    <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
        <property name="dataSource" ref="dataSource"/>
    </bean>
    <!--配置事务通知-->
    <tx:advice id="txadvice" transaction-manager="transactionManager">
        <tx:attributes>
            <tx:method name="add"/>
            <tx:method name="delete"/>
            <tx:method name="update"/>
        </tx:attributes>
    </tx:advice>
    <!--使用将事务织入切点中-->
    <aop:config>
        <aop:pointcut id="txCutPoint" expression="execution(* com.chen.mapper.*.*(..))"/>
        <aop:advisor advice-ref="txadvice" pointcut-ref="txCutPoint"/>
    </aop:config>

  

2.编程式事务:在代码中添加事务管理

  

这篇关于Spring事务管理的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!