---
title: "Spring管理iBatis事务的几种方法"
author: "chengtong_java"
date: "2015-11-04T16:59:40Z"
tags: []
categories: ["企业架构"]
slug: "spring-ibatis"
draft: false
lastmod: "2015-11-04T16:59:40Z"
id: 2254457
url: "https://chengtong-java.iteye.com/blog/2254457"
---

&lt;sqlMapConfig&gt;  
    &lt;sqlMap resource="com/angi/ibatis/maps/User.xml" /&gt;  
&lt;/sqlMapConfig&gt;

以上配置省去了transactionManager的配置，就会使用external（外部）事务管理（ExternalTransaction），即等同如下配置：

[![复制代码](http://common.cnblogs.com/images/copycode.gif)]()

&lt;sqlMapConfig&gt;  
    &lt;transactionManager type="EXTERNAL"&gt;  
                &lt;!--这个数据源其实没有什么意义，还是取上面的省略方式吧--&gt;  
        &lt;dataSource type="DBCP"&gt;  
        &lt;/dataSource&gt;  
    &lt;/transactionManager&gt;  
    &lt;sqlMap resource="com/angi/ibatis/maps/User.xml" /&gt;  
&lt;/sqlMapConfig&gt;

[![复制代码](http://common.cnblogs.com/images/copycode.gif)]()

方法 1、TransactionProxyFactoryBean

[![复制代码](http://common.cnblogs.com/images/copycode.gif)]()

&lt;?xml version="1.0" encoding="UTF-8"?&gt;  
&lt;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 "&gt;  
    &lt;!-- DataSource --&gt;  
    &lt;bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource"&gt;  
        &lt;property name="driverClassName"&gt;  
            &lt;value&gt;com.mysql.jdbc.Driver&lt;/value&gt;  
        &lt;/property&gt;  
        &lt;!--&lt;property name="defaultAutoCommit" value="false"/&gt;--&gt;  
        &lt;property name="url"&gt;  
            &lt;value&gt;jdbc:mysql://localhost/test&lt;/value&gt;  
        &lt;/property&gt;  
        &lt;property name="username"&gt;  
            &lt;value&gt;root&lt;/value&gt;  
        &lt;/property&gt;  
        &lt;property name="password"&gt;  
            &lt;value&gt;mysql&lt;/value&gt;  
        &lt;/property&gt;  
    &lt;/bean&gt;  
    &lt;!-- Spring iBatis Template --&gt;  
    &lt;bean id="sqlMapClient" class="org.springframework.orm.ibatis.SqlMapClientFactoryBean"&gt;  
        &lt;property name="configLocation" value="SqlMapConfig.xml" /&gt;  
        &lt;property name="dataSource" ref="dataSource" /&gt;  
    &lt;/bean&gt;  
    &lt;bean id="transactionManager"  
        class="org.springframework.jdbc.datasource.DataSourceTransactionManager"&gt;  
        &lt;property name="dataSource"&gt;  
            &lt;ref local="dataSource" /&gt;  
        &lt;/property&gt;  
    &lt;/bean&gt;  
    &lt;bean id="userDAO" class="com.angi.ibatis.dao.UserDaoImpl"&gt;  
        &lt;property name="sqlMapClient"&gt;  
            &lt;ref bean="sqlMapClient" /&gt;  
        &lt;/property&gt;  
    &lt;/bean&gt;  
    &lt;bean id="userService" class="com.angi.ibatis.service.UserService"&gt;  
        &lt;property name="userDao"&gt;  
            &lt;ref bean="userDAO" /&gt;  
        &lt;/property&gt;  
    &lt;/bean&gt;  
    &lt;bean id="userServiceProxy"  
        class="org.springframework.transaction.interceptor.TransactionProxyFactoryBean"&gt;  
        &lt;property name="transactionManager"&gt;  
            &lt;ref bean="transactionManager" /&gt;  
        &lt;/property&gt;  
        &lt;property name="target"&gt;  
            &lt;ref local="userService" /&gt;  
        &lt;/property&gt;  
        &lt;property name="transactionAttributes"&gt;  
            &lt;props&gt;  
                &lt;!-- 这里的方法签名可以精确到方法, 先懒惰一下全配置上 --&gt;  
                &lt;prop key="\*"&gt;PROPAGATION\_REQUIRED&lt;/prop&gt;  
            &lt;/props&gt;  
        &lt;/property&gt;  
    &lt;/bean&gt;  
&lt;/beans&gt;

[![复制代码](http://common.cnblogs.com/images/copycode.gif)]()

方法 2、TransactionInterceptor

[![复制代码](http://common.cnblogs.com/images/copycode.gif)]()

&lt;?xml version="1.0" encoding="UTF-8"?&gt;  
&lt;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 "&gt;  
    &lt;!-- DataSource --&gt;  
    &lt;bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource"&gt;  
        &lt;property name="driverClassName"&gt;  
            &lt;value&gt;com.mysql.jdbc.Driver&lt;/value&gt;  
        &lt;/property&gt;  
        &lt;!--&lt;property name="defaultAutoCommit" value="false"/&gt;--&gt;  
        &lt;property name="url"&gt;  
            &lt;value&gt;jdbc:mysql://localhost/test&lt;/value&gt;  
        &lt;/property&gt;  
        &lt;property name="username"&gt;  
            &lt;value&gt;root&lt;/value&gt;  
        &lt;/property&gt;  
        &lt;property name="password"&gt;  
            &lt;value&gt;mysql&lt;/value&gt;  
        &lt;/property&gt;  
    &lt;/bean&gt;  
    &lt;!-- Spring iBatis Template --&gt;  
    &lt;bean id="sqlMapClient" class="org.springframework.orm.ibatis.SqlMapClientFactoryBean"&gt;  
        &lt;property name="configLocation" value="SqlMapConfig.xml" /&gt;  
        &lt;property name="dataSource" ref="dataSource" /&gt;  
    &lt;/bean&gt;  
    &lt;bean id="transactionManager"  
        class="org.springframework.jdbc.datasource.DataSourceTransactionManager"&gt;  
        &lt;property name="dataSource"&gt;  
            &lt;ref local="dataSource" /&gt;  
        &lt;/property&gt;  
    &lt;/bean&gt;  
    &lt;bean id="userDAO" class="com.angi.ibatis.dao.UserDaoImpl"&gt;  
        &lt;property name="sqlMapClient"&gt;  
            &lt;ref bean="sqlMapClient" /&gt;  
        &lt;/property&gt;  
    &lt;/bean&gt;  
    &lt;bean id="userService" class="com.angi.ibatis.service.UserService"&gt;  
        &lt;property name="userDao"&gt;  
            &lt;ref bean="userDAO" /&gt;  
        &lt;/property&gt;  
    &lt;/bean&gt;  
    &lt;bean  
        class="org.springframework.aop.framework.autoproxy.BeanNameAutoProxyCreator"&gt;  
        &lt;property name="beanNames"&gt;  
            &lt;list&gt;  
                &lt;value&gt;userService&lt;/value&gt;  
            &lt;/list&gt;  
        &lt;/property&gt;  
        &lt;property name="interceptorNames"&gt;  
            &lt;list&gt;  
                &lt;value&gt;transactionInterceptor&lt;/value&gt;  
            &lt;/list&gt;  
        &lt;/property&gt;  
    &lt;/bean&gt;  
    &lt;bean id="transactionInterceptor"  
        class="org.springframework.transaction.interceptor.TransactionInterceptor"&gt;  
        &lt;property name="transactionManager" ref="transactionManager" /&gt;  
        &lt;property name="transactionAttributes"&gt;  
            &lt;props&gt;  
                &lt;!-- 这里的方法签名可以精确到方法, 先懒惰一下全配置上 --&gt;  
                &lt;prop key="\*"&gt;PROPAGATION\_REQUIRED&lt;/prop&gt;  
            &lt;/props&gt;  
        &lt;/property&gt;  
    &lt;/bean&gt;  
&lt;/beans&gt;

[![复制代码](http://common.cnblogs.com/images/copycode.gif)]()

方法 3、AOP和TX配置

[![复制代码](http://common.cnblogs.com/images/copycode.gif)]()

&lt;?xml version="1.0" encoding="UTF-8"?&gt;  
&lt;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 "&gt;  
    &lt;!-- DataSource --&gt;  
    &lt;bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource"  
        destroy-method="close"&gt;  
        &lt;property name="driverClassName"&gt;  
            &lt;value&gt;com.mysql.jdbc.Driver&lt;/value&gt;  
        &lt;/property&gt;  
        &lt;property name="url"&gt;  
            &lt;value&gt;jdbc:mysql://localhost/test&lt;/value&gt;  
        &lt;/property&gt;  
        &lt;property name="username"&gt;  
            &lt;value&gt;root&lt;/value&gt;  
        &lt;/property&gt;  
        &lt;property name="password"&gt;  
            &lt;value&gt;mysql&lt;/value&gt;  
        &lt;/property&gt;  
    &lt;/bean&gt;  
    &lt;!-- Spring iBatis Template --&gt;  
    &lt;bean id="sqlMapClient" class="org.springframework.orm.ibatis.SqlMapClientFactoryBean"&gt;  
        &lt;property name="configLocation" value="SqlMapConfig.xml" /&gt;  
        &lt;property name="dataSource" ref="dataSource" /&gt;  
    &lt;/bean&gt;  
    &lt;bean id="transactionManager"  
        class="org.springframework.jdbc.datasource.DataSourceTransactionManager"&gt;  
        &lt;property name="dataSource" ref="dataSource" /&gt;  
    &lt;/bean&gt;  
    &lt;!-- 需要引入aop的命名空间 --&gt;  
    &lt;aop:config&gt;  
        &lt;!-- 切入点指明了在所有方法产生事务拦截操作 --&gt;  
        &lt;aop:pointcut id="serviceMethods"  
            expression="execution(* com.angi.ibatis.service.\*.\*(..))" /&gt;  
        &lt;!-- 定义了将采用何种拦截操作，这里引用到 txAdvice --&gt;  
        &lt;aop:advisor advice-ref="txAdvice" pointcut-ref="serviceMethods" /&gt;  
    &lt;/aop:config&gt;  
    &lt;!-- 需要引入tx的命名空间 --&gt;  
    &lt;!-- 这是事务通知操作，使用的事务管理器引用自 transactionManager --&gt;  
    &lt;tx:advice id="txAdvice" transaction-manager="transactionManager"&gt;  
        &lt;tx:attributes&gt;  
            &lt;!-- 指定哪些方法需要加入事务，这里懒惰一下全部加入，可以使用通配符来只加入需要的方法 --&gt;  
            &lt;tx:method name="\*" propagation="REQUIRED" /&gt;  
        &lt;/tx:attributes&gt;  
    &lt;/tx:advice&gt;  
    &lt;bean id="userDAO" class="com.angi.ibatis.dao.UserDaoImpl"&gt;  
        &lt;property name="sqlMapClient"&gt;  
            &lt;ref bean="sqlMapClient" /&gt;  
        &lt;/property&gt;  
    &lt;/bean&gt;  
    &lt;bean id="userService" class="com.angi.ibatis.service.UserService"&gt;  
        &lt;property name="userDao"&gt;  
            &lt;ref bean="userDAO" /&gt;  
        &lt;/property&gt;  
    &lt;/bean&gt;  
&lt;/beans&gt;

[![复制代码](http://common.cnblogs.com/images/copycode.gif)]()

方法 4、anotation

[![复制代码](http://common.cnblogs.com/images/copycode.gif)]()

&lt;?xml version="1.0" encoding="UTF-8"?&gt;  
&lt;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 "&gt;  
    &lt;!-- 需要引入tx的命名空间 --&gt;  
    &lt;tx:annotation-driven transaction-manager="transactionManager" /&gt;  
    &lt;!-- DataSource --&gt;  
    &lt;bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource"&gt;  
        &lt;property name="driverClassName"&gt;  
            &lt;value&gt;com.mysql.jdbc.Driver&lt;/value&gt;  
        &lt;/property&gt;  
        &lt;!--&lt;property name="defaultAutoCommit" value="false"/&gt;--&gt;  
        &lt;property name="url"&gt;  
            &lt;value&gt;jdbc:mysql://localhost/test&lt;/value&gt;  
        &lt;/property&gt;  
        &lt;property name="username"&gt;  
            &lt;value&gt;root&lt;/value&gt;  
        &lt;/property&gt;  
        &lt;property name="password"&gt;  
            &lt;value&gt;mysql&lt;/value&gt;  
        &lt;/property&gt;  
    &lt;/bean&gt;  
    &lt;!-- Spring iBatis Template --&gt;  
    &lt;bean id="sqlMapClient" class="org.springframework.orm.ibatis.SqlMapClientFactoryBean"&gt;  
        &lt;property name="configLocation" value="SqlMapConfig.xml" /&gt;  
        &lt;property name="dataSource" ref="dataSource" /&gt;  
    &lt;/bean&gt;  
    &lt;bean id="transactionManager"  
        class="org.springframework.jdbc.datasource.DataSourceTransactionManager"&gt;  
        &lt;property name="dataSource"&gt;  
            &lt;ref local="dataSource" /&gt;  
        &lt;/property&gt;  
    &lt;/bean&gt;  
    &lt;bean id="userDAO" class="com.angi.ibatis.dao.UserDaoImpl"&gt;  
        &lt;property name="sqlMapClient"&gt;  
            &lt;ref bean="sqlMapClient" /&gt;  
        &lt;/property&gt;  
    &lt;/bean&gt;  
    &lt;bean id="userService" class="com.angi.ibatis.service.UserService"&gt;  
        &lt;property name="userDao"&gt;  
            &lt;ref bean="userDAO" /&gt;  
        &lt;/property&gt;  
    &lt;/bean&gt;  
&lt;/beans&gt;

[![复制代码](http://common.cnblogs.com/images/copycode.gif)]()

Java代码：

[![复制代码](http://common.cnblogs.com/images/copycode.gif)]()

@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);

[![复制代码](http://common.cnblogs.com/images/copycode.gif)]()

    }