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

ibatis如何配置多个数据源
1.applicationContext.xml
<!-- 定义数据源dataSource -->
	<bean id="dataSource"
		class="org.apache.commons.dbcp.BasicDataSource">
		<property name="driverClassName"
			value="ianywhere.ml.jdbcodbc.jdbc3.IDriver">
		</property>
		<property name="url"
			value="jdbc:odbc:driver=SQL Anywhere 11;eng=zyx;links=tcpip{host=10.144.99.46;port=2998};">
		</property>
		<property name="username" value="zyx"></property>
		<property name="password" value="zyx"></property>
		<property name="initialSize" value="1"></property>
		<property name="maxActive" value="500"></property>
		<property name="maxIdle" value="5"></property>
		<property name="minIdle" value="1"></property>
	</bean>

	<!-- 定义数据源Oracle -->
	<bean id="dataSourceOracle"	class="org.apache.commons.dbcp.BasicDataSource">
		<property name="driverClassName" value="oracle.jdbc.driver.OracleDriver"></property>
		<property name="url" value="jdbc:oracle:thin:@10.144.99.114:1521/sanexcfg"></property>
		<property name="username" value="sanex"></property>
		<property name="password" value="sanex"></property>
		<property name="initialSize" value="1"></property>
		<property name="maxActive" value="500"></property>
		<property name="maxIdle" value="5"></property>
		<property name="minIdle" value="1"></property>
	</bean>

	<!-- 定义数据源sqlserver dataSource -->
	<bean id="dataSourceSqlServer"
		class="org.apache.commons.dbcp.BasicDataSource">

		<property name="driverClassName"
			value="com.microsoft.jdbc.sqlserver.SQLServerDriver">
		</property>
		<property name="url"
			value="jdbc:microsoft:sqlserver://10.71.158.50:1433;DatabaseName=Mon;">
		</property>
		<property name="username" value="sa"></property>
		<property name="password" value="sa"></property>

		<property name="initialSize" value="1"></property>
		<property name="maxActive" value="500"></property>
		<property name="maxIdle" value="5"></property>
		<property name="minIdle" value="1"></property>
	</bean>
	<!-- 配置 ibatis 的sqlMapClient -->
	<bean id="sqlMapClient"
		class="org.springframework.orm.ibatis.SqlMapClientFactoryBean">
		<property name="configLocation"
			value="/WEB-INF/sqlMapConfig.xml" />
		<property name="dataSource" ref="dataSource" />		
	</bean>
	<bean id="sqlMapClientOrac"
		class="org.springframework.orm.ibatis.SqlMapClientFactoryBean">
		<property name="configLocation"
			value="/WEB-INF/sqlMapConfig.xml" />
		<property name="dataSource" ref="dataSourceOracle" />		
	</bean>


2.sqlMapConfig.xml文件里面什么都不设置,仅放自定义的查询xml文件

3.代码中用的是spring的注释,Resource里面的name要对应applicationContext.xml里面bean的id,它就可以去找到对应的bean从而引用到你想要操作的数据源
@Resource(name = "sqlMapClient")
	private SqlMapClient sqlMapClient;
	
	@Resource(name = "sqlMapClientOrac")
	private SqlMapClient sqlMapClientOrac;

	public void executeSp(Map map) throws Exception{			
		sqlMapClient.queryForList("userpercept.userAnalyse.addUsefulProcedure", map);
	}

	public List<Cell> getCellList() throws Exception{
		return sqlMapClientOrac.queryForList("userpercept.userAnalyse.getCellList");
	}
	
	public PlaceCell getCell(Long id) throws Exception {
		PlaceCell cell = (PlaceCell) sqlMapClient
			.queryForObject("userpercept.userAnalyse.selectCell",id);
		return cell;
	}