`

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>
复制代码
Java代码:
复制代码
@Transactional
    
public void doTransaction() {
        User user 
= new User();
        user.setName(
"11111");
        user.setSex(
1);
        userDao.saveUser(user);
        User user1 
= new User();
        user1.setName(
"Angikkk");
        user1.setSex(
1);
        userDao.saveUser(user1);
复制代码

    } 

分享到:
评论

相关推荐

    Spring开发指南

    事务管理 持久层封装 JDBC Hibernate in Spring ibatis in Spring Aspect Oriented Programming AOP 概念 AOP in Spring Dynamic Proxy 与Spring AOP CGLib 与 Spring AOP AOP 应用 DAO Support ...

    Spring in Action(第2版)中文版

    6.1.2理解spring对事务管理的支持 6.2选择事务管理器 6.2.1jdbc事务 6.2.2hibernate事务 6.2.3jpa事务 6.2.4jdo事务 6.2.5jta事务 6.3在spring中编写事务 6.4声明式事务 6.4.1定义事务参数 6.4.2代理事务 ...

    Spring in Action(第二版 中文高清版).part2

    6.1.2 理解Spring对事务管理的支持 6.2 选择事务管理器 6.2.1 JDBC事务 6.2.2 Hibernate事务 6.2.3 JPA事务 6.2.4 JDO事务 6.2.5 JTA事务 6.3 在Spring中编写事务 6.4 声明式事务 6.4.1 定义事务参数 ...

    Spring in Action(第二版 中文高清版).part1

    6.1.2 理解Spring对事务管理的支持 6.2 选择事务管理器 6.2.1 JDBC事务 6.2.2 Hibernate事务 6.2.3 JPA事务 6.2.4 JDO事务 6.2.5 JTA事务 6.3 在Spring中编写事务 6.4 声明式事务 6.4.1 定义事务参数 ...

    ibatis 开发指南(pdf)

    43 &lt;br&gt;ibatis 开发指南 相对Hibernate 和Apache OJB 等“一站式”ORM 解决方案而言,ibatis 是一种“半 自动化”的ORM 实现。 所谓“半自动”,可能理解上有点生涩。纵观目前主流的ORM ,无论...

    Spring基础与快速入门

    其他还有一些特点不是Spring的核心,这里只做简单陈述,如:对JDBC的封装与简化,提供事务管理功能,对O/R mapping工具(hibernate、iBATIS)的整合;提供MVC解决方案,也可以与其他web框架(Struts、JSF)进行整合...

    springmybatis

    3. 在session 中完成对数据的增删改查和事务提交等. 4. 在用完之后关闭session 。 5. 在java 对象和 数据库之间有做mapping 的配置文件,也通常是xml 文件。 mybatis实战教程(mybatis in action)之一:开发环境搭建 ...

    2013java面试题搜集

    有几种方式? 5、spring底层用什么实现? 6、hibernate和Jdbc的区别 7、Hibernate与Ibatis的区别 8、Struts2和1的区别 9、Struts2的执行机制 10、ConnectionPool的底层,不是问你设计模式 11、用java,实现 C...

    最新Java面试宝典pdf版

    46、java中有几种方法可以实现一个线程?用什么关键字修饰同步方法? stop()和suspend()方法为何不推荐使用? 29 47、sleep() 和 wait() 有什么区别? 30 48、同步和异步有何异同,在什么情况下分别使用他们?举例说明...

    Java面试宝典2010版

    46、java中有几种方法可以实现一个线程?用什么关键字修饰同步方法? stop()和suspend()方法为何不推荐使用? 47、sleep() 和 wait() 有什么区别? 48、同步和异步有何异同,在什么情况下分别使用他们?举例说明。 ...

    Java面试笔试资料大全

    46、java中有几种方法可以实现一个线程?用什么关键字修饰同步方法? stop()和suspend()方法为何不推荐使用? 29 47、sleep() 和 wait() 有什么区别? 30 48、同步和异步有何异同,在什么情况下分别使用他们?举例说明...

    JAVA面试宝典2010

    46、java中有几种方法可以实现一个线程?用什么关键字修饰同步方法? stop()和suspend()方法为何不推荐使用? 29 47、sleep() 和 wait() 有什么区别? 30 48、同步和异步有何异同,在什么情况下分别使用他们?举例说明...

    Java面试宝典-经典

    46、java中有几种方法可以实现一个线程?用什么关键字修饰同步方法? stop()和suspend()方法为何不推荐使用? 29 47、sleep() 和 wait() 有什么区别? 30 48、同步和异步有何异同,在什么情况下分别使用他们?举例说明...

    java面试题大全(2012版)

    46、java中有几种方法可以实现一个线程?用什么关键字修饰同步方法? stop()和suspend()方法为何不推荐使用? 29 47、sleep() 和 wait() 有什么区别? 30 48、同步和异步有何异同,在什么情况下分别使用他们?举例说明...

    Java面试宝典2012版

    46、java中有几种方法可以实现一个线程?用什么关键字修饰同步方法? stop()和suspend()方法为何不推荐使用? 29 47、sleep() 和 wait() 有什么区别? 30 48、同步和异步有何异同,在什么情况下分别使用他们?举例...

    java面试宝典2012

    46、java中有几种方法可以实现一个线程?用什么关键字修饰同步方法? stop()和suspend()方法为何不推荐使用? 32 47、sleep() 和 wait() 有什么区别? 33 48、同步和异步有何异同,在什么情况下分别使用他们?举例说明...

Global site tag (gtag.js) - Google Analytics