Home  >  Article  >  Java  >  Analysis of usage examples of Spring MVC Mybatis multiple data sources

Analysis of usage examples of Spring MVC Mybatis multiple data sources

高洛峰
高洛峰Original
2017-01-24 10:28:231683browse

The project needs to obtain data from other websites. Because it is a temporary requirement, I did not expect to need multiple data sources when I started the project

So I searched on Baidu and found that I only need to modify the Spring applicationContext.xml file and write three tool classes It can be perfectly realized

applicationContext.xml

<!-- 多数据源配置 -->
 <bean id="ds1" class="org.apache.commons.dbcp.BasicDataSource">
  <property name="driverClassName" value="${jdbc.driverClassName}" />
  <property name="url" value="${jdbc.url}" />
  <property name="username" value="${jdbc.username}" />
  <property name="password" value="" />
 </bean>
 <bean id="ds2" class="org.apache.commons.dbcp.BasicDataSource">
  <property name="driverClassName" value="" />
  <property name="url" value="" />
  <property name="username" value="" />
  <property name="password" value="" />
 </bean>
 <!-- 动态配置数据源 -->
 <bean id="dataSource" class="com.test.utils.DynamicDataSource">//这里是你项目里DynamicDataSource.java的路径
  <property name="targetDataSources">
   <map key-type="java.lang.String">
    <entry value-ref="ds_admin" key="ds1"></entry>
    <entry value-ref="ds_partner" key="ds2"></entry>
   </map>
  </property>
  <!-- 默认使用ds1的数据源 -->
  <property name="defaultTargetDataSource" ref="ds_admin"></property> 
 </bean>

DataSourceContextHolder.java

public class DataSourceContextHolder {
 private static final ThreadLocal<String> contextHolder = new ThreadLocal<String>();
 public static void setDbType(String dbType) {
  contextHolder.set(dbType);
 }
 public static String getDbType() {
  return ((String) contextHolder.get());
 }
 public static void clearDbType() {
  contextHolder.remove();
 }
}

DataSourceType.java (set static variables)

public class DataSourceType {
 // 默认数据库
 public static final String SOURCE_ADMIN = "ds1";
 // 第二个数据库,在applicationContext.xml里的id
 public static final String SOURCE_PARTNER = "ds2";
}

The next one is the key DynamicDataSource.java which inherits AbstractRo utingDataSource The abstract method determineCurrentLookupKey is the core of the route that implements the data source. Override this method here.

import org.springframework.jdbc.datasource.lookup.AbstractRoutingDataSource;
public class DynamicDataSource extends AbstractRoutingDataSource {
 @Override
 protected Object determineCurrentLookupKey() {
  return DataSourceContextHolder.getDbType();
 }
}

The above is the usage example analysis of Spring MVC Mybatis multiple data sources introduced by the editor. I hope it will be helpful to you. If you have any questions, please leave me a message and the editor will reply to you in time. . I would also like to thank you all for your support of the PHP Chinese website!

For more articles related to the use case analysis of Spring MVC Mybatis multiple data sources, please pay attention to the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn