【问题标题】:How to use transactions with Spring/MyBatis? Best practice?如何在 Spring/MyBatis 中使用事务?最佳实践?
【发布时间】:2011-08-23 22:29:57
【问题描述】:

我正在尝试将事务与 MyBatis 和 Spring 一起使用,并且想知道是否有关于如何实现此目的的最佳实践?任何提示或想法表示赞赏。

我的应用程序将在 tomcat 容器中针对 MySQL 数据库运行。

【问题讨论】:

    标签: java spring tomcat transactions mybatis


    【解决方案1】:

    你想看看@Transactional注解docs 就最佳实践而言,它是数据库事务和弹簧的混合体。看看你需要回滚数据的地方,是否需要 JTA 等。

    示例类

    @Transactional
    public class DefaultFooService implements FooService {
    
       Foo getFoo(String fooName);
    }
    

    示例 xml

    <!-- this is the service object that we want to make transactional -->
    <bean id="fooService" class="x.y.service.DefaultFooService"/>
    <!-- enable the configuration of transactional behavior based on annotations -->         <tx:annotation-driven transaction-manager="txManager"/>
    
    <!-- a PlatformTransactionManager is still required -->
    <bean id="txManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
    <!-- (this dependency is defined somewhere else) -->
        <property name="dataSource" ref="dataSource"/>
    </bean>
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2018-09-25
      • 1970-01-01
      • 2012-05-31
      • 1970-01-01
      • 2015-03-18
      • 2015-09-11
      • 2020-03-08
      • 2010-12-22
      相关资源
      最近更新 更多