一、关闭数据源自动配置(很关键)

@SpringBootApplication(exclude = { DataSourceAutoConfiguration.class })

如果不关闭会报异常:org.springframework.beans.factory.BeanCurrentlyInCreationException: Error creating bean with name 'dataSource': Requested bean is currently in creation: Is there an unresolvable circular reference?

ps:之前一直没折腾成功,竟然就是少了这一个配置,泪奔

二、配置文件

spring.datasource.druid.one.driver-class-name=com.mysql.jdbc.Driver
spring.datasource.druid.one.url=
spring.datasource.druid.one.username=
spring.datasource.druid.one.password= spring.datasource.druid.two.driver-class-name=com.mysql.jdbc.Driver
spring.datasource.druid.two.url=
spring.datasource.druid.two.username=
spring.datasource.druid.two.password=

三、数据源定义

    @Bean
@ConfigurationProperties("spring.datasource.druid.one")
public DataSource dataSource1() throws Exception {
return DruidDataSourceBuilder.create().build();
} @Bean
@ConfigurationProperties("spring.datasource.druid.two")
public DataSource dataSource2() throws Exception {
return DruidDataSourceBuilder.create().build();
}

四、多数据类定义

package com.qmtt.config;

import org.springframework.jdbc.datasource.lookup.AbstractRoutingDataSource;

public class DynamicDataSource extends AbstractRoutingDataSource {

    @Override
protected Object determineCurrentLookupKey() {
return DynamicDataSourceHolder.getDataSourceKey();
} }

五、ThreadLocal定义

package com.qmtt.config;

import java.util.HashSet;
import java.util.Set; public class DynamicDataSourceHolder { private static final ThreadLocal<String> dataSourceThreadLocal = new ThreadLocal<String>(); private static Set<String> dataSourceKeys = new HashSet<String>(); static {
dataSourceKeys.add("master");
dataSourceKeys.add("slave");
} public static void setDataSourceKey(String dataSourceKey) {
dataSourceThreadLocal.set(dataSourceKey);
} public static String getDataSourceKey() {
return dataSourceThreadLocal.get();
} public static void clearDataSourceKey() {
dataSourceThreadLocal.remove();
} public static boolean containsDataSourceKey(String dataSourceKey) {
return dataSourceKeys.contains(dataSourceKey);
}
}

六、数据源定义

    @Bean
public DataSource dataSource(@Qualifier("dataSource1") DataSource dataSource1,
@Qualifier("dataSource2") DataSource dataSource2) throws Exception {
DynamicDataSource dynamicDataSource = new DynamicDataSource();
// 创建默认数据源
dynamicDataSource.setDefaultTargetDataSource(dataSource1());
// 配置多数据源
Map<Object, Object> dataSourceMap = new HashMap<>(4);
dataSourceMap.put("master", dataSource1);
dataSourceMap.put("slave", dataSource2);
dynamicDataSource.setTargetDataSources(dataSourceMap);
return dynamicDataSource;
}

七、SqlSessionFactory定义

常规写法

八、AOP切换

Aspect拦截dao就行

第一步很关键

spring boot druid mybatis多数据源的更多相关文章

  1. spring boot + druid + mybatis + atomikos 多数据源配置 并支持分布式事务

    文章目录 一.综述 1.1 项目说明 1.2 项目结构 二.配置多数据源并支持分布式事务 2.1 导入基本依赖 2.2 在yml中配置多数据源信息 2.3 进行多数据源的配置 三.整合结果测试 3.1 ...

  2. Spring boot 与mybatis 多数据源问题

    https://www.cnblogs.com/ityouknow/p/6102399.html Spring Boot 集成Mybatis实现多数据源 https://blog.csdn.net/m ...

  3. Spring Boot集成Mybatis双数据源

    这里用到了Spring Boot + Mybatis + DynamicDataSource配置动态双数据源,可以动态切换数据源实现数据库的读写分离. 添加依赖 加入Mybatis启动器,这里添加了D ...

  4. spring boot(七)mybatis多数据源解决方案

    说起多数据源,一般都来解决那些问题呢,主从模式或者业务比较复杂需要连接不同的分库来支持业务.我们项目是后者的模式,网上找了很多,大都是根据jpa来做多数据源解决方案,要不就是老的spring多数据源解 ...

  5. Spring Boot 整合 Mybatis 实现 Druid 多数据源详解

    摘要: 原创出处:www.bysocket.com 泥瓦匠BYSocket 希望转载,保留摘要,谢谢! “清醒时做事,糊涂时跑步,大怒时睡觉,独处时思考” 本文提纲一.多数据源的应用场景二.运行 sp ...

  6. Spring Boot2 系列教程(二十五)Spring Boot 整合 Jpa 多数据源

    本文是 Spring Boot 整合数据持久化方案的最后一篇,主要和大伙来聊聊 Spring Boot 整合 Jpa 多数据源问题.在 Spring Boot 整合JbdcTemplate 多数据源. ...

  7. Spring Boot + Druid 多数据源绑定

    date: 2019-12-19 14:40:00 updated: 2019-12-19 15:10:00 Spring Boot + Druid 多数据源绑定 版本环境:Spring Boot 2 ...

  8. 太妙了!Spring boot 整合 Mybatis Druid,还能配置监控?

    Spring boot 整合 Mybatis Druid并配置监控 添加依赖 <!--druid--> <dependency> <groupId>com.alib ...

  9. Spring Boot 2.x 多数据源配置之 MyBatis 篇

    场景假设:现有电商业务,商品和库存分别放在不同的库 配置数据库连接 app: datasource: first: driver-class-name: com.mysql.cj.jdbc.Drive ...

随机推荐

  1. android TextView 设置部分文字背景色 和 文字颜色

    通过SpannableStringBuilder来实现,它就像html里边的元素改变指定文字的文字颜色或背景色 ? 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 ...

  2. 【HDU2007】平方和与立方和

    http://acm.hdu.edu.cn/showproblem.php?pid=2007 垃圾水题 随便搜了几个公式(但我实际写的暴力...题目保证不爆int,说明n,m<=10^3) 1^ ...

  3. oracle重命名数据文件

    重命名数据文件   方法1: sql>alter tablespace users offline; sql>host cp /u01/app/oracle/oradata/orcl/us ...

  4. SPOJ:Robot(数学期望)

    There is a robot on the 2D plane. Robot initially standing on the position (0, 0). Robot can make a ...

  5. 【POJ 2411】 Mondriaan's Dream

    [题目链接] 点击打开链接 [算法] 很明显,我们可以用状态压缩动态规划解决此题 f[n][m]表示n-1行已经放满,第n行状态为m的合法的方案数 状态转移方程很好推 注意这题时限较紧,注意加一些小优 ...

  6. registerWithTouchDispatcher 注册单点触摸事件

    Doc: If isTouchEnabled, this method is called onEnter. Override it to change the way CCLayer receive ...

  7. python学习笔记2-条件语句

    #条件语句 ''' if 判断条件: 执行语句…… else: 执行语句…… ''' flag = False name = 'python' if name == 'python': # 判断变量否 ...

  8. 如何开始学习Go语言

    除了Java.Python和JavaScript之外,如果要开始学习一门新语言的话,我想应该是Go! Go语言正在被越来越多的公司使用.我们公司的后端服务已经全面采用Go语言实现了. 最开始接触Go语 ...

  9. Swift3.0 控制流

    常用的一些判断方式 //for in let numberList = [, , , , ] //获取第一个元素 !拆包 print(numberList.first!)//1 //获取最后一个元素 ...

  10. ObjectARX学习笔记

    http://blog.csdn.net/jangdong/article/category/1642265/3 http://blog.csdn.net/u011331383/article/cat ...