欧阳良才

不是别人,就是我阳良才
随笔 - 13, 文章 - 8, 评论 - 2, 引用 - 0
数据加载中……

SSi(spring+struts_ibatis)多数据库连接解决方案

1。创建配置文件 jdbc.properties

jdbc.driverClassName=com.mysql.jdbc.Driver
jdbc.url
=jdbc:mysql://192.168.99.100/infodata?useUnicode=true&characterEncoding=utf-8&zeroDateTimeBehavior=convertToNull&transformedBitIsBoolean=true
jdbc.username=root
jdbc.password
=root
jdbc2.driverClassName
=com.mysql.jdbc.Driver
jdbc2.url
=jdbc:mysql://192.168.99.101/data1?useUnicode=true&characterEncoding=utf-8
jdbc2.username=root
jdbc2.password
=root
jdbc3.driverClassName
=com.mysql.jdbc.Driver
jdbc3.url
=jdbc:mysql://192.168.99.102/data2?useUnicode=true&characterEncoding=utf-8
jdbc3.username=root
jdbc3.password
=root 

2。在spring配置文件中写入

<?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"
 
 xsi:schemaLocation
="http://www.springframework.org/schema/beans 
   http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
   http://www.springframework.org/schema/context/spring-context-2.5.xsd
        http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.5.xsd
           http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd">
<bean id="propertyConfigurer" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="locations">
    
<list>
      
<value>classpath:jdbc.properties</value>
    
</list>
</property>
</bean>
 
<!-- =========================transactionManager========================= -->
<bean id="autoProxyCreator" class="org.springframework.aop.framework.autoproxy.BeanNameAutoProxyCreator">
<property name="beanNames">
    
<value>dao*</value>
</property>
<property name="interceptorNames">
    
<list>
      
<value>transactionInterceptor</value>
       
<value>transactionInterceptor2</value>
    
</list>
</property>
</bean>
<!-- ========================= dataSource1========================= -->
<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">
   
<property name="driverClassName" value="${jdbc.driverClassName}"/>
   
<property name="url" value="${jdbc.url}&amp;characterEncoding=utf-8"/>
   
<property name="username" value="${jdbc.username}"/>
   
<property name="password" value="${jdbc.password}"/>
</bean>
       
<bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
   
<property name="dataSource" ref="dataSource"/>
</bean>
      
<bean id="transactionInterceptor" class="org.springframework.transaction.interceptor.TransactionInterceptor">
   
<property name="transactionManager" ref="transactionManager"/>
                
<property name="transactionAttributes">
                
<props>
                
<prop key="insert*">PROPAGATION_REQUIRED</prop>
                
<prop key="delete*">PROPAGATION_REQUIRED</prop>
                
<prop key="update*">PROPAGATION_REQUIRED</prop>
                
<prop key="get*">PROPAGATION_REQUIRED</prop>
                 
</props>
                
</property>
</bean>
<bean id="sqlMapClient" class="org.springframework.orm.ibatis.SqlMapClientFactoryBean">
   
<property name="configLocation">
   
<value>classpath:SqlMapConfig.xml</value>
   
</property>
   
<property name="dataSource" ref="dataSource"/>
</bean>
<!-- ========================= dataSource2========================= -->
   
<bean id="dataSource2" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">
   
<property name="driverClassName" value="${jdbc2.driverClassName}"/>
   
<property name="url" value="${jdbc2.url}&amp;characterEncoding=utf-8"/>
   
<property name="username" value="${jdbc2.username}"/>
   
<property name="password" value="${jdbc2.password}"/>
</bean>
    
<bean id="transactionManager2" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
   
<property name="dataSource" ref="dataSource2"/>
</bean>
<bean id="transactionInterceptor2" class="org.springframework.transaction.interceptor.TransactionInterceptor">
   
<property name="transactionManager" ref="transactionManager2"/>
                
<property name="transactionAttributes">
                
<props>
                
<prop key="insert*">PROPAGATION_REQUIRED</prop>
                
<prop key="delete*">PROPAGATION_REQUIRED</prop>
                
<prop key="update*">PROPAGATION_REQUIRED</prop>
                
<prop key="get*">PROPAGATION_REQUIRED</prop>
                 
</props>
                
</property>
</bean>
   
<bean id="sqlMapClient2" class="org.springframework.orm.ibatis.SqlMapClientFactoryBean">
   
<property name="configLocation">
   
<value>classpath:SqlMapConfig2.xml</value>
   
</property>
   
<property name="dataSource" ref="dataSource2"/>
</bean>
<!-- ========================= dataSource3========================= -->
   
<bean id="dataSource3" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">
   
<property name="driverClassName" value="${jdbc3.driverClassName}"/>
   
<property name="url" value="${jdbc3.url}&amp;characterEncoding=utf-8"/>
   
<property name="username" value="${jdbc3.username}"/>
   
<property name="password" value="${jdbc3.password}"/>
</bean>
    
<bean id="transactionManager3" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
   
<property name="dataSource" ref="dataSource3"/>
</bean>
<bean id="transactionInterceptor3" class="org.springframework.transaction.interceptor.TransactionInterceptor">
   
<property name="transactionManager" ref="transactionManager3"/>
                
<property name="transactionAttributes">
                
<props>
                
<prop key="insert*">PROPAGATION_REQUIRED</prop>
                
<prop key="delete*">PROPAGATION_REQUIRED</prop>
                
<prop key="update*">PROPAGATION_REQUIRED</prop>
                
<prop key="get*">PROPAGATION_REQUIRED</prop>
                 
</props>
                
</property>
</bean>
   
<bean id="sqlMapClient3" class="org.springframework.orm.ibatis.SqlMapClientFactoryBean">
   
<property name="configLocation">
   
<value>classpath:SqlMapConfig3.xml</value>
   
</property>
   
<property name="dataSource" ref="dataSource3"/>
</bean>

      
 
</beans>

 3。导入两个jar包:

c3p0-0.9.1.2.jar
commons-pool.jar
commons-dbcp.jar

此文章来自 wing123 详情http://wing123.iteye.com/blog/1350194

posted on 2012-04-07 15:52 欧阳良才 阅读(1612) 评论(0)  编辑  收藏


只有注册用户登录后才能发表评论。


网站导航: