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

1. mysql配置

配置文件:datasources.xml

 <bean id="dataSource1" class="com.mysql.jdbc.jdbc2.optional.MysqlDataSource">
<property name="url" value="${db.mysql.url}" />
<property name="user" value="${db.mysql.username}" />
<property name="password" value="${db.mysql.password}" />
</bean>

2. postgresql(postgres)配置

 <bean id="dataSourcePostgresql1" class="org.postgresql.ds.PGSimpleDataSource">
<property name="url" value="${db.postgresql.url}" />
<property name="user" value="${db.postgresql.username}" />
<property name="password" value="${db.postgresql.password}" />
<property name="serverName" value="127.0.0.1" />
<property name="portNumber" value="5432" />
<property name="databaseName" value="test" />
</bean>

3. spring配置

 <bean id="multipleDataSource" class="org.bighead.common.util.MultipleDataSource">
<property name="defaultTargetDataSource" ref="dataSource1" />
<property name="targetDataSources">
<map key-type="java.lang.String">
<entry key="oracle1" value-ref="dataSource1" />
<entry key="postgresql1" value-ref="dataSourcePostgresql1" />
</map>
</property>
<property name="lenientFallback" value="true"/>
</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. 备注

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

最新文章

  1. Ansible Ubuntu 安装部署
  2. [LeetCode] Find the Difference
  3. git版本控制管理实践-4
  4. 右键添加 CMD 命令提示符
  5. SRETAN
  6. light oj 1236 分解质因数
  7. backbone 更新1.1.2
  8. 玩转Log4Net
  9. uva 10306
  10. iOS 捕获系统外异常
  11. Makefile中的变量和shell变量
  12. F和弦大横按
  13. 检测磁盘驱动的健康程度SMART
  14. DevExpress AspxGridView分页使用隐藏系统默认英文分页
  15. 用存储过程向数据库添加大量数据【mysql】
  16. eclipse编写连接MySQL的简单动态网页
  17. vue計算屬性
  18. RPC简介
  19. Chrome之控制台使用【转载】
  20. Flutter - Migrate to AndroidX

热门文章

  1. scala转换date提取年月日时分秒
  2. python判断两个文件是否相同
  3. Javascript基本类型回顾
  4. oracle 12C利用dbca建库13步
  5. visual studio git for coding
  6. 『Balancing Act 树的重心』
  7. springboot+springcloud集成jar
  8. Docker快速搭建LNMP环境
  9. [Redis] redis的设计与实现-对象系统
  10. Java 学习笔记 二维数组和对象数组