Spring-JDBC事务2
1.事务控制钟xml方式与注解方式共同使用 2.xml方式
<!--aop自动代理-->
<aop:aspectj-***/>
<!--配置事务管理器-->
<bean id="txManger" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="dataSource"/>
</bean>
<!--配置事务通知 transaction-manager-事务通知绑定的是哪个管理器-->
<tx:advice id="txAdvice" transaction-manager="txManger">
<tx:attributes>
<!--定义什么方法使用通知-->
<tx:method name="add*" propagation="REQUIRED"/>
<tx:method name="update*" propagation="REQUIRED"/>
<tx:method name="delete*" propagation="REQUIRED"/>
<tx:method name="query*" read-only="true"/>
</tx:attributes>
</tx:advice>
<!--进行aop的配置-->
<aop:config>
<aop:pointcut id="cut" expression="(* org.example.Service..*.*(..))"/>
<aop:advisor advice-ref="txAdvice" pointcut="cut"/>
</aop:config>
2.注解方式
<!--aop自动代理-->
<aop:aspectj-***/>
<!--配置事务管理器-->
<bean id="txManger" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="dataSource"/>
</bean>
<!--开启自动注解-->
<tx:annotation-driven transaction-manager="txManger"/>
3.最终的spring.xml文件
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="http://www.springframework.org/schema/beans
https://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop.xsd">
<context:component-scan base-package="org.example"/>
<!--加载jdbc.properties数据源-->
<context:property-placeholder location="jdbc.properties"/>
<!--配置c3p0数据源-->
<bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
<!--通过property标签配置对应的值,value属性对应的是properties配置文件中的值-->
<property name="driverClass" value="${jdbc.driver}"/>
<property name="jdbcUrl" value="${jdbc.url}"/>
<property name="user" value="${jdbc.user}"/>
<property name="password" value="${jdbc.password}"/>
</bean>
<!--配置jdbcTemplate模板对象,并注入一个数据源-->
<bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate">
<property name="dataSource" ref="dataSource"/>
</bean>
<!--aop自动代理-->
<aop:aspectj-***/>
<!--配置事务管理器-->
<bean id="txManger" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="dataSource"/>
</bean>
<!--开启自动注解-->
<tx:annotation-driven transaction-manager="txManger"/>
<!--配置事务通知 transaction-manager-事务通知绑定的是哪个管理器-->
<tx:advice id="txAdvice" transaction-manager="txManger">
<tx:attributes>
<!--定义什么方法使用通知-->
<tx:method name="add*" propagation="REQUIRED"/>
<tx:method name="update*" propagation="REQUIRED"/>
<tx:method name="delete*" propagation="REQUIRED"/>
<tx:method name="query*" read-only="true"/>
</tx:attributes>
</tx:advice>
<!--进行aop的配置-->
<aop:config>
<aop:pointcut id="cut" expression="(* org.example.Service..*.*(..))"/>
<aop:advisor advice-ref="txAdvice" pointcut="cut"/>
</aop:config>
</beans>