spring3:多数据源配置使用
0. properties
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
####################################mysql########################################### db.mysql.driverClassName=com.mysql.jdbc.Driver db.mysql.url=jdbc:mysql://127.0.0.1:3306/test?useUnicode=true&characterEncoding=UTF-8 db.mysql.username=test db.mysql.password=test ####################################postgresql########################################### db.postgresql.driverClassName=org.postgresql.Driver db.postgresql.url=jdbc:postgresql://127.0.0.1:5432/test db.postgresql.username=postgres db.postgresql.password=hp242g2
1. mysql配置
配置文件:datasources.xml
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 <bean id="dataSource1" class="com.mysql.jdbc.jdbc2.optional.MysqlDataSource"> 2 <property name="url" value="${db.mysql.url}" /> 3 <property name="user" value="${db.mysql.username}" /> 4 <property name="password" value="${db.mysql.password}" /> 5 </bean>
2. postgresql(postgres)配置
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 <bean id="dataSourcePostgresql1" class="org.postgresql.ds.PGSimpleDataSource"> 2 <property name="url" value="${db.postgresql.url}" /> 3 <property name="user" value="${db.postgresql.username}" /> 4 <property name="password" value="${db.postgresql.password}" /> 5 <property name="serverName" value="127.0.0.1" /> 6 <property name="portNumber" value="5432" /> 7 <property name="databaseName" value="test" /> 8 </bean>
3. spring配置
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 <bean id="multipleDataSource" class="org.bighead.common.util.MultipleDataSource"> 2 <property name="defaultTargetDataSource" ref="dataSource1" /> 3 <property name="targetDataSources"> 4 <map key-type="java.lang.String"> 5 <entry key="oracle1" value-ref="dataSource1" /> 6 <entry key="postgresql1" value-ref="dataSourcePostgresql1" /> 7 </map> 8 </property> 9 <property name="lenientFallback" value="true"/> 10 </bean>
4. org.springframework.jdbc.datasource.lookup.AbstractRoutingDataSource继承
package org.bighead.common.util; import org.springframework.jdbc.datasource.lookup.AbstractRoutingDataSource; public class MultipleDataSource extends AbstractRoutingDataSource{ private static final ThreadLocal<String> threadLocal = new InheritableThreadLocal<String>(); @Override protected Object determineCurrentLookupKey() { return threadLocal.get(); } public static void setDataSourceKey(String dataSource) { threadLocal.set(dataSource); } }
5. 应用
package org.bighead.test; import java.sql.Connection; import java.sql.SQLException; import org.bighead.common.util.MultipleDataSource; import org.springframework.context.support.AbstractApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; public class TestTest { public static void main(String[] args) { String[] configs = {"config/spring-application-datasources.xml"}; AbstractApplicationContext appCont = new ClassPathXmlApplicationContext(configs); MultipleDataSource multipleDataSource = (MultipleDataSource) appCont.getBean("multipleDataSource"); try { MultipleDataSource.setDataSourceKey("postgresql1"); Connection connection = multipleDataSource.getConnection(); System.out.println(connection); } catch (SQLException e) { e.printStackTrace(); } } }
6. 备注
多数据库连接池问题,后续更新