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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值