spring 集成mybatis——多数据源切换(附带定时器的配置)
新建com.millery.utils包在其下新建DataSourceContextHolder类
package com.millery.utils;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(); }
}
复制代码
新建DataSourceContextHolder类
package com.millery.utils;import org.springframework.jdbc.datasource.lookup.AbstractRoutingDataSource;public class DynamicDataSource extends AbstractRoutingDataSource { @Override protected Object determineCurrentLookupKey() { return DataSourceContextHolder.getDbType(); }
}
复制代码
配置mybatis
<?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:context="http://www.springframework.org/schema/context"
xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:jpa="http://www.springframework.org/schema/data/jpa"
xmlns:util="http://www.springframework.org/schema/util"
xmlns:jaxws="http://cxf.apache.org/jaxws" xmlns:p="http://www.springframework.org/schema/p"
xmlns:aop="http://www.springframework.org/schema/aop" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.3.xsd http://www.springframework.org/schema/contexthttp://www.springframework.org/schema/context/spring-context-4.3.xsdhttp://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.3.xsd http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-4.3.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.3.xsd http://www.springframework.org/schema/data/jpa http://www.springframework.org/schema/data/jpa/spring-jpa-1.0.xsd http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://cxf.apache.org/jaxws http://cxf.apache.org/schemas/jaxws.xsd"><import resource="classpath:META-INF/cxf/cxf.xml"/> <import resource="classpath:META-INF/cxf/cxf-servlet.xml"/> <import resource="classpath:META-INF/cxf/cxf-extension-soap.xml"/> <!-- 自动扫描 --><context:component-scan base-package="com.hqgf" /><!-- 引入配置文件 --><bean id="propertyConfigurer"class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer"><property name="location" value="classpath:jdbc.properties" /></bean><!-- <util:properties id="APP_PROPERTIES" location="classpath:attendance.properties" local-override="true"/> --><bean id="HrtestSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close"><property name="driverClassName" value="com.microsoft.sqlserver.jdbc.SQLServerDriver"/><property name="url" value="jdbc:sqlserver://172.69.1.236;DatabaseName=LongshineWebHr" /><property name="username" value="sa" /><property name="password" value="Hqmart88" /></bean><bean id="HrSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close"><property name="driverClassName" value="com.microsoft.sqlserver.jdbc.SQLServerDriver"/><property name="url" value="jdbc:sqlserver://172.88.10.161;DatabaseName=LongshineWebHr " /><property name="username" value="sa" /><property name="password" value="Hqmart88" /></bean><bean id="AttendanceSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close"><property name="driverClassName" value="com.mysql.jdbc.Driver" /><property name="url" value="jdbc:mysql://172.69.1.8:3306/hws_sqlservice?useUnicode\=true;characterEncoding\=utf-8" /><property name="username" value="root" /><property name="password" value="mysql_root2017@hqtest.com" /></bean><!-- spring和MyBatis完美整合,不需要mybatis的配置映射文件 --><bean id="dataSource" class="com.millery.utils.DynamicDataSource"> <property name="targetDataSources"> <map key-type="java.lang.String"> <entry value-ref="HrtestSource" key="HrtestSource"></entry> <entry value-ref="HrSource" key="HrSource"></entry> <entry value-ref="AttendanceSource" key="AttendanceSource"></entry> </map> </property> <property name="defaultTargetDataSource" ref="HrtestSource"></property><!-- 默认使用HrtestSource的数据源 --> </bean> <!-- 2. mybatis 的SqlSession 的工厂: SqlSessionFactoryBean --> <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean"> <property name="dataSource" ref="dataSource"/> <property name="mapperLocations" value="classpath:com/hqgf/mapping/*.xml"/> </bean> <!-- 3. mybatis 自动扫描加载Sql 映射文件 : MapperScannerConfigurer --> <!-- <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer"> <property name="basePackage" value="com.mapper"/> <property name="sqlSessionFactory" ref="sqlSessionFactory"/> </bean> --> <!-- 4. 事务管理 : DataSourceTransactionManager --> <bean id="txManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager"> <property name="dataSource" ref="dataSource"/> </bean> <!-- 5. 使用声明式事务 --> <tx:annotation-driven transaction-manager="txManager" /> <!-- 数据抓取任务 --><!-- 任务实体 --><bean id="eleTaskBean" class="com.hqgf.attendanceupload.HwsTimeTask" /> <bean id="eletasktimingmethod" class="org.springframework.scheduling.quartz.MethodInvokingJobDetailFactoryBean"> <property name="targetObject" ref="eleTaskBean" /> <!-- targetMethod 配置定时执行的方法名 --><property name="targetMethod" value="TimeTaskData" /> <property name="concurrent" value="false" /> </bean><!-- 设置每天凌晨两点执行定时任务 --><bean id="eleTaskTrigger"class="org.springframework.scheduling.quartz.CronTriggerFactoryBean"> <property name="jobDetail" ref="eletasktimingmethod" /> <property name="cronExpression" value="01 50 16 * * ?" /> </bean> <!-- 订制任务列表 --><bean id="scheduler"class="org.springframework.scheduling.quartz.SchedulerFactoryBean"> <property name="triggers"> <list> <ref bean="eleTaskTrigger" /> </list> </property> </bean>
</beans>
复制代码
每个mapper调用之前
@Overridepublic String queryDept(String hr_dept_code) throws Exception {DataSourceContextHolder.setDbType("AttendanceSource");//注意这里在调用userMapper前切换到AttendanceSource的数据源 String attendanceid= getSqlSession().selectOne("com.hqgf.dao.IUploadAttendanceDao.queryDept",hr_dept_code);System.out.println("部门编码匹配成功__"+attendanceid);return attendanceid;}
复制代码
数据库切换成功。
转载于:https://juejin.im/post/5a532eb26fb9a01cba426cab
spring 集成mybatis——多数据源切换(附带定时器的配置)相关推荐
- Spring集成Mybatis多数据源配置
既然在整理Mybatis那就把经常用的这个多数据源的笔记也整一下吧. Spring集成Mybatis在之前就已经提到了.Spring集成Mybatis 集成Mybatis多数据源有两种方式: 1.创建 ...
- spring boot+mybatis 多数据源切换
由于公司业务划分了多个数据库,开发一个项目会同时调用多个库,经过学习我们采用了注解+aop的方式实现的 1.首先定义一个注解类 @Retention(RetentionPolicy.RUNTIME) ...
- hikari如何切换数据源_spring boot+mybatis 多数据源切换(实例讲解)
由于公司业务划分了多个数据库,开发一个项目会同事调用多个库,经过学习我们采用了注解+aop的方式实现的 1.首先定义一个注解类 @Retention(RetentionPolicy.RUNTIME) ...
- spring+mybatis 多数据源切换
spring+mybatis 多数据源切换 摘自: http://www.oschina.net/code/snippet_347813_12525 1. 代码: DbContextHolder pu ...
- Spring集成Mybatis,spring4.x整合Mybatis3.x
Spring集成Mybatis,spring4.x整合Mybatis3.x ============================== 蕃薯耀 2018年3月14日 http://www.cnblo ...
- Spring集成MyBatis
将mybatis与spring进行整合,主要解决的问题就是讲SqlSessionFactory对象交由spring来管理,所以,该整合,只需要将SqlSessionFactory的对象生成器SqlSe ...
- SSM + Shiro 整合 (2)- 实现 Spring 集成 MyBatis
项目源码:https://github.com/weimingge14/Shiro-project 演示地址:http://liweiblog.duapp.com/Shiro-project/logi ...
- Spring集成MyBatis框架
Spring集成MyBatis框架 Java在写数据库查询时,我接触过四种方式: 1.纯Java代码,引用对应的数据库驱动包,自己写连接与释放逻辑(可以用连接池) 这种模式实际上性能是非常不错的,但是 ...
- Spring集成Mybatis配置映射文件方法详解
Spring ORM模块集成Mybatis使用到了mybatis-spring,在配置mybatis映射文件的时候,一般不直接在Mybatis的配置文件里进行配置,而会在Spring的配置文件里使用M ...
最新文章
- 700 页的机器学习笔记火了!完整版开放下载
- 2020年智能车竞赛暑期工作计划一览表各
- 韩国遭到日本制裁,三星被打回原形,原来是个组装工厂?
- centos6.5卸载java_删除CentOS6.5系统自带的jdk
- jta atomikos_带有Atomikos示例的Tomcat中的Spring JTA多个资源事务
- 安装LibreOffice和字体
- 【干货】python多进程和多线程谁更快
- Linux下C程序进程地址空间布局[转]
- PX4姿态解算磁偏补偿
- 在线 IDE,支持 C-sharp, PHP, JavaScript - CodeRun
- 【Flutter 问题系列第 26 篇】给 TextField 添加背景色,为什么没有效果 ?
- 7z解压后重命名文件的方法
- 你想知道的NB-IoT知识都在这里了!
- 初学编程者开始Revit 二次开发自学教程
- 黑苹果0005——我的config文件(笔记本 intel HD630)
- 整数的按权展开 (10 分)
- 使用win10自带的手机投屏功能
- python实现爬取东方财富网
- GO语言学习资源整理
- shell 脚本中双引号、单引号、反引号的区别,其他命令
热门文章
- Guojiz网址导航系统PHP网站源码
- android编辑框最大字数,TextView 限制最大行数、最小行数、字数超过“...”表示...
- js将9999以内的值的个位改为0
- 全新玖五社区系统整站源码
- wordpress插件-WP Rocket 3.9.3缓存加速插件免授权版
- 品牌设计工作室html5模板
- IDEA配置Docker一键部署SpringBoot项目(企业级做法)
- Vue系列vue-router的参数传递的两种方式(五)
- Google我的商家设定
- 免费发短信(仅限美国国内)How to Send Free Text Messages Using PHP