搭建一個(gè)SSM項(xiàng)目 -6: 增加“事務(wù)”

現(xiàn)在應(yīng)該增加數(shù)據(jù)庫(kù)的事務(wù)管理了。
spring-myBatis的事務(wù)管理

事務(wù)管理的話,是用AOP實(shí)現(xiàn)的

1:引入依賴(lài)

<!--aop的框架-->
    <dependency>
      <groupId>org.springframework</groupId>
      <artifactId>spring-aop</artifactId>
      <version>${springVersion}</version>
    </dependency>
<!--分析切點(diǎn)表達(dá)式的框架-->
    <dependency>
      <groupId>org.aspectj</groupId>
      <artifactId>aspectjweaver</artifactId>
      <version>1.9.5</version>
    </dependency>

2:怎么用?

beans.xml里增加以下

    <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
        <!--這里的dataSource bean 要和org.mybatis.spring.SqlSessionFactoryBean里面的用同一個(gè)-->
        <constructor-arg ref="dataSource"/>
    </bean>

  <!--增強(qiáng)-->
    <tx:advice id="tx" transaction-manager="transactionManager">
        <tx:attributes>
            <tx:method name="fetch*" propagation="SUPPORTS" read-only="false"/>
            <tx:method name="*" propagation="REQUIRED" read-only="false"/>
        </tx:attributes>
    </tx:advice>

<!--切面在哪里,切到哪里-->
    <aop:config>
        <aop:pointcut id="pc" expression="execution(* com.ppf.service.*.*(..))"/>
        <aop:advisor advice-ref="tx" pointcut-ref="pc"/>
    </aop:config>
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書(shū)系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

友情鏈接更多精彩內(nèi)容