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

@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. LoadRunner 技巧之 IP欺骗

    IP欺骗也是也loadrunner自带的一个非常有用的功能. 需要使用ip欺骗的原因:1.当某个IP的访问过于频繁,或者访问量过大是,服务器会拒绝访问请求,这时候通过IP欺骗可以增加访问频率和访问量, ...

  2. 一步一步学Silverlight 2系列(7):全屏模式支持

    一步一步学Silverlight 2系列(7):全屏模式支持   概述 Silverlight 2 Beta 1版本发布了,无论从Runtime还是Tools都给我们带来了很多的惊喜,如支持框架语言V ...

  3. codeforces 690C3 C3. Brain Network (hard)(lca)

    题目链接: C3. Brain Network (hard) time limit per test 2 seconds memory limit per test 256 megabytes inp ...

  4. 【字符串】BZOJ上面几个AC自动机求最为字串出现次数的题目

    (一下只供自己复习用,目的是对比这几个题,所以写得不详细.需要细节的可以参考其他博主) [BZOJ3172:单词] 题目: 某人读论文,一篇论文是由许多(N)单词组成.但他发现一个单词会在论文中出现很 ...

  5. POJ2976:Dropping tests(01分数规划入门)

    In a certain course, you take n tests. If you get ai out of bi questions correct on test i, your cum ...

  6. iOS多线程 NSThread/GCD/NSOperationQueue

    无论是GCD,NSOperationQueue或是NSThread, 都没有线程安全 在需要同步的时候需要使用NSLock或者它的子类进行加锁同步 "] UTF8String], DISPA ...

  7. Oracle ORA-01033: ORACLE initialization or shutdown in progress 错误解决办法Windows版(手贱强制重启电脑的后果)

    转自:https://blog.csdn.net/rrrrroy_ha/article/details/80601497

  8. Eclipse安装配置Maven

    Eclipse安装配置Maven 1 安装配置Maven 1.1 下载Maven 从Apache网站 http://maven.apache.org/ 下载并且解压缩安装Apache Maven.   ...

  9. NYOJ1——A+B Problem NYOJ2——括号配对问题

    A+B Problem 时间限制:3000 ms  |  内存限制:65535 KB 难度:0   描述:此题为练手用题,请大家计算一下a+b的值  输入:输入两个数,a,b 输出:输出a+b的值 样 ...

  10. Cardboard对像的公共方法与属性

    一.  public Pose3D EyePose(Eye eye)/// The transformation from head to eye. 获取眼睛在头部坐标系中的局部transform: ...