spring mvc+mybatis+多数据源切换 选取Oracle切换到另一个Oracle数据源。oracle其中一个为默认数据源,在测试的action中,进行Oracle和另一个Oracle的动态切换。
web.xml配置
<context-param>
<param-name>webAppRootKey</param-name>
<param-value>trac</param-value>
</context-param>
<!-- Spring的log4j监听器 -->
<listener>
<listener-class>org.springframework.web.util.Log4jConfigListener</listener-class>
</listener>
<!-- 字符集 过滤器 -->
<filter>
<filter-name>CharacterEncodingFilter</filter-name>
<filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
<init-param>
<param-name>encoding</param-name>
<param-value>utf8</param-value>
</init-param>
<init-param>
<param-name>forceEncoding</param-name>
<param-value>true</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>CharacterEncodingFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
<!-- Spring view分发器 -->
<servlet>
<servlet-name>dispatcher</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<description>spring mvc 配置文件</description>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:springMvc-servlet.xml</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>dispatcher</servlet-name>
<url-pattern>*.action</url-pattern>
</servlet-mapping>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
springMvc-servlet.xml配置
<mvc:annotation-driven />
<context:component-scan base-package="com.trac" />
<bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver" />
<!-- freemarker config -->
<bean id="freemarkerConfig" class="org.springframework.web.servlet.view.freemarker.FreeMarkerConfigurer">
<property name="templateLoaderPath" value="/WEB-INF/freemarker/" />
<property name="freemarkerVariables">
<map>
<entry key="xml_escape" value-ref="fmXmlEscape" />
</map>
</property>
<property name="freemarkerSettings">
<props>
<prop key="defaultEncoding">UTF-8</prop>
</props>
</property>
</bean>
<bean id="fmXmlEscape" class="freemarker.template.utility.XmlEscape" />
<!-- View resolvers can also be configured with ResourceBundles or XML files.
If you need different view resolving based on Locale, you have to use the
resource bundle resolver. -->
<bean id="viewResolver" class="org.springframework.web.servlet.view.freemarker.FreeMarkerViewResolver">
<property name="exposeRequestAttributes" value="true" />
<property name="exposeSessionAttributes" value="true" />
<property name="exposeSpringMacroHelpers" value="true" />
<property name="contentType" value="text/html;charset=UTF-8" />
<property name="cache" value="true" />
<property name="prefix" value="" />
<property name="suffix" value=".jsp" />
</bean>
applicationContext.xml
<!-- 读取配置文件 -->
<bean id="propertyConfigurer" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="locations">
<list>
<value>classpath:jdbc.properties</value>
<value>classpath:application.properties</value>
</list>
</property>
</bean>
<bean id="parentDataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
</bean>
<bean id="oracleDataSource1" parent="parentDataSource">
<property name="driverClassName" value="${jdbc1.driver}"></property>
<property name="url" value="${jdbc1.url}"></property>
<property name="username" value="${jdbc1.username}"></property>
<property name="password" value="${jdbc1.password}"></property>
</bean>
<bean id="oracleDataSource2" parent="parentDataSource">
<property name="driverClassName" value="${jdbc2.driver}"></property>
<property name="url" value="${jdbc2.url}"></property>
<property name="username" value="${jdbc2.username}"></property>
<property name="password" value="${jdbc2.password}"></property>
</bean>
<bean id="dataSource" class="com.trac.dao.datasource.DataSources">
<property name="targetDataSources">
<map key-type="java.lang.String">
<entry value-ref="oracleDataSource1" key="ORACLE1"></entry>
<entry value-ref="oracleDataSource2" key="ORACLE2"></entry>
</map>
</property>
<property name="defaultTargetDataSource" ref="oracleDataSource1"></property>
</bean>
<bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="dataSource" />
</bean>
<!-- 创建SqlSessionFactory,同时指定数据源和mapper -->
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<property name="dataSource" ref="dataSource" />
<property name="mapperLocations" value="classpath*:com/trac/ibatis/dbcp/*.xml" />
</bean>
<bean id="sqlSession" class="org.mybatis.spring.SqlSessionTemplate">
<constructor-arg index="0" ref="sqlSessionFactory" />
</bean>
<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<property name="basePackage" value="com.trac.dao" />
</bean>
配置 parentDataSource 的父bean.再配置多个数据源继承这个父bean,对driverClass,url,username,password,等数据源连接参数进行各自的重写。例如 oracleDataSource1,在 DataSources bean中注入所有要切换的数据源,并且设置默认的数据源。
DataSourceInstances.java
/**
* 定义数据源的标识, 和applicationContext.xml中 DataSources 的 targetDataSources 的key对应
*/
public class DataSourceInstances{
public static final String ORACLE1="ORACLE1";
public static final String ORACLE2="ORACLE2";
}
DataSourceSwitch.java
public class DataSourceSwitch{
private static final ThreadLocal contextHolder=new ThreadLocal();
/**
* @Description: 设置数据源类型
* @param dataSourceType 数据库类型
* @return void
* @throws
*/
public static void setDataSourceType(String dataSourceType){
contextHolder.set(dataSourceType);
}
/**
* @Description: 获取数据源类型
* @param
* @return String
* @throws
*/
public static String getDataSourceType(){
return (String) contextHolder.get();
}
/**
* @Description: 清除数据源类型
* @param
* @return void
* @throws
*/
public static void clearDataSourceType(){
contextHolder.remove();
}
}
DataSources.java
import org.springframework.jdbc.datasource.lookup.AbstractRoutingDataSource;
/**
*配置于applicationContext 中,线程局部变量ThreadLocal contextHolder 保存当前需要的数据源类型,当 DataSourceSwitch. setDataSourceType(DataSourceInstances.XXX) 保存当前需要的数据源类型的时候,DataSources 会从当前线程中查找线程变量的数据源类型,从而决定使用何种数据源
*/
public class DataSources extends AbstractRoutingDataSource{
@Override
protected Object determineCurrentLookupKey() {
return DataSourceSwitch.getDataSourceType();
}
}
本文转载自:
http://blog.csdn.net/l1028386804/article/details/48163279
http://blog.csdn.net/gaofuqi/article/details/46417281
感谢这两位大神的文章帮我了很大的忙,希望可以帮到各位
分享到:
相关推荐
Java基于Spring+SpringMVC+MyBatis实现的学生信息管理系统源码,SSM+Vue的学生管理系统。 Java基于Spring+SpringMVC+MyBatis实现的学生信息管理系统源码,SSM+Vue的学生管理系统。 Java基于Spring+SpringMVC+...
完善的Spring+SpringMVC+Mybatis+easyUI后台管理系统(RESTful API+redis).zip 完善的Spring+SpringMVC+Mybatis+easyUI后台管理系统(RESTful API+redis).zip 完善的Spring+SpringMVC+Mybatis+easyUI后台管理系统...
在多数据源配置中,Spring能够帮助管理不同的数据源,通过配置bean来切换和控制数据源的使用。 **SpringMVC** 是Spring框架的一部分,专为Web开发设计。它简化了模型-视图-控制器(Model-View-Controller,MVC)的...
基于Spring+SpringMVC+Mybatis架构的博客系统:博客管理、图表数据、日志分析、访问记录、图库管理、资源管理、友链通知等。良好的页面预加载,无限滚动加载,文章置顶,博主推荐等。提供 用户端+管理端 的整套系统...
4. 配置Mybatis:创建mybatis的全局配置文件,mybatis-config.xml,配置数据源、SqlSessionFactory等,以及Mapper的XML配置文件。 5. 编写DAO层:定义Mapper接口,编写对应的Mapper XML文件,实现SQL语句。 6. 业务...
基于spring+springMvc+mybatis 开发的企业门户网站基于spring+springMvc+mybatis 开发的企业门户网站基于spring+springMvc+mybatis 开发的企业门户网站基于spring+springMvc+mybatis 开发的企业门户网站基于spring+...
基于SSM(Spring+SpringMVC+Mybatis)的新闻管理系统源码+数据库.zip 基于SSM(Spring+SpringMVC+Mybatis)的新闻管理系统源码+数据库.zip 基于SSM(Spring+SpringMVC+Mybatis)的新闻管理系统源码+数据库.zip 基于SSM...
基于 SpringBoot + Spring + SpringMvc + Mybatis + Shiro+ Redis 开发单点登录管理系统 基于 SpringBoot + Spring + SpringMvc + Mybatis + Shiro+ Redis 开发单点登录管理系统 基于 SpringBoot + Spring + ...
总之,“图书管理系统SpringMvc+Mybatis”是一个典型的Web应用程序实例,它展示了如何利用现代Java技术栈进行高效开发。理解并掌握SpringMvc的控制流程、Mybatis的SQL映射以及它们之间的协同工作原理,对于提升Java ...
接下来,我们将深入探讨这两个框架以及它们在"SpringMVC+Mybatis demo"中的应用。 **SpringMVC** SpringMVC是Model-View-Controller架构模式的一种实现,用于构建Web应用程序。它的主要组件包括DispatcherServlet...
本项目“Spring+SpringMVC+Mybatis动态链接多数据源”旨在实现一个灵活、可扩展的数据源切换机制,以适应复杂的业务场景。 Spring框架作为Java领域中最广泛使用的轻量级框架,它提供了强大的依赖注入和AOP(面向切...
**SpringMVC+Mybatis,CRM系统教程与源代码详解** 在现代企业信息化管理中,客户关系管理系统(CRM)扮演着至关重要的角色。本教程将深入讲解如何利用SpringMVC和Mybatis两大主流技术框架构建一个完整的CRM系统,...
总结来说,"spring+springMVC+mybatis+quartz动态定时任务创建"这个技术栈利用Spring的全面性、Spring MVC的Web处理能力、MyBatis的数据访问效率以及Quartz的定时任务管理,构建出一个能够灵活应对各种定时需求的...
标题 "Spring+SpringMVC+Mybatis资源课件" 提供了一个学习路径,涉及Java开发中的三个关键组件:Spring框架、SpringMVC和Mybatis。这些技术是构建现代企业级Web应用程序的基础,尤其是在Java世界中。 Spring框架是...
本篇将详细介绍如何在Spring、SpringMVC和MyBatis集成环境中配置多数据源。 首先,我们来理解数据源(DataSource)的概念。数据源是Java中用于存储和管理数据库连接的组件,它实现了Java的javax.sql.DataSource接口...
本项目框架“maven+springMVC+mybatis+velocity+mysql+junit”提供了一种高效、灵活且可维护的解决方案。以下将详细讲解这些组件及其作用。 1. Maven: Maven是一个项目管理工具,用于构建、依赖管理和项目信息...
Spring+SpringMVC+MyBatis+Shiro+MySQL+Redis+Maven+EasyUI+Bootstrap实现的通用权限管理系统。 Spring+SpringMVC+MyBatis+Shiro+MySQL+Redis+Maven+EasyUI+Bootstrap实现的通用权限管理系统 Spring+SpringMVC+...
在线投票系统-jsp+springMVC+mybatis在线投票系统-jsp+springMVC+mybatis在线投票系统-jsp+springMVC+mybatis在线投票...jsp+springMVC+mybatis在线投票系统-jsp+springMVC+mybatis在线投票系统-jsp+springMVC+mybatis
4. **配置Mybatis**:配置mybatis-config.xml,指定mapper文件的位置,以及数据源和SqlSessionFactory的配置。 5. **创建Velocity模板**:在src/main/webapp/WEB-INF/velocity目录下创建Velocity模板文件,用于渲染...
2. 配置Spring:创建Spring的配置文件,定义Bean、数据源、事务管理器等,以及Spring与Mybatis的整合配置。 3. 配置SpringMVC:配置DispatcherServlet,定义视图解析器,处理拦截器,以及Controller的映射。 4. 配置...