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.编程式事务:在代码中添加事务管理