日期:2014-05-16  浏览次数:20576 次

SSH配置多数据源(JDBC连接)

0.把用到的 JDBC 包导进来

1.创建类DataSourceMap?

?

public class DataSourceMap {  
	public static final String ds1= "ds1";
	
	public static final String ds2= "ds2";
}
?

?

2.创建类CustomerContextHolder?

?

?

import java.util.Map;
import com.FrameWork.db.interf.IDAO;
public class CustomerContextHolder {
	private    Map<String,IDAO> map =null;

	public Map<String, IDAO> getMap() {
		return map;
	}

	public void setMap(Map<String, IDAO> map) {
		this.map = map;
	}
}
?

?

3.配置连接池

?

<!-- 程序自身连接池 -->
	<bean id="dataSource"
		class="org.apache.commons.dbcp.BasicDataSource"
		destroy-method="close">
		<property name="driverClassName"
			value="oracle.jdbc.driver.OracleDriver" />
		<property name="url"
			value="jdbc:oracle:thin:@192.168.1.188:1521:orcl" />
		<property name="username" value="ddd" />
		<property name="password" value="xxxx" />
		<property name="maxActive" value="30" />
		<property name="maxIdle" value="30" />
		<property name="maxWait" value="1000" />
		<property name="defaultAutoCommit" value="true" />
		<property name="removeAbandoned" value="true" />
		<property name="removeAbandonedTimeout" value="60" />
	</bean>
	<!-- sql server 2000 car数据库 -->
	<bean id="dataSource1" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">
		<property name="driverClassName" value="com.microsoft.jdbc.sqlserver.SQLServerDriver"/>
		<property name="url" value="jdbc:microsoft:sqlserver://192.168.1.109:1433;DatabaseName=car"/>
        <property name="username" value="sa"/>
        <property name="password" value="sa"/>
        <property name="maxActive" value="30"/>
        <property name="maxIdle" value="30"/>
        <property name="maxWait" value="1000"/>
        <property name="defaultAutoCommit" value="true"/>
        <property name="removeAbandoned" value="true"/>
        <property name="removeAbandonedTimeout" value="60"/>
	</bean>
	<!-- sql server 2000 gps数据库 -->
	<bean id="dataSource2" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">
		<property name="driverClassName" value="com.microsoft.jdbc.sqlserver.SQLServerDriver"/>
		<property name="url" value="jdbc:microsoft:sqlserver://192.168.1.109:1433;DatabaseName=gps"/>
        <property name="username" value="sa"/>
        <property name="password" value="sa"/>
        <property name="maxActive" value="30"/>
        <property name="maxIdle" value="30"/>
        <property name="maxWait" value="1000"/>
        <property name="defaultAutoCommit" value="true"/>
        <property name="removeAbandoned" value="true"/>
        <property name="removeAbandonedTimeout" value="60"/>
	</bean>
?

?

4.设置数据库连接池

?

?

<!-- Generic IDao - can be used when doing standard CRUD-->
	<bean id="idao" class="com.FrameWork.db.impl.DAOImpl">
		<property name="dataSource" ref="dataSource" />
	</bean>
	
	 
	<bean id="idao1" class="com.FrameWork.db.impl.DAOImpl">
		<property name="dataSource" ref="dataSource1" />
	</bean>
	 
	<bean id="idao2" class="com.FrameWork.db.impl.DAOImpl">
		<property name="dataSource" ref="dataSource2" />
	</bean>

?

5.分配连接池

?

?

<!-- Construct Map -->
	<bean id="nameMap" class="java.util.HashMap">
		<constructor-arg>
			<map>
				<entry key="ds1" value-ref="idao1"/>
				<entry key="ds2" value-ref="idao2"/>
			</map>
		</constructor-arg>
	</bean>

	<bean id="customerContextHolder" class="