在开发当中,有时候需要的数据源不止一个,所以就需要配置数据源,也就是说需要不同数据库里的数据。

废话少说,转入正题!

一、框架SSI(Struts+Spring+Ibatis)

二、主要的就是Spring的配置文件,其他的都是次要的

三、我是将Spring的配置文件分开了,你只要参考我的改一下就行了

四、spring的配置文件

1、applicationContext_bean.xml

<?xmlversion="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-3.0.xsd
       http://www.springframework.org/schema/tx
      http://www.springframework.org/schema/tx/spring-tx-3.0.xsd
       http://www.springframework.org/schema/aop
      http://www.springframework.org/schema/aop/spring-aop-3.0.xsd"> 
 <!-- -->    
      //.红色部分为重点,这里是核心,配置两个dateSource
<beanid="dataSource"class="org.springframework.jndi.JndiObjectFactoryBean">
           <propertyname="jndiName">
vspn53JNDI</value>
           </property>
      </bean>
      
      <bean id="dataSource2"class="org.springframework.jndi.JndiObjectFactoryBean">
           <propertyname="jndiName">
sxJNDI</value>
           </property>
      </bean>
 
<!--class="org.apache.commons.dbcp.BasicDataSource"
      <bean id="dataSource"
           class="org.springframework.jdbc.datasource.DriverManagerDataSource">
           <propertyname="driverClassName">
                 <value>oracle.jdbc.driver.OracleDriver
                 </value>
           </property>
           <property name="url">
                 <value>jdbc:oracle:thin:@140.100.100.218:1521:oracle
                 </value>
           </property>
           <propertyname="username">
                 <value>vspn54</value>
           </property>
           <propertyname="password">
                 <value>vspn54</value>
           </property>
      </bean>
-->
sqlMapClient"class="org.springframework.orm.ibatis.SqlMapClientFactoryBean">
           <propertyname="configLocation">
classpath:sqlmap/sqlmap_base.xml</value>
           </property>
           <propertyname="dataSource">
                 <reflocal="dataSource" />
           </property>
      </bean>
      
sqlMapClient2"class="org.springframework.orm.ibatis.SqlMapClientFactoryBean">
           <propertyname="configLocation">
classpath:sqlmap/sqlmap_base.xml</value>
           </property>
           <propertyname="dataSource">
                 <reflocal="dataSource2" />
           </property>
      </bean>
      
      <bean id="transactionManager"
           class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
           <propertyname="dataSource" ref="dataSource"></property>
      </bean>
      <!-- 支持@Transactional 标记 -->
      <tx:annotation-driventransaction-manager="transactionManager" />
      <!-- 声明式事务管理-->
    <beanid="baseTransactionProxy" 
        class="org.springframework.transaction.interceptor.TransactionProxyFactoryBean"abstract="true">
        <propertyname="transactionManager"ref="transactionManager"></property>
        <propertyname="transactionAttributes">
            <props>
                <propkey="insert*">PROPAGATION_REQUIRED</prop>            
                <propkey="update*">PROPAGATION_REQUIRED</prop>           
                <propkey="delete*">PROPAGATION_REQUIRED</prop>
                <propkey="get*">PROPAGATION_REQUIRED,readOnly</prop>
            </props>
        </property>
    </bean>
      
</beans>

2、applicationContext_dao.xml

<?xmlversion="1.0" encoding="UTF-8"?>
<beansxmlns="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-3.0.xsd
       http://www.springframework.org/schema/tx
       http://www.springframework.org/schema/tx/spring-tx-3.0.xsd
      http://www.springframework.org/schema/aop 
      http://www.springframework.org/schema/aop/spring-aop-3.0.xsd">
      <!-- 用户登陆 -->
      <bean id="loginDao"class="com.vstsoft.da.system.dao.imp.LoginDaoImpl">
sqlMapClient" />
      </bean>
      
      <!-- 代码 -->
      <bean id="ans01Dao"class="com.vstsoft.da.system.dao.imp.Ans01DaoImpl">
sqlMapClient"/>
      </bean>
      <bean id="ans03Dao"class="com.vstsoft.da.system.dao.imp.Ans03DaoImpl">
           <propertyname="sqlMapClient" ref="sqlMapClient" />
      </bean>
      
      <bean id="jianDao"class="com.vstsoft.da.jian.dao.impl.JianDaoImpl">
sqlMapClient"/>
      </bean>
      //这里调用的是dateSource2的数据
<beanid="zujianDao"class="com.vstsoft.da.jian.dao.impl.ZuJianDaoImpl">
sqlMapClient2"/>
      </bean>
      <bean id="juanDao"class="com.vstsoft.da.juan.dao.impl.JuanDaoImpl">
sqlMapClient"/>
      </bean> 
      <!-- 代码 -->
      <bean id="heDao"class="com.vstsoft.da.he.dao.impl.HeDaoImpl">
sqlMapClient"/>
      </bean>
      <bean id="jianSuoDao"class="com.vstsoft.da.jiansuo.dao.impl.JianSuoDaoImpl">
sqlMapClient"/>
      </bean>
      
</beans>

//这里意思很明显,调用base中声明的dateSource,在这里区分那个dao需要调用那个数据源,在这里注意看黄色标记的部分

3、applicationContext_manage.xml

<?xmlversion="1.0" encoding="UTF-8"?>
<beansxmlns="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-3.0.xsd
       http://www.springframework.org/schema/tx
      http://www.springframework.org/schema/tx/spring-tx-3.0.xsd
      http://www.springframework.org/schema/aop 
       http://www.springframework.org/schema/aop/spring-aop-3.0.xsd">
 
      <!-- 用户登陆 -->
      <bean id="loginManage"class="com.vstsoft.da.system.manage.imp.LoginManageImpl">
           <propertyname="loginDao">
                 <refbean="loginDao" />
           </property>
      </bean>    
      <!-- 代码 -->
      <bean id="ans01Manage" class="com.vstsoft.da.system.manage.imp.Ans01ManageImpl">
           <propertyname="ans01Dao">
                 <refbean="ans01Dao" />
           </property>
      </bean>    
      <!-- 代码 -->
      <bean id="ans03Manage"class="com.vstsoft.da.system.manage.imp.Ans03ManageImpl">
           <property name="ans03Dao">
                 <refbean="ans03Dao" />
           </property>
      </bean>    
      <!-- 代码 -->
      <bean id="jianManage"parent="baseTransactionProxy">
           <propertyname="target">
                 <beanclass="com.vstsoft.da.jian.manage.impl.JianManageImpl">
                      <propertyname="jianDao">
                      <ref bean="jianDao"/>
                      </property>
                      <propertyname="zujianDao">
                      <refbean="zujianDao" />
                      </property>
                      <propertyname="ans03Dao">
                            <refbean="ans03Dao"/>
                      </property>
                      <propertyname="juanDao">
                            <refbean="juanDao" />
                      </property>
                      <propertyname="heDao">
                            <refbean="heDao" />
                      </property>
                 </bean>
           </property>
      </bean>
      <!-- 代码 -->
      <bean id="juanManage"parent="baseTransactionProxy">
           <propertyname="target">
                 <beanclass="com.vstsoft.da.juan.manage.impl.JuanManageImpl">
                      <propertyname="jianDao">
                            <refbean="jianDao" />
                      </property>
                      <propertyname="juanDao">
                            <refbean="juanDao" />
                      </property>
                      <propertyname="heDao">
                            <refbean="heDao" />
                      </property>
                      <propertyname="ans03Dao">
                            <refbean="ans03Dao"/>
                      </property>
                 </bean>
           </property>
      </bean>
      <!-- 代码 -->
      <bean id="heManage"parent="baseTransactionProxy">
           <propertyname="target">
                 <beanclass="com.vstsoft.da.he.manage.impl.HeManageImpl">
                      <propertyname="heDao">
                      <refbean="heDao" />
                      </property>
                      <propertyname="juanDao">
                            <refbean="juanDao" />
                      </property>
                      <propertyname="jianDao">
                            <refbean="jianDao" />
                      </property>
                 </bean>
           </property>
      </bean>
      <bean id="jianSuoManage"class="com.vstsoft.da.jiansuo.manage.impl.JianSuoManageImpl">
           <propertyname="jianSuoDao">
                 <ref bean="jianSuoDao"/>
           </property>
      </bean>
</beans>

//这个配置文件没有什么特别需要介绍的,就是给manage配置需要调用的dao

五、sqlMap的配置文件

      1、sqlmap_base.xml

<?xmlversion="1.0"encoding="UTF-8"?>
 
<!DOCTYPEsqlMapConfigPUBLIC"-//ibatis.apache.org//DTDSQL Map Config 2.0//EN"
"http://ibatis.apache.org/dtd/sql-map-config-2.dtd">
 
<sqlMapConfig>
//这里没什么特别,就是加载一些需要用的sqlmap
<!-- 按文件名的顺序依次添加 -->
<sqlMapresource="sqlmap/sqlmap_bean.xml"/>
<sqlMapresource="sqlmap/sqlmap_system.xml"/>
<sqlMapresource="sqlmap/sqlmap_jian.xml"/>
<sqlMapresource="sqlmap/sqlmap_juan.xml"/>
<sqlMapresource="sqlmap/sqlmap_he.xml"/>
<sqlMapresource="sqlmap/sqlmap_jiansuo.xml"/>
<sqlMapresource="sqlmap/sqlmap_jian_zujian.xml"/>
</sqlMapConfig>

2、其他的sqlmap都没什么特别,重点说一下最后一个sqlmap_jian_zujian.xml

<?xmlversion="1.0"encoding="UTF-8"?>
 
<!DOCTYPEsqlMapPUBLIC "-//ibatis.apache.org//DTDSQL Map 2.0//EN"
   "http://ibatis.apache.org/dtd/sql-map-2.dtd">
 
<sqlMap>
//这里的resultMap是指,将对方的库里的数据转换成我们库里对应的表,意思也就是说
   比如对方有个表将and00,我们需要将对方的数据转换成我们的数据,我们用and01接收查出的数据
<resultMapid="and01Result"class="and01">
   
//这下面的操作就是将对方数据库表中的字段,转换成我们库里对应表的字段,这个你懂得!
<!-- 区县编码 -->
<resultcolumn="QXBM"property="cnd112"jdbcType="VARCHAR"/>
     
<!-- 单位代码 -->
<resultcolumn="DWDM"property="cnd102"jdbcType="VARCHAR"/>
     
<!-- 单位名称 -->
<resultcolumn="DWMC"property="cnd103"jdbcType="VARCHAR"/>
     
<!-- 单位简称 -->
<resultcolumn="DWJC"property="cnd104"jdbcType="VARCHAR"/>
     
<!-- 社会保险登记证号 -->
<resultcolumn="DJZH"property="cnd105"jdbcType="VARCHAR"/>
     
<!-- 身份证号 -->
<resultcolumn="BZHM"property="cnd106"jdbcType="VARCHAR"/>
     
<!-- 电脑序号 -->
<resultcolumn="BXH"property="cnd116"jdbcType="VARCHAR"/>
     
<!-- 姓名 -->
<resultcolumn="XM"property="cnd107"jdbcType="VARCHAR"/>
     
<!-- 经办人姓名 -->
<resultcolumn="USERNAME"property="cnd113"jdbcType="VARCHAR"/>
     
<!-- 办理日期 -->
<resultcolumn="BLRQ"property="cnd114"jdbcType="DATE"/>
     
</resultMap>
 
<!-- 查询四险中的数据 -->
<selectid="sx"resultMap="and01Result">
     select * from sysdalog
</select>
</sqlMap>

六、将这些东西配置好,这个加载多个数据源的操作就完成了,Action中的代码就不贴了,很简单,用dao直接调用就行了,在配置文件中都已经配置好了,所以不需要费多大劲!

七、好了,这就是整个配置过程,希望对你有所帮助,谢谢!