spring3:多数据源配置使用
2024-08-31 03:38:17
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. 备注
多数据库连接池问题,后续更新
最新文章
- Ansible Ubuntu 安装部署
- [LeetCode] Find the Difference
- git版本控制管理实践-4
- 右键添加 CMD 命令提示符
- SRETAN
- light oj 1236 分解质因数
- backbone 更新1.1.2
- 玩转Log4Net
- uva 10306
- iOS 捕获系统外异常
- Makefile中的变量和shell变量
- F和弦大横按
- 检测磁盘驱动的健康程度SMART
- DevExpress AspxGridView分页使用隐藏系统默认英文分页
- 用存储过程向数据库添加大量数据【mysql】
- eclipse编写连接MySQL的简单动态网页
- vue計算屬性
- RPC简介
- Chrome之控制台使用【转载】
- Flutter - Migrate to AndroidX