spring3:多数据源配置使用

本文详细介绍了如何在Spring框架中配置MySQL与PostgreSQL两种数据库的数据源,并实现动态切换。通过XML配置文件设置不同数据库的连接参数,并利用自定义类继承Spring的AbstractRoutingDataSource来实现数据源的动态选择。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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. 备注

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

 

转载于:https://www.cnblogs.com/niejianqiang/p/7849833.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值