spring事物控制

<tx:advice id="txAdvice" transaction-manager="transactionManager">
<tx:attributes>
<!--定义查询方法都是只读的 -->
<tx:method name="query*" read-only="true" />
<tx:method name="find*" read-only="true" />
<tx:method name="get*" read-only="true" />


<!-- 主库执行操作,事务传播行为定义为默认行为 -->
<tx:method name="save*" propagation="REQUIRED" />
<tx:method name="update*" propagation="REQUIRED" />
<tx:method name="delete*" propagation="REQUIRED" />
<tx:method name="insert*" propagation="REQUIRED" />
<tx:method name="add*" propagation="REQUIRED" />


<!--其他方法使用默认事务策略 -->
<!--后期可能需要用到   -->
<tx:method name="*" read-only="true"/>
</tx:attributes>
</tx:advice>


<aop:config>
<!-- 定义切面,所有的service的所有方法 -->
<aop:pointcut id="txPointcut" expression="execution(* cn.tevo.energyConservation.service..*.*(..))" />
<!-- 应用事务策略到Service切面 -->
<aop:advisor advice-ref="txAdvice" pointcut-ref="txPointcut"/>

</aop:config>

再写controler和service时注意,方法名称要和事物上的命名一样

特别注意的是,上图配置的是service子孙类的所有方法都拦截,所以在controller写service方法增删改注意规范

spring事物控制

增删改的数据库方法不能写在查的Service里面