多平台统一管理软件接口,如何实现多平台统一管理软件接口
520
2023-05-13
Spring MVC配置双数据源实现一个java项目同时连接两个数据库的方法
前言
本文主要介绍的是关于Spring MVC配置双数据源实现一个java项目同时连接两个数据库的方法,分享出来供大家参考学习,下面来看看详细的介绍:
实现方法:
数据源在配置文件中的配置
xmlns:xsi="http://w3.org/2001/XMLSchema-instance" xmlns:aop="http://springframework.org/schema/aop" xmlns:cache="http://springframework.org/schema/cache" xmlns:context="http://springframework.org/schema/context" xmlns:jdbc="http://springframework.org/schema/jdbc" xmlns:jee="http://springframework.org/schema/jee" xmlns:jms="http://springframework.org/schema/jms" xmlns:lang="http://springframework.org/schema/lang" xmlns:mvc="http://springframework.org/schema/mvc" xmlns:oxm="http://springframework.org/schema/oxm" xmlns:p="http://springframework.org/schema/p" xmlns:task="http://springframework.org/schema/task" xmlns:tx="http://springframework.org/schema/tx" xmlns:util="http://springframework.org/schema/util" xsi:schemaLocation="http://springframework.org/schema/beans http://springframework.org/schema/beans/spring-beans.xsd http://springframework.org/schema/aop http://springframework.org/schema/aop/spring-aop-3.1.xsd http://springframework.org/schema/cache http://springframework.org/schema/cache/spring-cache-3.1.xsd http://springframework.org/schema/context http://springframework.org/schema/context/spring-context-3.1.xsd http://springframework.org/schema/jdbc http://springframework.org/schema/jdbc/spring-jdbc-3.1.xsd http://springframework.org/schema/jee http://springframework.org/schema/jee/spring-jee-3.1.xsd http://springframework.org/schema/jms http://springframework.org/schema/jms/spring-jms-3.1.xsd http://springframework.org/schema/lang http://springframework.org/schema/lang/spring-lang-3.1.xsd http://springframework.org/schema/mvc http://springframework.org/schema/mvc/spring-mvc-3.1.xsd http://springframework.org/schema/oxm http://springframework.org/schema/oxm/spring-oxm-3.1.xsd http://springframework.org/schema/task http://springframework.org/schema/task/spring-task-3.1.xsd http://springframework.org/schema/tx http://springframework.org/schema/tx/spring-tx-3.1.xsd http://springframework.org/schema/util http://springframework.org/schema/util/spring-util-3.1.xsd"> destroy-method="close"> destroy-method="close">
xmlns:xsi="http://w3.org/2001/XMLSchema-instance" xmlns:aop="http://springframework.org/schema/aop"
xmlns:cache="http://springframework.org/schema/cache"
xmlns:context="http://springframework.org/schema/context"
xmlns:jdbc="http://springframework.org/schema/jdbc" xmlns:jee="http://springframework.org/schema/jee"
xmlns:jms="http://springframework.org/schema/jms" xmlns:lang="http://springframework.org/schema/lang"
xmlns:mvc="http://springframework.org/schema/mvc" xmlns:oxm="http://springframework.org/schema/oxm"
xmlns:p="http://springframework.org/schema/p" xmlns:task="http://springframework.org/schema/task"
xmlns:tx="http://springframework.org/schema/tx" xmlns:util="http://springframework.org/schema/util"
xsi:schemaLocation="http://springframework.org/schema/beans http://springframework.org/schema/beans/spring-beans.xsd
http://springframework.org/schema/aop http://springframework.org/schema/aop/spring-aop-3.1.xsd
http://springframework.org/schema/cache http://springframework.org/schema/cache/spring-cache-3.1.xsd
http://springframework.org/schema/context http://springframework.org/schema/context/spring-context-3.1.xsd
http://springframework.org/schema/jdbc http://springframework.org/schema/jdbc/spring-jdbc-3.1.xsd
http://springframework.org/schema/jee http://springframework.org/schema/jee/spring-jee-3.1.xsd
http://springframework.org/schema/jms http://springframework.org/schema/jms/spring-jms-3.1.xsd
http://springframework.org/schema/lang http://springframework.org/schema/lang/spring-lang-3.1.xsd
http://springframework.org/schema/mvc http://springframework.org/schema/mvc/spring-mvc-3.1.xsd
http://springframework.org/schema/oxm http://springframework.org/schema/oxm/spring-oxm-3.1.xsd
http://springframework.org/schema/task http://springframework.org/schema/task/spring-task-3.1.xsd
http://springframework.org/schema/tx http://springframework.org/schema/tx/spring-tx-3.1.xsd
http://springframework.org/schema/util http://springframework.org/schema/util/spring-util-3.1.xsd">
destroy-method="close">
destroy-method="close">
destroy-method="close">
destroy-method="close">
DynamicDataSource.class
package com.core;
import org.springframework.jdbc.datasource.lookup.AbstractRoutingDataSource;
public class DynamicDataSource extends AbstractRoutingDataSource{
@Override
protected Object determineCurrentLookupKey() {
return DatabaseContextHolder.getCustomerType();
}
}
DatabaseContextHolder.class设置数据源的类
package com.core;
public class DatabaseContextHolder {
private static final ThreadLocal
//设置要使用的数据源
public static void setCustomerType(String customerType) {
contextHolder.set(customerType);
}
//获取数据源
public static String getCustomerType() {
return contextHolder.get();
}
//清除数据源,使用默认的数据源
public static void clearCustomerType() {
contextHolder.remove();
}
}
DataSourceInterceptor.class
package com.core;
import org.aspectj.lang.JoinPoint;
import org.springframework.stereotype.Component;
@Component
public class DataSourceInterceptor {
//数据源1
public static final String SOURCE_PLAN = "dataSourceOne";
//数据源2
}
springMVC数据源
jdbc_driverClassName=com.microsoft.sqlserver.jdbc.SQLServerDriver
jdbc_username=**jdbc_password=**
dataSourceTwo=jdbc:sqlserver://115.29.***.*;DatabaseName=DB_Fund
Spring MVC会默认有一个数据源,当需要更换数据源时,要在调用事务之前配置
DataSourceContextHolder.setDbType(DataSourceType.SOURCE_FUND);//更换数据源
/**
* @ClassName: DataSourceContextHolder
* @Description: 数据库切换工具类
* @author: wzx
* @date: 2016-07-27 上午10:26:01
*/
public class DataSourceContextHolder {
private static final ThreadLocal
public static void setDbType(String dbType) {
contextHolder.set(dbType);
}
public static String getDbType() {
return ((String) contextHolder.get());
}
public static void clearDbType() {
contextHolder.remove();
}
}
总结
以上就是这篇文章的全部内容了,希望本文的内容对大家的学习或者工作能带来一定的帮助,如果有疑问大家可以留言交流,谢谢大家对我们的支持。
版权声明:本文内容由网络用户投稿,版权归原作者所有,本站不拥有其著作权,亦不承担相应法律责任。如果您发现本站中有涉嫌抄袭或描述失实的内容,请联系我们jiasou666@gmail.com 处理,核实后本网站将在24小时内删除侵权内容。
发表评论
暂时没有评论,来抢沙发吧~