0. properties ####################################mysql########################################### db.mysql.driverClassName=com.mysql.jdbc.Driver db.m ...
0. properties
####################################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=hp242g2View Code
1. mysql配置
配置文件:datasources.xml
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>View Code
2. postgresql(postgres)配置
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>View Code
3. spring配置
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>View Code
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. 備註
多資料庫連接池問題,後續更新