- 添加命名空间tx
- 创建对象
- 指定那一层的方法(指定需要事务管理的方法的位置
- 确定事务管理,哪些方法需要事务管理
从第3步开始,方法二
添加标签tx:advice
注解方法
添加命名空间tx
xmlns:tx="http://www.springframework.org/schema/tx"
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx.xsd
创建对象
<!--创建事务对象-->
<bean id="tx" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="db"/>
</bean>
指定那一层的方法(指定需要事务管理的方法的位置)
<!--确定需要事务管理的方法在什么位置-->
<aop:config>
<aop:pointcut id="p1" expression="execution(* com.fdk.service.impl.*.*(..))"/>
<aop:advisor advice-ref="mytx" pointcut-ref="p1"></aop:advisor>
</aop:config>
确定事务管理,哪些方法需要事务管理
<!--确定哪些方法需要执行事务管理-->
<tx:advice id="mytx" transaction-manager="tx">
<!--attributes 属性-->
<tx:attributes>
<tx:method name="zhuanzhang"/>
</tx:attributes>
</tx:advice>
方法二
在Spring.xml文件中添加标签
<!--注解管理事务-->
<tx:annotation-driven transaction-manager="tx"/>
在需要事务的方法或者类上面添加标签
@Transactional