spring boot 多数据源集成mysql、postgresql、phoenix、doris等
如何搭建多数据源项目只要以下简单几步;
一. 创建核心在config.datasource文件夹里
二. 引入相对应的jar包
三. 创建数据库连接配置
四. 写逻辑代码进行验证
1.DataSource
package com.irootech.config.datasource;import java.lang.annotation.*;@Target({ElementType.METHOD, ElementType.TYPE})
@Retention(RetentionPolicy.RUNTIME)
@Documented
public @interface DataSource {DataSourceType value() default DataSourceType.MYSQL;
}
2.DataSourceAspect
package com.irootech.config.datasource;import org.aspectj.lang.annotation.After;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.springframework.stereotype.Component;@Aspect
@Component
public class DataSourceAspect {@Before("@annotation(dataSource)")public void changeDataSource(DataSource dataSource) {DataSourceType dataSourceType = dataSource.value();DynamicDataSource.setDataSourceType(dataSourceType);}@After("@annotation(dataSource)")public void restoreDataSource(DataSource dataSource) {DynamicDataSource.clearDataSourceType();}
}
3.DataSourceConfig
package com.irootech.config.datasource;import com.baomidou.mybatisplus.extension.spring.MybatisSqlSessionFactoryBean;
import org.apache.ibatis.session.SqlSessionFactory;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.boot.jdbc.DataSourceBuilder;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Primary;
import org.springframework.jdbc.datasource.DataSourceTransactionManager;import javax.sql.DataSource;
import java.util.HashMap;
import java.util.Map;@Configuration
@MapperScan(basePackages = "com.irootech.mapper", sqlSessionFactoryRef = "sqlSessionFactory")
public class DataSourceConfig {@Bean(name = "mysqlDataSource")@ConfigurationProperties(prefix = "spring.datasource.mysql")public DataSource mysqlDataSource() {return DataSourceBuilder.create().build();}@Bean(name = "postgresDataSource")@ConfigurationProperties(prefix = "spring.datasource.postgres")public DataSource postgresDataSource() {return DataSourceBuilder.create().build();}@Bean(name = "phoenixDataSource")@ConfigurationProperties(prefix = "spring.datasource.phoenix")public DataSource phoenixDataSource() {return DataSourceBuilder.create().build();}@Bean(name = "dorisDataSource")@ConfigurationProperties(prefix = "spring.datasource.doris")public DataSource dorisDataSource() {return DataSourceBuilder.create().build();}@Bean@Primarypublic DynamicDataSource dataSource(@Qualifier("mysqlDataSource") DataSource mysqlDataSource,@Qualifier("postgresDataSource") DataSource postgresDataSource,@Qualifier("phoenixDataSource") DataSource phoenixDataSource,@Qualifier("dorisDataSource") DataSource dorisDataSource) {Map<Object, Object> targetDataSources = new HashMap<>();targetDataSources.put(DataSourceType.MYSQL, mysqlDataSource);targetDataSources.put(DataSourceType.POSTGRES, postgresDataSource);targetDataSources.put(DataSourceType.PHOENIX, phoenixDataSource);targetDataSources.put(DataSourceType.DORIS, dorisDataSource);return new DynamicDataSource(mysqlDataSource,targetDataSources);}@Beanpublic SqlSessionFactory sqlSessionFactory(@Qualifier("dataSource") DataSource dataSource) throws Exception {MybatisSqlSessionFactoryBean bean = new MybatisSqlSessionFactoryBean();bean.setDataSource(dataSource);return bean.getObject();}@Beanpublic DataSourceTransactionManager transactionManager(@Qualifier("dataSource") DataSource dataSource) {return new DataSourceTransactionManager(dataSource);}
}
4.DataSourceType
package com.irootech.config.datasource;public enum DataSourceType {MYSQL,POSTGRES,PHOENIX,DORIS
}
5.DynamicDataSource
package com.irootech.config.datasource;
import org.springframework.jdbc.datasource.lookup.AbstractRoutingDataSource;import javax.sql.DataSource;
import java.util.Map;public class DynamicDataSource extends AbstractRoutingDataSource {private static final ThreadLocal<DataSourceType> contextHolder = new ThreadLocal<>();public DynamicDataSource(DataSource defaultTargetDataSource, Map<Object, Object> targetDataSources) {super.setDefaultTargetDataSource(defaultTargetDataSource);super.setTargetDataSources(targetDataSources);super.afterPropertiesSet();}@Overrideprotected Object determineCurrentLookupKey() {return contextHolder.get();}public static void setDataSourceType(DataSourceType dataSourceType) {contextHolder.set(dataSourceType);}public static DataSourceType getDataSourceType() {return contextHolder.get();}public static void clearDataSourceType() {contextHolder.remove();}
}
6. DataSourceMapper
package com.irootech.mapper;import com.irootech.config.datasource.DataSource;
import com.irootech.config.datasource.DataSourceType;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Select;import java.util.List;@Mapper
public interface DataSourceMapper {@DataSource(DataSourceType.MYSQL)@Select("select * from data_source")List<Object> getDataSourceMysql();@DataSource(DataSourceType.PHOENIX)@Select("select * from data_source")List<Object> getDataSourcePhoenix();
}
7. DataSourceServiceImpl
package com.irootech.service.impl;import com.irootech.mapper.DataSourceMapper;
import com.irootech.service.DataSourceService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;import java.util.List;@Service
public class DataSourceServiceImpl implements DataSourceService {@Autowiredprivate DataSourceMapper dataSourceMapper;@Overridepublic List<Object> getDataSourceMysql() {return dataSourceMapper.getDataSourceMysql();}@Overridepublic List<Object> getDataSourcePhoenix() {return dataSourceMapper.getDataSourcePhoenix();}
}
8. WebController
package com.irootech.web;import com.irootech.service.DataSourceService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;import java.util.List;@RestController
@RequestMapping("/web_controller")
public class WebController {@Autowiredprivate DataSourceService dataSourceService;@GetMapping(value ="/message")public String message() {List<Object> dataSourceMysqlList = dataSourceService.getDataSourceMysql();System.out.println(dataSourceMysqlList);List<Object> dataSourcePhoenixList = dataSourceService.getDataSourcePhoenix();System.out.println(dataSourcePhoenixList);return "WebController created";}
}
下载demo地址:https://download.csdn.net/download/u013772876/90233957
相关文章:
![](https://i-blog.csdnimg.cn/direct/98823fc2d0994b18972c7571b72d85d8.jpeg)
spring boot 多数据源集成mysql、postgresql、phoenix、doris等
如何搭建多数据源项目只要以下简单几步; 一. 创建核心在config.datasource文件夹里 二. 引入相对应的jar包 三. 创建数据库连接配置 四. 写逻辑代码进行验证 1.DataSource package com.irootech.config.datasource;import java.lang.annotation.*;Target({ElementType.MET…...
![](https://www.ngui.cc/images/no-images.jpg)
USB基础 -- USB 控制传输(Control Transfer)的重传机制
USB 控制传输(Control Transfer)的重传机制 1. 控制传输的事务结构 控制传输分为三个阶段,每个阶段都有自己的事务,并可能触发重传机制: 设置阶段(Setup Stage):主机发送 8 字节的…...
![](https://i-blog.csdnimg.cn/direct/80d8da5c91db4884b70bb15f993148b4.png)
云计算基础,虚拟化原理
文章目录 一、虚拟化1.1 什么是虚拟化1.2 虚拟化类型 二 、存储虚拟化2.1 存储指标2.2 存储类型2.3 存储协议2.4 RAID 三、内存 i/O虚拟化3.1 内存虚拟化基本概念地址空间转换原理内存共享与隔离原理 3.2 I/O 虚拟化基本概念模拟(Emulation)方式半虚拟化…...
![](https://www.ngui.cc/images/no-images.jpg)
浮点数在C语言开发中为什么不精确?
在C语言开发中,浮点数的精度问题是一个常见的陷阱,尤其是对于刚接触编程的开发者来说,可能会对浮点数的行为感到困惑。为什么0.1 0.2不等于0.3?为什么浮点数计算会出现微小误差?本文将从计算机底层原理出发࿰…...
![](https://www.ngui.cc/images/no-images.jpg)
ChatGPT网络错误如何解决
在当今的信息化社会,网络技术已无处不在。无论是日常生活中的在线购物,还是工作中的远程会议,网络的稳定性和可靠性成为了我们无时无刻不在关注的重要问题。而在智能技术的快速发展中,像ChatGPT这样的人工智能模型,因其…...
![](https://i-blog.csdnimg.cn/direct/8a7db5967b844785b52c703ded90f51a.png)
Vue3初学之插槽(slot)使用
在 Vue 3 中,插槽(Slots)是一种强大的内容分发机制,允许你在组件中定义可替换的内容区域,从而使组件更加通用和灵活。以下是 Vue 3 中插槽的几种常见用法: 默认插槽 默认插槽是最基本的插槽类型࿰…...
![](https://i-blog.csdnimg.cn/direct/ea6ffaa91686455da162bd89dc4caaa9.png)
使用PVE快速创建虚拟机集群并搭建docker环境
安装Linux系统 这里以安装龙蜥操作系统AnolisOS8.9为例加以说明。 通过PVE后台上传操作系统ISO镜像。 然后在PVE上【创建虚拟机】,选定上传的龙蜥操作系统镜像进行系统安装。 注意:在安装过程中,要设定语言、时区、超管用户root的密码、普…...
![](https://i-blog.csdnimg.cn/direct/4b2b18716b564d6f9596331431965bff.png)
带格式 pdf 翻译
支持 openAI 接口,国内 deepseek 接口兼容 openAI 接口, deepseek api 又非常便宜 https://pdf2zh.com/ https://github.com/Byaidu/PDFMathTranslate...
![](https://i-blog.csdnimg.cn/blog_migrate/bcd42a08cb30488eed29584d8a6c7501.gif)
【C++】C++11(一)
目录 一、C11简介二、统一的列表初始化2.1 {}初始化2.2 std::initializer_list 三、声明3.1 auto3.2 decltype3.3 nullptr 四、范围for五、智能指针六、STL中一些变化七、右值引用和移动语义7.1 左值引用和右值引用7.2 左值引用与右值引用比较7.3 右值引…...
![](https://i-blog.csdnimg.cn/direct/a6154f759bde4746a3f9fb9cf36fd579.png)
初学stm32 --- ADC单通道采集
目录 ADC寄存器介绍(F1) ADC控制寄存器 1(ADC_CR1) ADC控制寄存器 2(ADC_CR2) ADC采样时间寄存器1(ADC_SMPR1) ADC采样时间寄存器2(ADC_SMPR2) ADC规则序列寄存器 1(ADC_SQR1) ADC规则序列寄存器 2(ADC_SQR2) ADC规则序列寄存器 3(ADC_SQR3) AD…...
![](https://i-blog.csdnimg.cn/direct/7de4d9daf3a941b48950ea249e369d14.png)
【动态规划篇】欣赏概率论与镜像法融合下,别出心裁探索解答括号序列问题
本篇鸡汤:没有人能替你承受痛苦,也没有人能拿走你的坚强. 欢迎拜访:羑悻的小杀马特.-CSDN博客 本篇主题:带你解答洛谷的括号序列问题(绝对巧解) 制作日期:2025.01.10 隶属专栏:C/C题…...
![](https://i-blog.csdnimg.cn/direct/6829a8cb3f6049c8aebf031cddbc4378.png)
Java(day7)
字符串练习 生成验证码 package day6; /*生成验证码 内容:可以是小写字母,也可以是大写字,还可以是数字 规则: 长度为5 内容中是四位字母,1位数字。 其中数字只有1位,但是可以出现在任意的位置。*/ impor…...
![](https://www.ngui.cc/images/no-images.jpg)
Word 转成pdf及打印的开源方案支持xp
Word转成pdf、打印的方案几乎没有免费开源的方案,现在提供一个通过LibreOffice实现的方案 操作依赖LibreOffice需要安装,点此下载老版本 5.4.7.2是最后一个支持xp的 版本如需xp要请安装此版本 LibreOffice官方介绍 LibreOffice 是一款开放源代码的自…...
![](https://i-blog.csdnimg.cn/img_convert/bfe0127353ce5c2acb94ab100f284b9f.png)
LabVIEW软件侵权分析与应对
问:如果涉及到LabVIEW软件的仿制或模仿,特别是在功能、界面等方面,如何判断是否构成侵权?该如何应对? 答:LabVIEW软件的侵权问题,尤其是在涉及到仿制或模仿其功能、界面、设计等方面࿰…...
![](https://www.ngui.cc/images/no-images.jpg)
【redis】centos7下安装redis7
在CentOS 7下安装Redis7可以通过以下两种方法实现:手动编译安装和使用YUM进行安装。 CentOS 7系统的环境和版本: $ cat /etc/centos-release CentOS Linux release 7.9.2009 (Core)手动编译安装 参考官方文档:https://redis.io/docs/lates…...
![](https://www.ngui.cc/images/no-images.jpg)
[network]回顾:集线器(Hub)
集线器(Hub)的发明是计算机网络发展史上的一个重要里程碑。它最初的设计目的是为了解决局域网(LAN)中多台计算机共享网络资源的需求。 #mermaid-svg-OAmOmKYGAXoglS5z {font-family:"trebuchet ms",verdana,arial,sans-…...
![](https://www.ngui.cc/images/no-images.jpg)
79 Openssl3.0 RSA公钥加密数据
1 引言 最近不小心用到了openssl3.0,项目中需要使用rsa非对称加解密算法,所以把openssl3.0使用公钥加密数据的函数调用摸了一遍。 之所以记录此篇文章,是因为网络上大多数是openssl3.0以前的版本的函数接口,而openssl3.0之后已经丢…...
![](https://www.ngui.cc/images/no-images.jpg)
EFCore HasDefaultValueSql (续2 HasComputedColumnSql)
前情:EFCore HasDefaultValueSql EFCore HasDefaultValueSql (续1 ValueGeneratedOnAdd)-CSDN博客 小伙伴在使用 HasDefaultValueSql 时,对相关的 ValueGeneratedOnAdd, HasComputedColumnSql 也有了疑问: HasComputedColumnSql 对于计算…...
![](https://i-blog.csdnimg.cn/direct/74649bf27e494a62abfcf5e733fba449.png)
阿里巴巴TransmittableThreadLocal使用指南
前言 ThreadLocal在上下文的数据传输上非常的方便和简洁。工业实践中,比较常用的有三个,ThreadLocal、InheritableThreadLocal、TransmittableThreadLocal,那么他们三个之间有什么区别呢? 常见的三种ThreadLocal比较 ThreadLoc…...
![](https://www.ngui.cc/images/no-images.jpg)
ubuntu20下编译linux1.0 (part1)
author: hjjdebug date: 2025年 01月 09日 星期四 15:56:15 CST description: ubuntu20下编译linux1.0 (part1) 该博客记录了新gcc编译旧代码可能碰到的问题和解决办法, 可留作参考 操作环境: ubuntu20 $ gcc --version gcc (Ubuntu 9.4.0-1ubuntu1~20.04.2) 9.4.0 $ as --vers…...
![](https://i-blog.csdnimg.cn/img_convert/7f2d984876df26967b09b948deb14e0a.png)
欧拉公式和傅里叶变换
注:英文引文机翻,未校。 中文引文未整理去重,如有异常,请看原文。 Euler’s Formula and Fourier Transform Posted byczxttkl October 7, 2018 Euler’s formula states that e i x cos x i sin x e^{ix} \cos{x} i …...
![](https://i-blog.csdnimg.cn/direct/04d31fa600584d3886b77b92ab5c6122.png)
Jenkins内修改allure报告名称
背景: 最近使用Jenkins搭建自动化测试环境时,使用Jenkins的allure插件生成的报告,一直显示默认ALLURE REPORT,想自定义成与项目关联的名称,如图所示,很明显自定义名称显得高大上些,之前…...
![](https://i-blog.csdnimg.cn/direct/22456d20c2164811bab1d7b50a65bf5b.png)
30天开发操作系统 第 12 天 -- 定时器 v1.0
前言 定时器(Timer)对于操作系统非常重要。它在原理上却很简单,只是每隔一段时间(比如0.01秒)就发送一个中断信号给CPU。幸亏有了定时器,CPU才不用辛苦地去计量时间。……如果没有定时器会怎么样呢?让我们想象一下吧。 假如CPU看不到定时器而仍想计量时…...
![](https://www.ngui.cc/images/no-images.jpg)
Ubuntu | PostgreSQL | 解决 ERROR: `xmllint` is missing on your system.
解决 sudo apt install apt-file sudo apt-file updatesudo apt-file search xmllint sudo apt install libxml2-utils执行 # postgres源码安装包解压文件夹中 make install make install问题 make -C src install make[2]: Entering directory /home/postgres/postgresql-1…...
![](https://i-blog.csdnimg.cn/direct/5dfd114a2b1c4512ae15149d2806c3df.png)
uniapp使用chooseLocation安卓篇
本文章全部以高德地图为例 代码 <view class"bottom"><button click"choose">定位</button> </view> choose() {uni.chooseLocation({success: function(res) {console.log(位置名称: res.name);console.log(详细地…...
![](https://www.ngui.cc/images/no-images.jpg)
《PC 上的开源神经网络多模态模型:开启智能交互新时代》
《PC 上的开源神经网络多模态模型:开启智能交互新时代》 一、引言二、多模态模型基础剖析(一)核心概念解读(二)技术架构探秘 三、开源多模态模型的独特魅力(一)开源优势尽显(二&…...
![](https://i-blog.csdnimg.cn/direct/0a47b689d0a2499ab67fbc7e3a377fd7.png)
Apache JMeter 压力测试使用说明
文章目录 一、 安装步骤步骤一 下载相关的包步骤二 安装 Jmeter步骤三 设置 Jmeter 工具语言类型为中文 二、使用工具2.1 创建测试任务步骤一 创建线程组步骤二 创建 HTTP 请求 2.2 配置 HTTP 默认参数添加 HTTP消息头管理器HTTP请求默认值 2.3 添加 查看结果监听器2.4 查看结果…...
![](https://i-blog.csdnimg.cn/direct/9787647fc3ef47d09987f4ceb6202b13.png)
腾讯云AI代码助手编程挑战赛-知识百科AI
作品简介 知识百科AI这一编程主要用于对于小朋友的探索力的开发,让小朋友在一开始就对学习具有探索精神。在信息化时代下,会主动去学习自己认知以外的知识,同时丰富了眼界,开拓了新的知识。同时催生了在大数据时代下的信息共享化…...
![](https://i-blog.csdnimg.cn/direct/ad9f17b3ac6f40f988fa8c885e5e6163.png)
【SpringAOP】Spring AOP 底层逻辑:切点表达式与原理简明阐述
前言 🌟🌟本期讲解关于spring aop的切面表达式和自身实现原理介绍~~~ 🌈感兴趣的小伙伴看一看小编主页:GGBondlctrl-CSDN博客 🔥 你的点赞就是小编不断更新的最大动力 &am…...
![](https://i-blog.csdnimg.cn/direct/d356be18697e439db09e902751d2b9ac.png)
HTTP-响应协议
HTTP的响应过程? 浏览器请求数据--》web服务器过程:请求过程 web服务器将响应数据-》到浏览器:响应过程 响应数据有哪些内容? 1.和请求数据类似。 2. 响应体中存储着web服务器返回给浏览器的响应数据。并且注意响应头和响应体之间…...
![](/images/no-images.jpg)
中企动力邮箱登陆首页/成都优化官网公司
上周,Mike和我们分享了他在数据库设计方面的部分经验和SQL的一些注意点。 一、数据库设计 1.树形结构数据 :分为邻接模型和 物化路径模型 1.1树形数据,需要解决的问题是: 如何将数据高效地以树形的形式展现给用户&…...
![](https://img-blog.csdnimg.cn/20210401083714304.png?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L215V29ybGQwMDE=,size_16,color_FFFFFF,t_70)
门户网站开发过程/百度客服人工服务电话
将windos环境下的ssh公钥放到服务器上就可以 没有公钥就生成 $ ssh-keygen -t rsa -b 4096写入到服务器端authorized_keys文件里(没有就自己创建)...
![](https://img-blog.csdnimg.cn/20200629182251721.png?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L0NTRE4yNDk3MjQyMDQx,size_16,color_FFFFFF,t_70)
沈阳 网站建设/网站网页设计
前言:打脸了,前脚刚说过要跟Servlet正式告别。结果最近的面试被问到了同一个Servlet可不可以被映射到多个URL上,也就是如何用一个Servlet实现多个功能。 前置知识: Servlet容器如何处理请求资源路径? 1、这个地址 ht…...
![](/images/no-images.jpg)
企业网站的类型包括/长沙seo网站推广
谈判中最重要也是最艰难的一幕就是谈判中的较量,前期哪怕做了很多准备,如果在这个实际运作中把握不住,也往往前功尽弃。 现实生活中,我们基本上每天都在进行谈判,只不过是形式有所不同而已,谈判也往往涉及到…...
![](https://img-blog.csdnimg.cn/15bd554229d5432c81e5a739e700027c.png?x-oss-process=image/watermark,type_ZHJvaWRzYW5zZmFsbGJhY2s,shadow_50,text_Q1NETiBA5ZKV5Zmc5ZKV5ZmcODg=,size_20,color_FFFFFF,t_70,g_se,x_16#pic_center)
微信公众号微网站制作/自助建站系统哪个好
最近,开源项目库并不安宁。前有 Python 官方存储库 PyPI 成“祸源”,后有 JavaScript 的官方软件包管理器 NPM 感染挖矿病毒。 Node Package Manager(NPM)作为 JavaScript 的官方软件包管理器,一直广受开发者欢迎。但…...
![](/images/no-images.jpg)
成全视频免费观看在线看电视剧/网站优化推广哪家好
最近偶有开发同事咨询 PostgreSQL 日期函数,对日期处理不太熟悉,今天详细看了下手册的日期函数,整理如下,供参考。 一 取当前日期的函数 --取当前时间skytf> select now(); now ---------------…...