设为首页 收藏本站
查看: 2410|回复: 0

[经验分享] Spring MVC+MyBatis框架使用多个数据源以及之间的切换

[复制链接]

尚未签到

发表于 2016-11-28 08:58:49 | 显示全部楼层 |阅读模式
至于怎么搭建Spring MVC+MyBatis框架,我这里就不详细说了,可以参考:http://xieke90.iyunv.com/blog/2241054
一、同时使用多个数据源
   以下实例演示同时使用两个数据源:
   <1>、applicationContext.xml

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:p="http://www.springframework.org/schema/p"
xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-3.0.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-3.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-2.5.xsd"
default-init-method="init">   
<!-- 引入jdbc配置文件 -->
<context:property-placeholder location="classpath:jdbc.properties" />
<!--创建jdbc数据源 -->
<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">
<property name="driverClassName" value="${driver}" />
<property name="url" value="${url}" />
<property name="username" value="${username}" />
<property name="password" value="${password}" />
</bean>
<bean id="dataSource2" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">
<property name="driverClassName" value="${driver}" />
<property name="url" value="${url2}" />
<property name="username" value="${username2}" />
<property name="password" value="${password2}" />
</bean>
<!-- 创建SqlSessionFactory,同时指定数据源 -->
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<property name="dataSource" ref="dataSource" />
<!-- 配置sql映射文件所在位置  注意:默认与mapper类位置相同   -->
<property name="mapperLocations" value="classpath:sqlmap/car/*.xml" />
</bean>
<bean id="sqlSessionFactory2" class="org.mybatis.spring.SqlSessionFactoryBean">
<property name="dataSource" ref="dataSource2" />
<property name="mapperLocations" value="classpath:sqlmap/order/*.xml" />
</bean>
<!-- Mapper接口所在包名,Spring会自动查找其下的Mapper -->
<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<property name="basePackage" value="com.xieke.test.mapper.car" />
<property name="sqlSessionFactoryBeanName" value="sqlSessionFactory" />
<!-- 默认情况下会自动注入id=sqlSessionFactory的bean,也可以按上述方式指定sqlSessionFactory -->
</bean>
<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<property name="basePackage" value="com.xieke.test.mapper.order" />
<property name="sqlSessionFactoryBeanName" value="sqlSessionFactory2" />
</bean>
</beans>
   <2>、jdbc.properties

driver=com.mysql.jdbc.Driver
url=jdbc:mysql://localhost:3306/test
username=root
password=999999
url2=jdbc:mysql://localhost:3306/test2
username2=root
password2=999999
   <3>、以上是主要的配置,其它配置跟配置Spring MVC是一样的,详细情况请阅读下面的源代码。
 
二、数据源之间的切换
   以下演示两个数据源之间的切换:
   <1>、applicationContext.xml

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:p="http://www.springframework.org/schema/p"
xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-3.0.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-3.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-2.5.xsd"
default-init-method="init">   
<!-- 引入jdbc配置文件 -->
<context:property-placeholder location="classpath:jdbc.properties" />
<!--创建jdbc数据源 -->
<bean id="dataSourceA" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">
<property name="driverClassName" value="${driver}" />
<property name="url" value="${a.url}" />
<property name="username" value="${a.username}" />
<property name="password" value="${a.password}" />
</bean>
<bean id="dataSourceB" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">
<property name="driverClassName" value="${driver}" />
<property name="url" value="${b.url}" />
<property name="username" value="${b.username}" />
<property name="password" value="${b.password}" />
</bean>
<!-- 配置dataSource管理key和value的对应,默认选择dataSourceB -->
<bean id="dataSource" class="com.xieke.test.util.DynamicDataSource">
<!-- 通过key-value的形式来关联数据源 -->
<property name="targetDataSources">
<map key-type="java.lang.String">
<entry value-ref="dataSourceA" key="dataSourceA"></entry>
<entry value-ref="dataSourceB" key="dataSourceB"></entry>
</map>
</property>
<property name="defaultTargetDataSource" ref="dataSourceB" >
</property>
</bean>
<!-- 创建SqlSessionFactory,同时指定数据源 -->
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<property name="dataSource" ref="dataSource" />
<!-- 配置sql映射文件所在位置  注意:默认与mapper类位置相同   -->
<property name="mapperLocations" value="classpath:sqlmap/**/*.xml" />
</bean>
<!-- Mapper接口所在包名,Spring会自动查找其下的Mapper -->
<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<property name="basePackage" value="com.xieke.test.mapper" />
<property name="sqlSessionFactoryBeanName" value="sqlSessionFactory" />
<!-- 默认情况下会自动注入id=sqlSessionFactory的bean,也可以按上述方式指定sqlSessionFactory -->
</bean>
</beans>
   <2>、jdbc.properties

driver=com.mysql.jdbc.Driver
a.url=jdbc:mysql://localhost:3306/test
a.username=root
a.password=999999
b.url=jdbc:mysql://localhost:3306/test2
b.username=root
b.password=999999
   <3>、数据源管理类:DynamicDataSource,需要继承AbstractRoutingDataSource。

package com.xieke.test.util;
import java.sql.SQLFeatureNotSupportedException;
import java.util.logging.Logger;
import org.springframework.jdbc.datasource.lookup.AbstractRoutingDataSource;
public class DynamicDataSource extends AbstractRoutingDataSource
{
public static final String DATA_SOURCE_A = "dataSourceA";
public static final String DATA_SOURCE_B = "dataSourceB";
private static final ThreadLocal<String> contextHolder = new ThreadLocal<String>();
public static void setCustomerType(String customerType)
{
contextHolder.set(customerType);
}
public static String getCustomerType()
{
return contextHolder.get();
}
public static void clearCustomerType()
{
contextHolder.remove();
}
@Override
protected Object determineCurrentLookupKey()
{
return getCustomerType();
}
@Override
public Logger getParentLogger() throws SQLFeatureNotSupportedException
{
return null;
}
}
   <4>、切换数据源时调用setCustomerType方法,比如我要使用dataSourceA,切换代码就是“DynamicDataSource.setCustomerType(DynamicDataSource.DATA_SOURCE_A);”。
   <5>、以上是主要的配置,其它配置跟配置Spring MVC是一样的,详细情况请阅读下面的源代码。
 
三、源代码下载地址
  <1>、同时使用两个数据源实例的源代码下载地址:http://pan.baidu.com/s/1o66a3kI
  <2>、两个数据源之间的切换实例的源代码下载地址:http://pan.baidu.com/s/1sjxgPSp
 
  转载请注明出处:http://xieke90.iyunv.com/blog/2259320
 

运维网声明 1、欢迎大家加入本站运维交流群:群②:261659950 群⑤:202807635 群⑦870801961 群⑧679858003
2、本站所有主题由该帖子作者发表,该帖子作者与运维网享有帖子相关版权
3、所有作品的著作权均归原作者享有,请您和我们一样尊重他人的著作权等合法权益。如果您对作品感到满意,请购买正版
4、禁止制作、复制、发布和传播具有反动、淫秽、色情、暴力、凶杀等内容的信息,一经发现立即删除。若您因此触犯法律,一切后果自负,我们对此不承担任何责任
5、所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其内容的准确性、可靠性、正当性、安全性、合法性等负责,亦不承担任何法律责任
6、所有作品仅供您个人学习、研究或欣赏,不得用于商业或者其他用途,否则,一切后果均由您自己承担,我们对此不承担任何法律责任
7、如涉及侵犯版权等问题,请您及时通知我们,我们将立即采取措施予以解决
8、联系人Email:admin@iyunv.com 网址:www.yunweiku.com

所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其承担任何法律责任,如涉及侵犯版权等问题,请您及时通知我们,我们将立即处理,联系人Email:kefu@iyunv.com,QQ:1061981298 本贴地址:https://www.yunweiku.com/thread-306416-1-1.html 上篇帖子: 表一对多关联([尚硅谷]_张晓飞_Mybatis 学习笔记四) 下篇帖子: spring多数据源的处理 mybatis实现跨库查询
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

扫码加入运维网微信交流群X

扫码加入运维网微信交流群

扫描二维码加入运维网微信交流群,最新一手资源尽在官方微信交流群!快快加入我们吧...

扫描微信二维码查看详情

客服E-mail:kefu@iyunv.com 客服QQ:1061981298


QQ群⑦:运维网交流群⑦ QQ群⑧:运维网交流群⑧ k8s群:运维网kubernetes交流群


提醒:禁止发布任何违反国家法律、法规的言论与图片等内容;本站内容均来自个人观点与网络等信息,非本站认同之观点.


本站大部分资源是网友从网上搜集分享而来,其版权均归原作者及其网站所有,我们尊重他人的合法权益,如有内容侵犯您的合法权益,请及时与我们联系进行核实删除!



合作伙伴: 青云cloud

快速回复 返回顶部 返回列表