spring mvc+mybatis+多数据源切换 选取oracle,mysql作为例子切换数据源。oracle为默认数据源,在测试的action中,进行mysql和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>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/dispatcher.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>
dispatcher.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=".ftl" />
</bean>
applicationContext.xml
<bean id="parentDataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
</bean>
<bean id="mySqlDataSource" parent="parentDataSource">
<property name="driverClassName" value="com.mysql.jdbc.Driver"></property>
<property name="url" value="jdbc:mysql://localhost:3306/test"></property>
<property name="username" value="root"></property>
<property name="password" value="root"></property>
</bean>
<bean id="oracleDataSource" parent="parentDataSource">
<property name="driverClassName" value="oracle.jdbc.driver.OracleDriver"></property>
<property name="url" value="jdbc:oracle:thin:@10.16.17.40:1531:addb"></property>
<property name="username" value="trac"></property>
<property name="password" value="trac"></property>
</bean>
<bean id="dataSource" class="com.trac.dao.datasource.DataSources">
<property name="targetDataSources">
<map key-type="java.lang.String">
<entry value-ref="mySqlDataSource" key="MYSQL"></entry>
<entry value-ref="oracleDataSource" key="ORACLE"></entry>
</map>
</property>
<property name="defaultTargetDataSource" ref="oracleDataSource"></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,等数据源连接参数进行各自的重写。例如 mySqlDataSource ,在 DataSources bean中注入所有要切换的数据源,并且设置默认的数据源。
DataSourceInstances.java
public class DataSourceInstances{
public static final String MYSQL="MYSQL";
public static final String ORACLE="ORACLE";
}
定义数据源的标识, 和applicationContext.xml中 DataSources 的 targetDataSources 的key对应
DataSourceSwitch.java
public class DataSourceSwitch{
private static final ThreadLocal contextHolder=new ThreadLocal();
public static void setDataSourceType(String dataSourceType){
contextHolder.set(dataSourceType);
}
public static String getDataSourceType(){
return (String) contextHolder.get();
}
public static void clearDataSourceType(){
contextHolder.remove();
}
}
DataSources.java
import org.springframework.jdbc.datasource.lookup.AbstractRoutingDataSource;
public class DataSources extends AbstractRoutingDataSource{
@Override
protected Object determineCurrentLookupKey() {
return DataSourceSwitch.getDataSourceType();
}
}
配置于applicationContext 中,线程局部变量ThreadLocal contextHolder 保存当前需要的数据源类型,当 DataSourceSwitch.setDataSourceType(DataSourceInstances.XXX) 保存当前需要的数据源类型的时候,DataSources 会从当前线程中查找线程变量的数据源类型,从而决定使用何种数据源
TestAction.java
@Controller
@SuppressWarnings("unused")
public class TestAction {
@Autowired
TestMapper testMapper;
@RequestMapping("/test.action")
public ModelAndView test(
HttpServletRequest request,
HttpServletResponse resp){
ModelAndView model = new ModelAndView("test");
model.addObject("test1", "这是一个测试,获取默认数据连接MYSQL:"+testMapper.test());
DataSourceSwitch.setDataSourceType(DataSourceInstances.ORACLE);
model.addObject("test2", "这是一个测试,获取数据连接ORACLE:"+testMapper.test());
DataSourceSwitch.setDataSourceType(DataSourceInstances.MYSQL);
model.addObject("test3", "这是一个测试,获取数据连接MYSQL:"+testMapper.test());
return model;
}
}
分享到:
相关推荐
Spring Boot+Apache Shiro+Spring MVC+MyBatis+Quartz+Druid DEMO
基于Spring Spring MVC MyBatis的图书馆管理系统,使用Maven进行包管理。主要功能包括:图书查询、图书管理、图书编辑、读者管理、图书的借阅与归还以及借还日志记录等。 环境配置 开发环境:Windows 10,IntelliJ ...
数据源监控:druid 接口swagger文档 日志查询 邮件管理:发送邮件、搜索邮件 文件管理:上传文件、文件列表、文件删除 公告管理:公告未读提醒、发布公告、查询公告、公告阅读人列表 excel下载:自定义sql导出...
项目基于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静态资源,文件上传下载,多数据源切换,缓存...
视图框架: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 前端技术...
开发技术:JAVA, JSP, Servlet, JDBC Development technology:JAVA, JSP, Servlet, JDBC 开发环境:IntelliJ idea 2017.1+, jdk 8u91, mysql ...Development Framework:Spring、SpringMVC、Mybatis、Jquery、layui
ssm(spring+spring mvc+mybatis+maven)高仿bilibili视频网站项目实例里面包含论坛, 购物商城 网页 后台管理的java项目集成演示地址: (服务器不太稳定,可以尝试刷新,感谢大佬捐赠的服务器费用)开发环境:Eclipse ...
使用aop进行多数据源切换 springMVC+spring+mybatis增删改查的使用。dk8+tomcat8+mysql+Eclipse+maven。spring+spring mvc+mybatis+bootstrap+jquery
可以用于毕业设计(项目源码+项目说明)目前在window10/11测试环境一切正常,用于演示的图片和部署教程说明都在压缩包里
基础数据 系统维护 运行环境 Eclipse ,JDK 1.8 ,Tomcat7,maven 项目技术(必填) 后端技术 SpringMVC MVC框架 Spring Framework 容器 Apache Shiro 安全框架 Mybatis ORM框架 MyBatis Generator 代码生成 ...
项目基于jdk1.8整合了springboot+mvc+mybatis(通用mapper)+druid+jsp+bootstrap等技术,springboot+Listener(监听器),Filter(过滤器),Interceptor(拦截器),Servlet,springmvc静态资源,文件上传下载,多数据源切换,缓存...
项目描述 ...使用SpringBoot+Mybatis+thymeleaf开发。前端使用bootstrap、html5、css3动画、jQuery、WangEditor富文本编辑器、Webuoloader和Base64处理图片上传、ajax...spring boot+spring mvc+mybatis+bootstrap+jquery
1. 首先安装Mysql5.7,设置用户名为root,密码为123456,并保证其在运行状态,并执行library.sql文件导入数据。 2. 然后再配置Maven到环境变量中,在源代码目录下运行 3. 使用浏览器访问http://localhost:8080即可...
利用MVC(SpringMVC+Spring+MyBatis)实现购物车试题,包括完整源代码和测试数据
SpringMVC+Mybatis(多数据源)+Mongo(多数据...正如我代码中的demo,配置了两个mysql数据源+2个mongo数据源,可以在service实现中随意访问。除此之外还通过maven profile支持了多环境自动切换配置。欢迎下载,互相学习。
2.4 spring-db 我这里创建数据源,但是alt+/出不来提示,我一想,mysql的包没载入,在maven中加入 还是没得,恩,我加了jdbc的包,还是没有,我以为是没有源码,下载了,还是没有提示,棒 没有提示,我追了下源码...
采用springmvc+mybatis+shiro+page分页插件+多数据源框架,实现shiro权限控制、mybatis分页插件、实现多数据源控制、maven项目
调用webservice,插入数据,整合druid监控数据源; webservice地址:http://localhost:8080/taixingMsg/webservice/SendMsgService?wsdl; webservice配置文件在spring-mvc.xml和web.xml中; 数据库监控地址...
mybatis+spring+springmvc实现对oracle数据库的简单查询,只需修改一下application中数据源的配置,在数据库中建立相应的表即可直接运行