一、同时使用多个数据源
以下实例演示同时使用两个数据源:
<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.iteye.com/blog/2259320
相关推荐
Spring Boot+Apache Shiro+Spring MVC+MyBatis+Quartz+Druid DEMO
基于Spring Spring MVC MyBatis的图书馆管理系统,使用Maven进行包管理。主要功能包括:图书查询、图书管理、图书编辑、读者管理、图书的借阅与归还以及借还日志记录等。 环境配置 开发环境:Windows 10,IntelliJ ...
数据源监控:druid 接口swagger文档 日志查询 邮件管理:发送邮件、搜索邮件 文件管理:上传文件、文件列表、文件删除 公告管理:公告未读提醒、发布公告、查询公告、公告阅读人列表 excel下载:自定义sql导出...
视图框架:Spring MVC 4.3.5 任务调度:Spring + Quartz 2.2.3 持久层框架:MyBatis 3.4.2 + Mybatis-plus 2.0.1 日志管理:SLF4J 1.7 + Log4j2 2.7 工具类:Apache Commons、Jackson 2.2、fastjson 1.2.20 前端技术...
项目基于jdk1.8整合了springboot+mvc+mybatis(通用mapper)+druid+jsp+bootstrap等技术,springboot+Listener(监听器),Filter(过滤器),Interceptor(拦截器),Servlet,springmvc静态资源,文件上传下载,多数据源切换,缓存...
项目基于jdk1.8整合了springboot+mvc+mybatis(通用mapper)+druid+jsp+bootstrap等技术,springboot+Listener(监听器),Filter(过滤器),Interceptor(拦截器),Servlet,springmvc静态资源,文件上传下载,多数据源切换,缓存...
开发技术:JAVA, JSP, Servlet, JDBC Development technology:JAVA, JSP, Servlet, JDBC ...开发框架:Spring、SpringMVC、Mybatis、Jquery、layui Development Framework:Spring、SpringMVC、Mybatis、Jquery、layui
使用aop进行多数据源切换 springMVC+spring+mybatis增删改查的使用。dk8+tomcat8+mysql+Eclipse+maven。spring+spring mvc+mybatis+bootstrap+jquery
ssm(spring+spring mvc+mybatis+maven)高仿bilibili视频网站项目实例里面包含论坛, 购物商城 网页 后台管理的java项目集成演示地址: (服务器不太稳定,可以尝试刷新,感谢大佬捐赠的服务器费用)开发环境:Eclipse ...
可以用于毕业设计(项目源码+项目说明)目前在window10/11测试环境一切正常,用于演示的图片和部署教程说明都在压缩包里
SpringMVC MVC框架 Spring Framework 容器 Apache Shiro 安全框架 Mybatis ORM框架 MyBatis Generator 代码生成 C3P0 数据库连接池 Ehcache 进程内缓存框架 Apache poi 文件导入导出 Maven 项目构建管理 ...
项目基于jdk1.8整合了springboot+mvc+mybatis(通用mapper)+druid+jsp+bootstrap等技术,springboot+Listener(监听器),Filter(过滤器),Interceptor(拦截器),Servlet,springmvc静态资源,文件上传下载,多数据源切换,缓存...
采用springmvc+mybatis+shiro+page分页插件+多数据源框架,实现shiro权限控制、mybatis分页插件、实现多数据源控制、maven项目
项目描述 WEB端toutiao.com做的一个Java web项目。使用SpringBoot+Mybatis+thymeleaf开发。前端使用bootstrap、html5、css3动画、jQuery、WangEditor富文本编辑器...spring boot+spring mvc+mybatis+bootstrap+jquery
2.4 spring-db 我这里创建数据源,但是alt+/出不来提示,我一想,mysql的包没载入,在maven中加入 还是没得,恩,我加了jdbc的包,还是没有,我以为是没有源码,下载了,还是没有提示,棒 没有提示,我追了下源码...
1. 首先安装Mysql5.7,设置用户名为root,密码为123456,并保证其在运行状态,并执行library.sql文件导入数据。 2. 然后再配置Maven到环境变量中,在源代码目录下运行 3. 使用浏览器访问http://localhost:8080即可...
SpringMVC+Mybatis(多数据源)+Mongo(多数据...正如我代码中的demo,配置了两个mysql数据源+2个mongo数据源,可以在service实现中随意访问。除此之外还通过maven profile支持了多环境自动切换配置。欢迎下载,互相学习。
利用MVC(SpringMVC+Spring+MyBatis)实现购物车试题,包括完整源代码和测试数据
调用webservice,插入数据,整合druid监控数据源; webservice地址:http://localhost:8080/taixingMsg/webservice/SendMsgService?wsdl; webservice配置文件在spring-mvc.xml和web.xml中; 数据库监控地址...
mybatis+spring+springmvc实现对oracle数据库的简单查询,只需修改一下application中数据源的配置,在数据库中建立相应的表即可直接运行