Spring事务配置-注解篇

事务注解,可以作用在类或者public方法上,写在类上的时候,对该类下的所有的public的方法有用。假如方法上加了final修饰,将会导致事务不可用。

具体参考:https://blog.csdn.net/bao19901210/article/details/41724355

举例说明:myBatis为例   基于注解的声明式事务管理配置@Transactional

spring.xml

<span style="background-color: rgb(255, 255, 255);"><span style="background-color: rgb(255, 204, 153);"><!-- mybatis config -->
    <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
        <property name="dataSource" ref="dataSource" />
        <property name="configLocation">
            <value>classpath:mybatis-config.xml</value>
        </property>
    </bean>
    
    <!-- mybatis mappers, scanned automatically -->
    <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
        <property name="basePackage">
            <value>
                com.baobao.persistence.test
            </value>
        </property>
        <property name="sqlSessionFactory" ref="sqlSessionFactory" />
    </bean>
    
    <!-- 配置spring的PlatformTransactionManager,名字为默认值 -->
    <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
        <property name="dataSource" ref="dataSource" />
    </bean>
    
    <!-- 开启事务控制的注解支持 -->
    <tx:annotation-driven transaction-manager="transactionManager"/></span></span>

添加tx名字空间

<span style="background-color: rgb(255, 255, 255);"><span style="background-color: rgb(255, 204, 153);">xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx"
 
xsi:schemaLocation="http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.0.xsd
    http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.0.xsd"</span></span>

猜你喜欢

转载自blog.csdn.net/weixin_42028409/article/details/85161733