文章目录
SSM配置多个数据源
在项目中,难免会用到多个数据源,此篇文章以两个数据源为例
一、思路及实践步骤
1.确定需要使用的数据源
在datasource.properties文件配置数据源信息
jdbc.driverClassName = com.mysqL.jdbc.Driver
jdbc.urL = jdbc:mysqL://localhost:3306/one
jdbc.username = test1
jdbc.password = 123
jdbc.driverClassName2 = com.mysqL.jdbc.Driver
jdbc.urL2 = jdbc:mysqL://localhost:3307/one
jdbc.username2 = test2
jdbc.password2 = 123
2. SSM项目需要在XML里面进行相关配置
代码如下(示例):
<!--第一个数据库-->
<bean id="One_dataSource" class= "org.apache.commons.dbcp.BasicDataSource" destroy -method="close">
<property name= "driverClassName" value=" ${jdbc.driverCLassName}" />
<property name="urL" value="${jdbc.urL}" />
<property name="username" value="${jdbc.username}" />
<property name="password" value="${jdbc.password}" />
<property name= "maxActive" value= "30"/>
<property name="maxIdle" value="20" />
<property name="maxWait" value="60000" />
</bean>
<!--第二个数据库-->
<bean id="Two_dataSource" class= "org.apache.common.dbcp.BasicDataSource" destroy -method="close">
<property name= "driverClassName" value=" ${jdbc.driverCLassName2}" />
<property name="urL" value="${jdbc.urL2}" />
<property name="username" value="${jdbc.username2}" />
<property name="password" value="${jdbc.password2}" />
<property name= "maxActive" value= "30"/>
<property name="maxIdle" value="20" />
<property name="maxWait" value="60000" />>
</bean>
3.对两个数据源进行相关配置
<!--配置指定的类-->
<bean id="dataSource" class= "com.yjy.util.DynamicDataSource">
<property name ="targetDataSources">
<map key-type="java.Lang.String">
<--指定LookupKey和与之对应的数据源,这里的key可以自行定义,要切换数据库的时候以key为标识,不要写错- ->
<entry key= "One_dataSource" value-ref= "One_dataSource "></entry>
<entry key= "Two_dataSource" value-ref= "Two_dataSource "></ entry>
</map>
</property>
<--这里可以指定默认的数据源->
<property name= "defauttTargetDataSource" ref= "One_dataSource "></property>
</bean>
4.程序中如何调用所需要的数据库
在SSM框架中,我们在applicationContext.xml配置文件中添加数据源就可以实现数据库增删改查,但是只能连接一个数据库,这个时候我们就要从spring提供的源码下手看看有没有相关数据源切换的方法,找到关键源码 AbstractRoutingDataSource类,该类就相当于一个dataSource的调度者,用于根据key值来进行切换对应的dataSource。
AbstractRoutingDataSource类:
@Override
public Connection getConnection() throws SQLException {
return determineTargetDataSource().getConnection();
}
@Override
public Connection getConnection(String username, String password) throws SQLException {
return determineTargetDataSource().getConnection(username, password);
}
/**
* Retrieve the current target DataSource. Determines the
* {@link #determineCurrentLookupKey() current lookup key}, performs
* a lookup in the {@link #setTargetDataSources targetDataSources} map,
* falls back to the specified
* {@link #setDefaultTargetDataSource default target DataSource} if necessary.
* @see #determineCurrentLookupKey()
*/
protected DataSource determineTargetDataSource() {
Assert.notNull(this.resolvedDataSources, "DataSource router not initialized");
Object lookupKey = determineCurrentLookupKey();
DataSource dataSource = this.resolvedDataSources.get(lookupKey);
if (dataSource == null && (this.lenientFallback || lookupKey == null)) {
dataSource = this.resolvedDefaultDataSource;
}
if (dataSource == null) {
throw new IllegalStateException("Cannot determine target DataSource for lookup key [" + lookupKey + "]");
}
return dataSource;
}
/**
* Determine the current lookup key. This will typically be
* implemented to check a thread-bound transaction context.
* <p>Allows for arbitrary keys. The returned key needs
* to match the stored lookup key type, as resolved by the
* {@link #resolveSpecifiedLookupKey} method.
*/
protected abstract Object determineCurrentLookupKey();
可以看出方法getConnection()调用的 determineTargetDataSource则是关键方法,这个方法返回了具体使用的是哪个数据库;而 determineCurrentLookupKey( ) 方法来返回当前数据源的key值。
将返回的key值在resolvedDataSources这个map中找到对应的value(当前使用的数据源)。
源码:
@Override
public void afterPropertiesSet() {
if (this.targetDataSources == null) {
throw new IllegalArgumentException("Property 'targetDataSources' is required");
}
this.resolvedDataSources = new HashMap<Object, DataSource>(this.targetDataSources.size());
for (Map.Entry<Object, Object> entry : this.targetDataSources.entrySet()) {
Object lookupKey = resolveSpecifiedLookupKey(entry.getKey());
DataSource dataSource = resolveSpecifiedDataSource(entry.getValue());
this.resolvedDataSources.put(lookupKey, dataSource);
}
if (this.defaultTargetDataSource != null) {
this.resolvedDefaultDataSource = resolveSpecifiedDataSource(this.defaultTargetDataSource);
}
}
这个方法是通过targetDataSources对resolvedDataSources进行赋值的。targetDataSources我们可以用过配置文件进行配置,这样就可以设置当前使用哪个数据库了。
4.1新建DynamicDataSource类
我们先要重写上面的determineCurrentLookupKey方法,我们新建一个创建一个DynamicDataSource的类,用来获取自定义获取数据源的标识(和当前线程中使用的数据源):
import org.springframework.jdbc.datasource.lookup.AbstractRoutingDataSource;
public class DynamicDataSource extends AbstractRoutingDataSource {
@Override
protected Object determineCurrentLookupKey() {
// 从自定义的位置获取数据源标识
return DynamicDataSourceHolder.getDataSource();
}
}
4.2 创建DynamicDataSourceHolder类
切换数据源
public class DynamicDataSourceHolder{
private static final ThreadLocal<String> THREAD_DATA_SOURCE = new ThreadLocal();
public static String getDataSource(){
return (String)THREAD_DATA_SOURCE.get();
}
public static void setDataSource(String dataSource){
THREAD_DATA_SOURCE.set(dataSource);
}
public static void clearDataSource(){
THREAD_DATA_SOURCE.remove();
}
}
以上,需要的配置完成。
4.3 调用
public void data() {
JSONObject jsonObject = new JSONObject();
List<Map<String ,Object>> list = testmapper.selectAllColor();
jsonObject.put("AllColor",list);
//切换数据源
DynamicDataSourceHolder.setDataSource( "Two_dataSource") ;
testmapper.addAllColor();
}
注意:因为配置了默认数据源,所以默认为第一个数据源,需要切换数据源的时候再进行切换。
建议:如果切换了数据源,需要在继续操作默认数据源的时候,建议切换回默认数据源。
本文参考 http://t.zoukankan.com/MagicAsa-p-11089329.html,感谢!
欢迎讨论,欢迎指正。
PS:第一篇文章,小激动哇~ 希望可以帮助到有需要的人~