Spring 开启事务管理
来源:互联网 发布:软件研制任务书范文 编辑:程序博客网 时间:2024/06/11 05:00
1.导入 spring-tx-4.3.2.RELEASE.jar
2.开启spring的事务管理有两种声明式方法
- xml声明
<?xml version="1.0" encoding="UTF-8"?> <!-- 导入tx约束--><beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx" xmlns:context="http://www.springframework.org/schema/context" xsi:schemaLocation=" http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.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 http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd"> <!-- 配置事务管理器,对于不同的dao框架具有不同的事务管理器的实现类 jdbc/MyBatis:org.springframework.jdbc.datasource.DataSourceTransactionManager hibernate:org.springframework.orm.hibernate3.HibernateTransactionManager --> <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager"> <!-- 注入连接池(dbcp,c3p0....)--> <property name="dataSource" ref="dataSource"></property> </bean> <!-- 配置事务管理器具体要增强哪个方法--> <tx:advice id="advice" transaction-manager="transactionManager"> <tx:attributes> <tx:method name=“transfarAccounts"/> </tx:attributes> </tx:advice > <!-- 配置切入面和切点(spring实现事务是通过aop的方式实现的) --> <aop:config> <aop:pointcut expression="execution(* cn.yellowimg.service.transfarAccounts(..))" id="pointcut1"/> <aop:advisor advice-ref="advice" pointcut-ref="pointcut1"/> </aop:config></beans>
- 注解声明
<?xml version="1.0" encoding="UTF-8"?><beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx" xmlns:context="http://www.springframework.org/schema/context" xsi:schemaLocation=" http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.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 http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd"> <!-- 配置事务管理器,对于不同的dao框架具有不同的事务管理器的实现类 jdbc/MyBatis:org.springframework.jdbc.datasource.DataSourceTransactionManager hibernate:org.springframework.orm.hibernate3.HibernateTransactionManager --> <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager"> <!-- 注入连接池(dbcp,c3p0....)--> <property name="dataSource" ref="dataSource"></property> </bean> <!-- 注入连接池(dbcp,c3p0....)--> <tx:annotation-driven transaction-manager="transactionManager"/></beans>
在需要开启事务的类上声明注解
//开启事务,该类上的所有方法都会开启事务@Transactionalpublic class AccountService { @Resource(name="accountDao") private AccountDao accountDao; public void setAccountDao(AccountDao accountDao) { this.accountDao = accountDao; } public void makemoney() { accountDao.edit("小马", 1000); accountDao.edit("小王", -1000); }}
阅读全文
0 0
- Spring 开启事务管理
- Spring事务管理(5)-开启事务
- 事务管理之Spring事务管理
- Spring事务管理
- spring事务管理
- Spring事务管理
- Spring事务管理
- Spring事务管理
- Spring事务管理
- Spring事务管理
- Spring 事务管理
- Spring事务管理
- spring 事务管理
- spring 事务管理
- spring 事务管理
- spring事务管理
- Spring 事务管理
- Spring事务管理
- 用模板写单链表 尹成
- 数据结构之:栈与队列
- qt也可以使用控制台编程
- Tensorflow MPI Enable Intro
- webservice服务搭建
- Spring 开启事务管理
- 数据储存形式
- 面向对象实现简单版的超级马里奥
- maven本地上传jar包到私服库
- 《Android-Genymotion 启动ip占用问题》
- HDU-3237-Help Bubu
- 数据类型转换&运算符
- 红黑树详解博客
- GC算法实践(三) 标记-压缩算法