Spring管理iBatis事务
<sqlMapConfig> <sqlMap resource="com/angi/ibatis/maps/User.xml" /> </sqlMapConfig>
以上配置省去了transactionManager的配置,就会使用external(外部)事务管理(ExternalTransaction),即等同如下配置:
<sqlMapConfig> <transactionManager type="EXTERNAL"> <!--这个数据源其实没有什么意义,还是取上面的省略方式吧--> <dataSource type="DBCP"> </dataSource> </transactionManager> <sqlMap resource="com/angi/ibatis/maps/User.xml" /> </sqlMapConfig>
1、TransactionProxyFactoryBean
<?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:p="http://www.springframework.org/schema/p" xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.0.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.0.xsd "> <!-- DataSource --> <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource"> <property name="driverClassName"> <value>com.mysql.jdbc.Driver</value> </property> <!--<property name="defaultAutoCommit" value="false"/>--> <property name="url"> <value>jdbc:mysql://localhost/test</value> </property> <property name="username"> <value>root</value> </property> <property name="password"> <value>mysql</value> </property> </bean> <!-- Spring iBatis Template --> <bean id="sqlMapClient" class="org.springframework.orm.ibatis.SqlMapClientFactoryBean"> <property name="configLocation" value="SqlMapConfig.xml" /> <property name="dataSource" ref="dataSource" /> </bean> <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager"> <property name="dataSource"> <ref local="dataSource" /> </property> </bean> <bean id="userDAO" class="com.angi.ibatis.dao.UserDaoImpl"> <property name="sqlMapClient"> <ref bean="sqlMapClient" /> </property> </bean> <bean id="userService" class="com.angi.ibatis.service.UserService"> <property name="userDao"> <ref bean="userDAO" /> </property> </bean> <bean id="userServiceProxy" class="org.springframework.transaction.interceptor.TransactionProxyFactoryBean"> <property name="transactionManager"> <ref bean="transactionManager" /> </property> <property name="target"> <ref local="userService" /> </property> <property name="transactionAttributes"> <props> <!-- 这里的方法签名可以精确到方法, 先懒惰一下全配置上 --> <prop key="*">PROPAGATION_REQUIRED</prop> </props> </property> </bean> </beans>
2、TransactionInterceptor
<?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:p="http://www.springframework.org/schema/p" xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.0.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.0.xsd "> <!-- DataSource --> <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource"> <property name="driverClassName"> <value>com.mysql.jdbc.Driver</value> </property> <!--<property name="defaultAutoCommit" value="false"/>--> <property name="url"> <value>jdbc:mysql://localhost/test</value> </property> <property name="username"> <value>root</value> </property> <property name="password"> <value>mysql</value> </property> </bean> <!-- Spring iBatis Template --> <bean id="sqlMapClient" class="org.springframework.orm.ibatis.SqlMapClientFactoryBean"> <property name="configLocation" value="SqlMapConfig.xml" /> <property name="dataSource" ref="dataSource" /> </bean> <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager"> <property name="dataSource"> <ref local="dataSource" /> </property> </bean> <bean id="userDAO" class="com.angi.ibatis.dao.UserDaoImpl"> <property name="sqlMapClient"> <ref bean="sqlMapClient" /> </property> </bean> <bean id="userService" class="com.angi.ibatis.service.UserService"> <property name="userDao"> <ref bean="userDAO" /> </property> </bean> <bean class="org.springframework.aop.framework.autoproxy.BeanNameAutoProxyCreator"> <property name="beanNames"> <list> <value>userService</value> </list> </property> <property name="interceptorNames"> <list> <value>transactionInterceptor</value> </list> </property> </bean> <bean id="transactionInterceptor" class="org.springframework.transaction.interceptor.TransactionInterceptor"> <property name="transactionManager" ref="transactionManager" /> <property name="transactionAttributes"> <props> <!-- 这里的方法签名可以精确到方法, 先懒惰一下全配置上 --> <prop key="*">PROPAGATION_REQUIRED</prop> </props> </property> </bean> </beans>
3、AOP和TX配置
<?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:p="http://www.springframework.org/schema/p" xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.0.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.0.xsd "> <!-- DataSource --> <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close"> <property name="driverClassName"> <value>com.mysql.jdbc.Driver</value> </property> <property name="url"> <value>jdbc:mysql://localhost/test</value> </property> <property name="username"> <value>root</value> </property> <property name="password"> <value>mysql</value> </property> </bean> <!-- Spring iBatis Template --> <bean id="sqlMapClient" class="org.springframework.orm.ibatis.SqlMapClientFactoryBean"> <property name="configLocation" value="SqlMapConfig.xml" /> <property name="dataSource" ref="dataSource" /> </bean> <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager"> <property name="dataSource" ref="dataSource" /> </bean> <!-- 需要引入aop的命名空间 --> <aop:config> <!-- 切入点指明了在所有方法产生事务拦截操作 --> <aop:pointcut id="serviceMethods" expression="execution(* com.angi.ibatis.service.*.*(..))" /> <!-- 定义了将采用何种拦截操作,这里引用到 txAdvice --> <aop:advisor advice-ref="txAdvice" pointcut-ref="serviceMethods" /> </aop:config> <!-- 需要引入tx的命名空间 --> <!-- 这是事务通知操作,使用的事务管理器引用自 transactionManager --> <tx:advice id="txAdvice" transaction-manager="transactionManager"> <tx:attributes> <!-- 指定哪些方法需要加入事务,这里懒惰一下全部加入,可以使用通配符来只加入需要的方法 --> <tx:method name="*" propagation="REQUIRED" /> </tx:attributes> </tx:advice> <bean id="userDAO" class="com.angi.ibatis.dao.UserDaoImpl"> <property name="sqlMapClient"> <ref bean="sqlMapClient" /> </property> </bean> <bean id="userService" class="com.angi.ibatis.service.UserService"> <property name="userDao"> <ref bean="userDAO" /> </property> </bean> </beans>
4、anotation
<?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:p="http://www.springframework.org/schema/p" xmlns:tx="http://www.springframework.org/schema/tx" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.0.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.0.xsd "> <!-- 需要引入tx的命名空间 --> <tx:annotation-driven transaction-manager="transactionManager" /> <!-- DataSource --> <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource"> <property name="driverClassName"> <value>com.mysql.jdbc.Driver</value> </property> <!--<property name="defaultAutoCommit" value="false"/>--> <property name="url"> <value>jdbc:mysql://localhost/test</value> </property> <property name="username"> <value>root</value> </property> <property name="password"> <value>mysql</value> </property> </bean> <!-- Spring iBatis Template --> <bean id="sqlMapClient" class="org.springframework.orm.ibatis.SqlMapClientFactoryBean"> <property name="configLocation" value="SqlMapConfig.xml" /> <property name="dataSource" ref="dataSource" /> </bean> <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager"> <property name="dataSource"> <ref local="dataSource" /> </property> </bean> <bean id="userDAO" class="com.angi.ibatis.dao.UserDaoImpl"> <property name="sqlMapClient"> <ref bean="sqlMapClient" /> </property> </bean> <bean id="userService" class="com.angi.ibatis.service.UserService"> <property name="userDao"> <ref bean="userDAO" /> </property> </bean> </beans>
相关推荐
很好的spring+ibatis事务的配置文档.
SPRING与IBATIS整合采用保留IBATIS事务,可自己控制回滚!
spring+ibatis声明式事务Demo源码
spring ibatis 配置spring ibatis 配置spring ibatis 配置spring ibatis 配置
SPRING与IBATIS整合采用保留IBATIS事务,可自己控制回滚!
spring+ibatis声明式事务Demo
本工程以“银行账号转账”为例子演示了如何处理Spring、Ibatis结合MySQL数据库使用时的事务操作 本工程编码方式:UTF-8 须执行的SQL语句: CREATE DATABASE `test`; USE `test`; CREATE TABLE `lm_bank_card` ( `...
struts2整合spring、ibatis学生信息管理系统。事务管理、分页处理
自己配置的springmvc+ibatis框架,自己找齐了包,不容易啊,所以上来跟大家分享一下,spring自动注入包,事务包,ibatis包,数据库连接包,都齐全的,仅局限于springmvc+ibatis,这些包不多,不少,刚够
Spring+iBatis+JOTM实现JTA事务 spring ibatis jotm 分布 事务 多数据源
NULL 博文链接:https://ymgjava.iteye.com/blog/1117624
ibatis 完美例子 一对多 批处理 事务 和 spring struts2集成 ,一朵多 插入1万条数据,不到2秒,备注不包含类库
本例子通过Spring容器管理ibatis dwr2的javaBean以及事务,Extjs通过dwr2来调用Spring管理类
为了开发方便,打算换一个架构,自己整合了s2sibatis框架,采用spring事务管理数据库,喜欢的可以看看,有什么问题请多多指教!
ibatis+spring事务控制 压缩文件中有很多例子,如果控制事务,事务不回滚的解决方法等等。另外附上python学习文档。
系统采用spring2.5 + ibatis2.3.4,采用注解方式配置,非xml配置方式。包括分页排序查询、新增、修改、删除、登陆验证功能,数据库采用oracle。
代码下载下来,想要了解更多相关内容可以看http://www.cnblogs.com/shamo89/p/7307961.html
这是一个servlet + spring + ibatis 的完整例子,里面包含了spring的事务管理以及所有的jar包,下载下来后直接改下数据库配置就可以运行!从界面到action,业务层,dao层有现成的代码!
NULL 博文链接:https://wilr.iteye.com/blog/1190524