Spring 之注解事务 @Transactional

来源:互联网 发布:asp源码 编辑:程序博客网 时间:2024/06/07 14:51
1.定义一个数据源
<beanid="comboPooledDataSource"class="com.mchange.v2.c3p0.ComboPooledDataSource">
     <propertyvalue="${jdbc.driverClass}"name="driverClass"/>
     <propertyvalue="${jdbc.url}"name="jdbcUrl"/>
     <propertyvalue="${jdbc.username}"name="user"/>
     <propertyvalue="${jdbc.password}"name="password"/>
     </bean>
2.定义jdbc的jdbcTemplate
<beanid="jdbcTemplate"class="org.springframework.jdbc.core.JdbcTemplate">
     <propertyname="dataSource"ref="comboPooledDataSource"></property>
     
     </bean>
3.配置事物管理器
<beanname="transactionManager"class="DataSourceTransactionManager">
     <propertyname="dataSource"ref="comboPooledDataSource"></property>
</bean
4.开启事物
<!-- 开启注解事物管理   替代了xml里面的切面和通知-->
     <tx:annotation-driventransaction-manager="transactionManager"/>
5.在该Bean的代码中标注@Transactional
service层  逻辑处理层  也就是真正实现方法层