springmvc+mybatis事務回滾


spring-mybatis.xml中 配置了

<!-- 攔截器方式配置事物 -->
	<tx:advice id="transactionAdvice" transaction-manager="transactionManager">
		<tx:attributes>
			<tx:method name="add*" propagation="REQUIRED" />
			<tx:method name="append*" propagation="REQUIRED" />
			<tx:method name="insert*" propagation="REQUIRED" />
			<tx:method name="save*" propagation="REQUIRED" />
			<tx:method name="update*" propagation="REQUIRED" />
			<tx:method name="modify*" propagation="REQUIRED" />
			<tx:method name="edit*" propagation="REQUIRED" />
			<tx:method name="delete*" propagation="REQUIRED" />
			<tx:method name="remove*" propagation="REQUIRED" />
			<tx:method name="repair" propagation="REQUIRED" />
			<tx:method name="delAndRepair" propagation="REQUIRED" />
			<tx:method name="get*" propagation="SUPPORTS" />
			<tx:method name="find*" propagation="SUPPORTS" />
			<tx:method name="load*" propagation="SUPPORTS" />
			<tx:method name="search*" propagation="SUPPORTS" />
			<tx:method name="datagrid*" propagation="SUPPORTS" />

			<tx:method name="*" propagation="SUPPORTS" />
		</tx:attributes>
	</tx:advice>

  spring-mvc.xml中配置了

<!-- 自動掃描controller包下的所有類,使其認為spring mvc的控制器 -->
	<context:component-scan base-package="com.ms.controller" />

  開啟了自動掃描,發現service層並不能回滾。

  在spring-mvc.xml添加

<context:component-scan base-package="com.njzaizao">  
		<context:exclude-filter type="annotation" expression="org.springframework.stereotype.Service" />  
	</context:component-scan>  

  此為:掃描所有的controller 但是不掃描service

  這樣service層就能自動開啟事務了

  但是這樣有個問題,service層不能使用try catch

  如果必須在service層捕捉異常的話可使用 注解 @Transactional


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM