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

Spring 配置Oracle和MySql双数据源
Spring DataSource配置:
<?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.0.xsd
			http://www.springframework.org/schema/aop 
			http://www.springframework.org/schema/aop/spring-aop-2.0.xsd
			http://www.springframework.org/schema/tx
            http://www.springframework.org/schema/tx/spring-tx-2.0.xsd">


<!--
	############################################################################
	#############################   datasource配置      ############################
	############################################################################
-->
	<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource"
		destroy-method="close">
		<property name="driverClassName">
			<value>${database.driver}</value>
		</property>
		<property name="url">
			<value>${database.url}</value>
		</property>
		<property name="username">
			<value>${database.username}</value>
		</property>
		<property name="password">
			<value>${database.password}</value>
		</property>
		<property name="maxActive">
			<value>10</value>
		</property>
	</bean>
	
	<bean id="dataSourceMySql" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">
        <!-- Connection Info -->
        <property name="driverClassName" value="${database.mysql.driver}" />
        <property name="url" value="${database.mysql.url}" />
        <property name="username" value="${database.mysql.username}" />
        <property name="password" value="${database.mysql.password}" />

        <!-- Connection Pooling Info -->
        <property name="initialSize" value="5" />
        <property name="maxActive" value="100" />
        <property name="maxIdle" value="30" />
        <property name="maxWait" value="1000" />
        <property name="poolPreparedStatements" value="true" />
        <property name="defaultAutoCommit" value="false" />
    </bean>

		
	<bean id="daoConfigBase" abstract="true">
		<property name="sqlMapClient">
			<ref bean="sqlMapClient" />
		</property>
	</bean>
	
	<bean id="daoConfigBaseMySql" abstract="true">
		<property name="sqlMapClient">
			<ref bean="sqlMapClientMySql" />
		</property>
	</bean>
	
	<bean id="sqlMapClient" class="org.springframework.orm.ibatis.SqlMapClientFactoryBean">
		<property name="configLocation"	value="classpath:/config/dao/sqlmap-config.xml" />
		<property name="dataSource" ref="dataSource" />
	</bean>
	
	<bean id="sqlMapClientMySql" class="org.springframework.orm.ibatis.SqlMapClientFactoryBean">
		<property name="configLocation"	value="classpath:/config/dao/sqlmap-config.xml" />
		<property name="dataSource" ref="dataSourceMySql" />
	</bean>
</beans>

DAO继承配置:
<bean id="ipDAO"		class="org.zju.cdpf.service.biz.mysql.dao.IbatisIpDao"
		scope="prototype" parent="daoConfigBaseMySql" />
<bean id="ipDAO"		class="org.zju.cdpf.service.biz.dao.area.ibatis.IbatisipDAO"
		scope="prototype" parent="daoConfigBase" />