当前位置: 首页 > news >正文

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

相关文章:

spring boot 多数据源集成mysql、postgresql、phoenix、doris等

如何搭建多数据源项目只要以下简单几步; 一. 创建核心在config.datasource文件夹里 二. 引入相对应的jar包 三. 创建数据库连接配置 四. 写逻辑代码进行验证 1.DataSource package com.irootech.config.datasource;import java.lang.annotation.*;Target({ElementType.MET…...

USB基础 -- USB 控制传输(Control Transfer)的重传机制

USB 控制传输&#xff08;Control Transfer&#xff09;的重传机制 1. 控制传输的事务结构 控制传输分为三个阶段&#xff0c;每个阶段都有自己的事务&#xff0c;并可能触发重传机制&#xff1a; 设置阶段&#xff08;Setup Stage&#xff09;&#xff1a;主机发送 8 字节的…...

云计算基础,虚拟化原理

文章目录 一、虚拟化1.1 什么是虚拟化1.2 虚拟化类型 二 、存储虚拟化2.1 存储指标2.2 存储类型2.3 存储协议2.4 RAID 三、内存 i/O虚拟化3.1 内存虚拟化基本概念地址空间转换原理内存共享与隔离原理 3.2 I/O 虚拟化基本概念模拟&#xff08;Emulation&#xff09;方式半虚拟化…...

浮点数在C语言开发中为什么不精确?

在C语言开发中&#xff0c;浮点数的精度问题是一个常见的陷阱&#xff0c;尤其是对于刚接触编程的开发者来说&#xff0c;可能会对浮点数的行为感到困惑。为什么0.1 0.2不等于0.3&#xff1f;为什么浮点数计算会出现微小误差&#xff1f;本文将从计算机底层原理出发&#xff0…...

ChatGPT网络错误如何解决

在当今的信息化社会&#xff0c;网络技术已无处不在。无论是日常生活中的在线购物&#xff0c;还是工作中的远程会议&#xff0c;网络的稳定性和可靠性成为了我们无时无刻不在关注的重要问题。而在智能技术的快速发展中&#xff0c;像ChatGPT这样的人工智能模型&#xff0c;因其…...

Vue3初学之插槽(slot)使用

在 Vue 3 中&#xff0c;插槽&#xff08;Slots&#xff09;是一种强大的内容分发机制&#xff0c;允许你在组件中定义可替换的内容区域&#xff0c;从而使组件更加通用和灵活。以下是 Vue 3 中插槽的几种常见用法&#xff1a; 默认插槽 默认插槽是最基本的插槽类型&#xff0…...

使用PVE快速创建虚拟机集群并搭建docker环境

安装Linux系统 这里以安装龙蜥操作系统AnolisOS8.9为例加以说明。 通过PVE后台上传操作系统ISO镜像。 然后在PVE上【创建虚拟机】&#xff0c;选定上传的龙蜥操作系统镜像进行系统安装。 注意&#xff1a;在安装过程中&#xff0c;要设定语言、时区、超管用户root的密码、普…...

带格式 pdf 翻译

支持 openAI 接口&#xff0c;国内 deepseek 接口兼容 openAI 接口&#xff0c; deepseek api 又非常便宜 https://pdf2zh.com/ https://github.com/Byaidu/PDFMathTranslate...

【C++】C++11(一)

目录 一、C11简介二、统一的列表初始化2.1 &#xff5b;&#xff5d;初始化2.2 std::initializer_list 三、声明3.1 auto3.2 decltype3.3 nullptr 四、范围for五、智能指针六、STL中一些变化七、右值引用和移动语义7.1 左值引用和右值引用7.2 左值引用与右值引用比较7.3 右值引…...

初学stm32 --- ADC单通道采集

目录 ADC寄存器介绍&#xff08;F1&#xff09; 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…...

【动态规划篇】欣赏概率论与镜像法融合下,别出心裁探索解答括号序列问题

本篇鸡汤&#xff1a;没有人能替你承受痛苦&#xff0c;也没有人能拿走你的坚强. 欢迎拜访&#xff1a;羑悻的小杀马特.-CSDN博客 本篇主题&#xff1a;带你解答洛谷的括号序列问题&#xff08;绝对巧解&#xff09; 制作日期&#xff1a;2025.01.10 隶属专栏&#xff1a;C/C题…...

Java(day7)

字符串练习 生成验证码 package day6; /*生成验证码 内容&#xff1a;可以是小写字母&#xff0c;也可以是大写字&#xff0c;还可以是数字 规则&#xff1a; 长度为5 内容中是四位字母&#xff0c;1位数字。 其中数字只有1位&#xff0c;但是可以出现在任意的位置。*/ impor…...

Word 转成pdf及打印的开源方案支持xp

Word转成pdf、打印的方案几乎没有免费开源的方案&#xff0c;现在提供一个通过LibreOffice实现的方案 操作依赖LibreOffice需要安装&#xff0c;点此下载老版本 5.4.7.2是最后一个支持xp的 版本如需xp要请安装此版本 LibreOffice官方介绍 LibreOffice 是一款开放源代码的自…...

LabVIEW软件侵权分析与应对

问&#xff1a;如果涉及到LabVIEW软件的仿制或模仿&#xff0c;特别是在功能、界面等方面&#xff0c;如何判断是否构成侵权&#xff1f;该如何应对&#xff1f; 答&#xff1a;LabVIEW软件的侵权问题&#xff0c;尤其是在涉及到仿制或模仿其功能、界面、设计等方面&#xff0…...

【redis】centos7下安装redis7

在CentOS 7下安装Redis7可以通过以下两种方法实现&#xff1a;手动编译安装和使用YUM进行安装。 CentOS 7系统的环境和版本&#xff1a; $ cat /etc/centos-release CentOS Linux release 7.9.2009 (Core)手动编译安装 参考官方文档&#xff1a;https://redis.io/docs/lates…...

[network]回顾:集线器(Hub)

集线器&#xff08;Hub&#xff09;的发明是计算机网络发展史上的一个重要里程碑。它最初的设计目的是为了解决局域网&#xff08;LAN&#xff09;中多台计算机共享网络资源的需求。 #mermaid-svg-OAmOmKYGAXoglS5z {font-family:"trebuchet ms",verdana,arial,sans-…...

79 Openssl3.0 RSA公钥加密数据

1 引言 最近不小心用到了openssl3.0&#xff0c;项目中需要使用rsa非对称加解密算法&#xff0c;所以把openssl3.0使用公钥加密数据的函数调用摸了一遍。 之所以记录此篇文章&#xff0c;是因为网络上大多数是openssl3.0以前的版本的函数接口&#xff0c;而openssl3.0之后已经丢…...

EFCore HasDefaultValueSql (续2 HasComputedColumnSql)

前情&#xff1a;EFCore HasDefaultValueSql EFCore HasDefaultValueSql (续1 ValueGeneratedOnAdd)-CSDN博客 小伙伴在使用 HasDefaultValueSql 时&#xff0c;对相关的 ValueGeneratedOnAdd, HasComputedColumnSql 也有了疑问&#xff1a; HasComputedColumnSql 对于计算…...

阿里巴巴TransmittableThreadLocal使用指南

前言 ThreadLocal在上下文的数据传输上非常的方便和简洁。工业实践中&#xff0c;比较常用的有三个&#xff0c;ThreadLocal、InheritableThreadLocal、TransmittableThreadLocal&#xff0c;那么他们三个之间有什么区别呢&#xff1f; 常见的三种ThreadLocal比较 ThreadLoc…...

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…...

KubeSphere 容器平台高可用:环境搭建与可视化操作指南

Linux_k8s篇 欢迎来到Linux的世界&#xff0c;看笔记好好学多敲多打&#xff0c;每个人都是大神&#xff01; 题目&#xff1a;KubeSphere 容器平台高可用&#xff1a;环境搭建与可视化操作指南 版本号: 1.0,0 作者: 老王要学习 日期: 2025.06.05 适用环境: Ubuntu22 文档说…...

IDEA运行Tomcat出现乱码问题解决汇总

最近正值期末周&#xff0c;有很多同学在写期末Java web作业时&#xff0c;运行tomcat出现乱码问题&#xff0c;经过多次解决与研究&#xff0c;我做了如下整理&#xff1a; 原因&#xff1a; IDEA本身编码与tomcat的编码与Windows编码不同导致&#xff0c;Windows 系统控制台…...

观成科技:隐蔽隧道工具Ligolo-ng加密流量分析

1.工具介绍 Ligolo-ng是一款由go编写的高效隧道工具&#xff0c;该工具基于TUN接口实现其功能&#xff0c;利用反向TCP/TLS连接建立一条隐蔽的通信信道&#xff0c;支持使用Let’s Encrypt自动生成证书。Ligolo-ng的通信隐蔽性体现在其支持多种连接方式&#xff0c;适应复杂网…...

[2025CVPR]DeepVideo-R1:基于难度感知回归GRPO的视频强化微调框架详解

突破视频大语言模型推理瓶颈,在多个视频基准上实现SOTA性能 一、核心问题与创新亮点 1.1 GRPO在视频任务中的两大挑战 ​安全措施依赖问题​ GRPO使用min和clip函数限制策略更新幅度,导致: 梯度抑制:当新旧策略差异过大时梯度消失收敛困难:策略无法充分优化# 传统GRPO的梯…...

rknn优化教程(二)

文章目录 1. 前述2. 三方库的封装2.1 xrepo中的库2.2 xrepo之外的库2.2.1 opencv2.2.2 rknnrt2.2.3 spdlog 3. rknn_engine库 1. 前述 OK&#xff0c;开始写第二篇的内容了。这篇博客主要能写一下&#xff1a; 如何给一些三方库按照xmake方式进行封装&#xff0c;供调用如何按…...

多场景 OkHttpClient 管理器 - Android 网络通信解决方案

下面是一个完整的 Android 实现&#xff0c;展示如何创建和管理多个 OkHttpClient 实例&#xff0c;分别用于长连接、普通 HTTP 请求和文件下载场景。 <?xml version"1.0" encoding"utf-8"?> <LinearLayout xmlns:android"http://schemas…...

为什么需要建设工程项目管理?工程项目管理有哪些亮点功能?

在建筑行业&#xff0c;项目管理的重要性不言而喻。随着工程规模的扩大、技术复杂度的提升&#xff0c;传统的管理模式已经难以满足现代工程的需求。过去&#xff0c;许多企业依赖手工记录、口头沟通和分散的信息管理&#xff0c;导致效率低下、成本失控、风险频发。例如&#…...

智能在线客服平台:数字化时代企业连接用户的 AI 中枢

随着互联网技术的飞速发展&#xff0c;消费者期望能够随时随地与企业进行交流。在线客服平台作为连接企业与客户的重要桥梁&#xff0c;不仅优化了客户体验&#xff0c;还提升了企业的服务效率和市场竞争力。本文将探讨在线客服平台的重要性、技术进展、实际应用&#xff0c;并…...

【ROS】Nav2源码之nav2_behavior_tree-行为树节点列表

1、行为树节点分类 在 Nav2(Navigation2)的行为树框架中,行为树节点插件按照功能分为 Action(动作节点)、Condition(条件节点)、Control(控制节点) 和 Decorator(装饰节点) 四类。 1.1 动作节点 Action 执行具体的机器人操作或任务,直接与硬件、传感器或外部系统…...

Cinnamon修改面板小工具图标

Cinnamon开始菜单-CSDN博客 设置模块都是做好的&#xff0c;比GNOME简单得多&#xff01; 在 applet.js 里增加 const Settings imports.ui.settings;this.settings new Settings.AppletSettings(this, HTYMenusonichy, instance_id); this.settings.bind(menu-icon, menu…...