有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java如何使用Spring/MyBatis事务?最佳实践?

我正在尝试使用MyBatis和Spring的事务,我想知道是否有实现这一点的最佳实践?如有任何提示或想法,我们将不胜感激

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


共 (1) 个答案

  1. # 1 楼答案

    您想看看@Transactional注释docs 就最佳实践而言,它是数据库事务和spring的混合体。看看您需要在哪里回滚数据,是否需要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>