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="com.FrameWork.dynamicDataSource.CustomerContextHolder">
		<property name="map">
    			<ref bean="nameMap"></ref>
  		</property>
	</bean>

6.配置sessionFactory

<bean id="sessionFactory"
		class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
		<property name="dataSource">  <!-- 这里要注意-->
			<ref bean="dataSource" />
		</property>
		<property name="lobHandler" ref="lobHandler" />
		<property name="hibernateProperties">
			<props>
				<prop key="hibernate.dialect">
					org.hibernate.dialect.Oracle9Dialect
				</prop>

				<!-- 
					<prop key="hibernate.show_sql">true</prop>
					<prop key="hibernate.format_sql">true</prop>
				-->
			</props>

		</property>


		<!--Spring Blob数据类型支持--><!--
			<property name="lobHandler" ref="oracleLobHandler" />
		--><!--hbm.xml映射文件在此处注册-->
		<property name="mappingResources">
			<list>
				<!-- 库房管理  -->
				<value>
					com/czkj/event/wzgl/mapping/TWzDepot.hbm.xml
				</value>
				<value>
					com/czkj/event/wzgl/mapping/TWzCkgly.hbm.xml
				</value>
				....
			</list>
		</property>
	</bean>

7.spring 注入

<!-- 数据同步 -->
	<bean id="synCarImp" class="com.czkj.service.syn.SynCarImp">
		<property name="dynDataSource" ref="customerContextHolder"></property>
		<property name="idao" ref="idao"></property>
		<property name="ihibernatedao" ref="ihibernatedao"></property>
		<property name="sctx" ref="SystemContext"></property>
	</bean>

8.java代码调用  类中声明

// 配置多数据源操作  声明
	private CustomerContextHolder dynDataSource = null;
	public void setDynDataSource(CustomerContextHolder dynDataSource) {
		this.dynDataSource = dynDataSource;
	}
 

9.调用

public void test(){
		List list = dynDataSource.getMap().get(DataSourceMap.ds2).queryforlist("select * from FuncMain ");
		System.out.println(list);
	}

10.完成

相关推荐