注册 登录  
 加关注
   显示下一条  |  关闭
温馨提示!由于新浪微博认证机制调整,您的新浪微博帐号绑定已过期,请重新绑定!立即重新绑定新浪微博》  |  关闭

我的博客

等闲识得东风面,万紫千红总是春!

 
 
 

日志

 
 

Spring+ibatis事务处理配置  

2010-09-27 23:29:30|  分类: J2EE 高级 |  标签: |举报 |字号 订阅

  下载LOFTER 我的照片书  |

第一次接触Spring和Ibatis,在事务处理方面遇到了很大的麻烦,用了一整天的时间上网查看资料,虽然找到不少,但没有一个能调试出来,主要原因还是所给的代码不够详尽。不过在大家的共同努力之下,问题解决了,在这里不作任何保留,拿出来分享。

applicationContext.xml

view plaincopy to clipboardprint?
<?xml version="1.0" encoding="UTF-8"?> 
 
<beans xmlns="http://www.springframework.org/schema/beans
 
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance
 
    xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.0.xsd"> 
 
 
 
    <bean id="dataSource" 
 
        class="org.springframework.jdbc.datasource.DriverManagerDataSource"> 
 
        <property name="driverClassName"> 
 
            <value>oracle.jdbc.driver.OracleDriver</value> 
 
        </property> 
 
        <property name="url"> 
 
            <value>jdbc:oracle:thin:@172.17.7.191:1521:STRUTS</value> 
 
        </property> 
 
        <property name="username"> 
 
            <value>STRUTS</value> 
 
        </property> 
 
        <property name="password"> 
 
            <value>STRUTS</value> 
 
        </property> 
 
 
 
    </bean> 
 
 
 
 
 
    <bean id="insertDao" class="flex.samples.spring.dao.InsertCl"> 
 
        <property name="sqlMapClient"> 
 
            <ref local="sqlMapClient" /> 
 
        </property> 
 
    </bean> 
 
 
 
    <bean name="insertAction" 
 
        class="flex.samples.spring.action.InsertAction"> 
 
        <property name="insertService"> 
 
            <ref bean="insertService"/> 
 
        </property> 
 
    </bean> 
 
 
 
    <bean id="insertService" 
 
        class="flex.samples.spring.dao.InsertService"> 
 
        <property name="insertDao"> 
 
            <ref local="insertDao" /> 
 
        </property> 
 
    </bean> 
 
 
 
    <bean id="sqlMapClient" 
 
        class="org.springframework.orm.ibatis.SqlMapClientFactoryBean"> 
 
        <property name="configLocation"> 
 
            <value> 
 
                WEB-INF/SqlMapConfig.xml  
 
            </value> 
 
        </property> 
 
        <property name="dataSource"> 
 
            <ref bean="dataSource" /> 
 
        </property> 
 
    </bean> 
 
 
 
 
 
    <bean name="logger"      
 
        class="flex.samples.spring.interceptor.LoggingInterceptor"> 
 
    </bean> 
 
    <bean name="beanNameAutoProxy" 
 
        class="org.springframework.aop.framework.autoproxy.BeanNameAutoProxyCreator" /> 
 
 
 
    <bean id="transactionProxy" parent="beanNameAutoProxy"> 
 
        <property name="beanNames"> 
 
            <list> 
 
                <value>insertService</value> 
 
            </list> 
 
        </property> 
 
        <property name="interceptorNames"> 
 
            <value>transactionInterceptor</value> 
 
        </property> 
 
    </bean> 
 
 
 
    <bean id="logginProxy" parent="beanNameAutoProxy"> 
 
        <property name="beanNames"> 
 
            <list> 
 
                <value>insertDao</value> 
 
            </list> 
 
        </property> 
 
        <property name="interceptorNames"> 
 
            <value>logger</value> 
 
        </property> 
 
    </bean> 
 
 
 
    <bean id="transactionManager" 
 
        class="org.springframework.jdbc.datasource.DataSourceTransactionManager"> 
 
        <property name="dataSource"> 
 
            <ref bean="dataSource" /> 
 
        </property> 
 
    </bean> 
 
 
 
    <bean id="transactionInterceptor" class="org.springframework.transaction.interceptor.TransactionInterceptor"> 
 
       <property name="transactionManager"> 
 
           <ref local="transactionManager" /> 
 
       </property> 
 
 
 
       <property name="transactionAttributes"> 
 
           <props> 
 
              <prop key="*">PROPAGATION_REQUIRED,-Exception</prop> 
 
           </props> 
 
       </property> 
 
    </bean> 
 
</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"

 xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.0.xsd">

 

 <bean id="dataSource"

  class="org.springframework.jdbc.datasource.DriverManagerDataSource">

  <property name="driverClassName">

   <value>oracle.jdbc.driver.OracleDriver</value>

  </property>

  <property name="url">

   <value>jdbc:oracle:thin:@172.17.7.191:1521:STRUTS</value>

  </property>

  <property name="username">

   <value>STRUTS</value>

  </property>

  <property name="password">

   <value>STRUTS</value>

  </property>

 

 </bean>

 

 

 <bean id="insertDao" class="flex.samples.spring.dao.InsertCl">

  <property name="sqlMapClient">

   <ref local="sqlMapClient" />

  </property>

 </bean>

 

 <bean name="insertAction"

  class="flex.samples.spring.action.InsertAction">

  <property name="insertService">

         <ref bean="insertService"/>

      </property>

 </bean>

 

 <bean id="insertService"

  class="flex.samples.spring.dao.InsertService">

  <property name="insertDao">

   <ref local="insertDao" />

  </property>

 </bean>

 

 <bean id="sqlMapClient"

  class="org.springframework.orm.ibatis.SqlMapClientFactoryBean">

  <property name="configLocation">

   <value>

    WEB-INF/SqlMapConfig.xml

   </value>

  </property>

  <property name="dataSource">

   <ref bean="dataSource" />

  </property>

 </bean>

 

 

 <bean name="logger"   

     class="flex.samples.spring.interceptor.LoggingInterceptor">

   </bean>

 <bean name="beanNameAutoProxy"

  class="org.springframework.aop.framework.autoproxy.BeanNameAutoProxyCreator" />

 

 <bean id="transactionProxy" parent="beanNameAutoProxy">

  <property name="beanNames">

   <list>

    <value>insertService</value>

   </list>

  </property>

  <property name="interceptorNames">

   <value>transactionInterceptor</value>

  </property>

 </bean>

 

 <bean id="logginProxy" parent="beanNameAutoProxy">

  <property name="beanNames">

   <list>

    <value>insertDao</value>

   </list>

  </property>

  <property name="interceptorNames">

   <value>logger</value>

  </property>

 </bean>

 

 <bean id="transactionManager"

  class="org.springframework.jdbc.datasource.DataSourceTransactionManager">

  <property name="dataSource">

   <ref bean="dataSource" />

  </property>

 </bean>

 

    <bean id="transactionInterceptor" class="org.springframework.transaction.interceptor.TransactionInterceptor">

       <property name="transactionManager">

           <ref local="transactionManager" />

       </property>

 

       <property name="transactionAttributes">

           <props>

              <prop key="*">PROPAGATION_REQUIRED,-Exception</prop>

           </props>

       </property>

    </bean>

</beans>InsertAction.java

view plaincopy to clipboardprint?
package flex.samples.spring.action;  
 
 
 
//import java.sql.Timestamp;  
 
//import java.text.SimpleDateFormat;  
 
 
 
 
 
import java.util.List;  
 
 
 
import flex.samples.spring.dao.InsertService;  
 
import flex.samples.spring.bean.UserBean;  
 
import flex.samples.spring.interceptor.LoggingInterceptor;  
 
 
 
import org.apache.commons.logging.*;  
 
 
 
 
 
public class InsertAction {  
 
    //##############################################  
 
      
 
    private Log log =LogFactory.getLog(LoggingInterceptor.class);  
 
    private InsertService insertService;  
 
 
 
 
 
      
 
    public InsertService getInsertService() {  
 
        return this.insertService;  
 
    }  
 
    public void setInsertService(InsertService insertService) {  
 
        this.insertService = insertService;  
 
    }  
 
      
 
    public void insertData(UserBean user,List edu,List pro_list) throws Exception{  
 
 
 
        try{  
 
            insertService.insertUserInfo(user, edu, pro_list);  
 
        }catch (Exception e){  
 
            log.info(e.toString());  
 
            throw e;  
 
        }  
 
          
 
          
 
    }  
 

package flex.samples.spring.action;

 

//import java.sql.Timestamp;

//import java.text.SimpleDateFormat;

 

 

import java.util.List;

 

import flex.samples.spring.dao.InsertService;

import flex.samples.spring.bean.UserBean;

import flex.samples.spring.interceptor.LoggingInterceptor;

 

import org.apache.commons.logging.*;

 

 

public class InsertAction {

 //##############################################

 

 private Log log =LogFactory.getLog(LoggingInterceptor.class);

 private InsertService insertService;

 

 

 

 public InsertService getInsertService() {

  return this.insertService;

 }

 public void setInsertService(InsertService insertService) {

  this.insertService = insertService;

 }

 

 public void insertData(UserBean user,List edu,List pro_list) throws Exception{

 

  try{

   insertService.insertUserInfo(user, edu, pro_list);

  }catch (Exception e){

   log.info(e.toString());

   throw e;

  }

  

  

 }

}

InsertCl.java

view plaincopy to clipboardprint?
package flex.samples.spring.dao;  
 
 
 
import java.util.List;  
 
import org.springframework.orm.ibatis.support.SqlMapClientDaoSupport;  
 
import flex.samples.spring.bean.UserBean;  
 
 
 
public class InsertCl extends SqlMapClientDaoSupport implements InsertIn{  
 
       
 
    public void deleteUserInfo(UserBean user){  
 
        this.getSqlMapClientTemplate().update("del_data",user);  
 
    }  
 
    public void insertBaseInfo(UserBean user){  
 
        this.getSqlMapClientTemplate().update("insUser",user);  
 
    }  
 
    public void insertDBInfo(UserBean user){  
 
         this.getSqlMapClientTemplate().insert("insDB_Access",user);  
 
         this.getSqlMapClientTemplate().insert("insDB_SQLserver",user);  
 
         this.getSqlMapClientTemplate().insert("insDB_ORACLE",user);  
 
         this.getSqlMapClientTemplate().insert("insDB_Informix",user);  
 
         this.getSqlMapClientTemplate().insert("insDB_DB2",user);  
 
         this.getSqlMapClientTemplate().insert("insDB_MySQL",user);  
 
         this.getSqlMapClientTemplate().insert("insDB_otherdb",user);  
 
    }  
 
    public void insertOSInfo(UserBean user){  
 
         this.getSqlMapClientTemplate().insert("insOS_Unix",user);  
 
         this.getSqlMapClientTemplate().insert("insOS_Linux",user);  
 
         this.getSqlMapClientTemplate().insert("insOS_MVS",user);  
 
         this.getSqlMapClientTemplate().insert("insOS_WinXP",user);  
 
         this.getSqlMapClientTemplate().insert("insOS_WinNT/2000",user);  
 
         this.getSqlMapClientTemplate().insert("insOS_AS/400",user);  
 
         this.getSqlMapClientTemplate().insert("insOS_otheros",user);  
 
    }  
 
    public void insertLangInfo(UserBean user){  
 
         this.getSqlMapClientTemplate().insert("insLANG_C/C++/VC",user);  
 
         this.getSqlMapClientTemplate().insert("insLANG_.NET",user);  
 
         this.getSqlMapClientTemplate().insert("insLANG_COBOL",user);  
 
         this.getSqlMapClientTemplate().insert("insLANG_VB",user);  
 
         this.getSqlMapClientTemplate().insert("insLANG_JAVA/JSP",user);  
 
         this.getSqlMapClientTemplate().insert("insLANG_Apache",user);  
 
         this.getSqlMapClientTemplate().insert("insLANG_Weblogic",user);  
 
         this.getSqlMapClientTemplate().insert("insLANG_Framework",user);  
 
         this.getSqlMapClientTemplate().insert("insLANG_UML",user);  
 
    }  
 
    public void insertEduInfo(List edu){  
 
         for(int i=0;i<edu.size();i++){  
 
             this.getSqlMapClientTemplate().insert("insEdu",edu.get(i));  
 
         }  
 
    }  
 
    public void insertProInfo(List pro_list){  
 
         for(int i=0;i<pro_list.size();i++){  
 
             this.getSqlMapClientTemplate().insert("insPro",pro_list.get(i));  
 
         }  
 
    }  
 
      
 
 
 

package flex.samples.spring.dao;

 

import java.util.List;

import org.springframework.orm.ibatis.support.SqlMapClientDaoSupport;

import flex.samples.spring.bean.UserBean;

 

public class InsertCl extends SqlMapClientDaoSupport implements InsertIn{

 

 public void deleteUserInfo(UserBean user){

  this.getSqlMapClientTemplate().update("del_data",user);

 }

 public void insertBaseInfo(UserBean user){

  this.getSqlMapClientTemplate().update("insUser",user);

 }

 public void insertDBInfo(UserBean user){

   this.getSqlMapClientTemplate().insert("insDB_Access",user);

   this.getSqlMapClientTemplate().insert("insDB_SQLserver",user);

   this.getSqlMapClientTemplate().insert("insDB_ORACLE",user);

   this.getSqlMapClientTemplate().insert("insDB_Informix",user);

   this.getSqlMapClientTemplate().insert("insDB_DB2",user);

   this.getSqlMapClientTemplate().insert("insDB_MySQL",user);

   this.getSqlMapClientTemplate().insert("insDB_otherdb",user);

 }

 public void insertOSInfo(UserBean user){

   this.getSqlMapClientTemplate().insert("insOS_Unix",user);

   this.getSqlMapClientTemplate().insert("insOS_Linux",user);

   this.getSqlMapClientTemplate().insert("insOS_MVS",user);

   this.getSqlMapClientTemplate().insert("insOS_WinXP",user);

   this.getSqlMapClientTemplate().insert("insOS_WinNT/2000",user);

   this.getSqlMapClientTemplate().insert("insOS_AS/400",user);

   this.getSqlMapClientTemplate().insert("insOS_otheros",user);

 }

 public void insertLangInfo(UserBean user){

   this.getSqlMapClientTemplate().insert("insLANG_C/C++/VC",user);

   this.getSqlMapClientTemplate().insert("insLANG_.NET",user);

   this.getSqlMapClientTemplate().insert("insLANG_COBOL",user);

   this.getSqlMapClientTemplate().insert("insLANG_VB",user);

   this.getSqlMapClientTemplate().insert("insLANG_JAVA/JSP",user);

   this.getSqlMapClientTemplate().insert("insLANG_Apache",user);

   this.getSqlMapClientTemplate().insert("insLANG_Weblogic",user);

   this.getSqlMapClientTemplate().insert("insLANG_Framework",user);

   this.getSqlMapClientTemplate().insert("insLANG_UML",user);

 }

 public void insertEduInfo(List edu){

   for(int i=0;i<edu.size();i++){

    this.getSqlMapClientTemplate().insert("insEdu",edu.get(i));

   }

 }

 public void insertProInfo(List pro_list){

   for(int i=0;i<pro_list.size();i++){

    this.getSqlMapClientTemplate().insert("insPro",pro_list.get(i));

   }

 }

 

 

}

SqlMapConfig.xml

view plaincopy to clipboardprint?
<?xml version="1.0" encoding="UTF-8"?> 
 
<!DOCTYPE sqlMapConfig        
 
    PUBLIC "-//ibatis.apache.org//DTD SQL Map Config 2.0//EN"        
 
    "http://ibatis.apache.org/dtd/sql-map-config-2.dtd"> 
 
<sqlMapConfig> 
 
       <sqlMap resource="flex/samples/spring/xml/test.xml"/> 
 
</sqlMapConfig> 


本文来自CSDN博客,转载请标明出处:http://blog.csdn.net/pentium1512/archive/2008/06/13/2543575.aspx#

  评论这张
 
阅读(1855)| 评论(0)
推荐 转载

历史上的今天

评论

<#--最新日志,群博日志--> <#--推荐日志--> <#--引用记录--> <#--博主推荐--> <#--随机阅读--> <#--首页推荐--> <#--历史上的今天--> <#--被推荐日志--> <#--上一篇,下一篇--> <#-- 热度 --> <#-- 网易新闻广告 --> <#--右边模块结构--> <#--评论模块结构--> <#--引用模块结构--> <#--博主发起的投票-->
 
 
 
 
 
 
 
 
 
 
 
 
 
 

页脚

网易公司版权所有 ©1997-2017