日期:2014-05-16 浏览次数:20390 次
1.使用Spring的配置文件完成多数据库连接:
1.1 applicationContext.xml [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" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd"> <!-- 知识管理数据库 --> <bean id="km_source" class="org.apache.commons.dbcp.BasicDataSource"> <property name="driverClassName" value="com.microsoft.sqlserver.jdbc.SQLServerDriver"></property> <property name="url" value="jdbc:sqlserver://localhost:1433;databaseName=docmanager"></property> <property name="username" value="sa"></property> <property name="password" value="123"></property> </bean> <!-- 框架数据库 --> <bean id="ma_source" class="org.apache.commons.dbcp.BasicDataSource"> <property name="driverClassName" value="com.microsoft.sqlserver.jdbc.SQLServerDriver"></property> <property name="url" value="jdbc:sqlserver://localhost:1433;databaseName=framemanager"></property> <property name="username" value="sa"></property> <property name="password" value="123"></property> <property name="maxActive" value="100000"></property> <property name="maxIdle" value="20"></property> <property name="maxWait" value="1000"></property> </bean> <!-- 用户组织数据库 --> <bean id="um_source" class="org.apache.commons.dbcp.BasicDataSource"> <property name="driverClassName" value="com.microsoft.sqlserver.jdbc.SQLServerDriver"></property> <property name="url" value="jdbc:sqlserver://localhost:1433;databaseName=usermanager"></property> <property name="username" value="sa"></property> <property name="password" value="123"></property> </bean> <!-- 工作流数据库 --> <bean id="wfe_source" class="org.apache.commons.dbcp.BasicDataSource"> <property name="driverClassName" value="com.microsoft.sqlserver.jdbc.SQLServerDriver"></property> <property name="url" value="jdbc:sqlserver://localhost:1433;databaseName=wfemanager"></property> <property name="username" value="sa"></property> <property name="password" value="123"></property> </bean> <bean id="myDataSource" class="com.smartcom.util.jdbcUtil.DynamicDataSource"> <property name="targetDataSources"> <map> <entry key="ma_source" value-ref="ma_source"/> <entry key="um_source" value-ref="um_source"/> </map> </property> <property name="defaultTargetDataSource" ref="km_source"/> </bean> <bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate" abstract="false" lazy-init="false" autowire="default"> <property name="dataSource" ref="myDataSource"/> </bean> <import resource="config/ma/beans_pojo.xml"/> <import resource="config/ma/beans_dao.xml"/> <import resource="config/ma/beans_service.xml"/> <import resource="config/ma/beans_action.xml"/> <import resource="config/ma/beans_ajax.xml"/> </beans>
?
1.2 JdbcContext.java [建立一个获得和设置上下文的类]
package com.smartcom.util.jdbcUtil; /** * 建立一个获得和设置上下文的类 * @author Liyongbin * 此类正在使用 */ public class JdbcContext { private static final ThreadLocal<String> contextHolder = new ThreadLocal<String>(); /** * XXXXX */ public static void setJdbcType(String jdbcType) { contextHolder.set(jdbcType); } /** * XXXXX */ public static String getJdbcType()