spring3:多数据源配置使用

2018-06-17 20:01:28来源:未知 阅读 ()

新老客户大回馈,云服务器低至5折

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=hp242g2
View 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. 备注

多数据库连接池问题,后续更新

 

标签:

版权申明:本站文章部分自网络,如有侵权,请联系:west999com@outlook.com
特别注意:本站所有转载文章言论不代表本站观点,本站所提供的摄影照片,插画,设计作品,如需使用,请与原作者联系,版权归原作者所有

上一篇:PostgreSQL9.6主从配置

下一篇:spark快速上手