(详细)Springboot 整合动态多数据源 这里有mysql(分为master 和 slave) 和oracle,根据不同路径适配不同数据源
文章目录
- Springboot 整合多动态数据源 这里有mysql(分为master 和 slave) 和oracle
- 1. 引入相关的依赖
- 2. 创建相关配置文件
- 3. 在相关目录下进行编码,不同路径会使用不同数据源
Springboot 整合多动态数据源 这里有mysql(分为master 和 slave) 和oracle
1. 引入相关的依赖
<!--动态数据源--><dependency><groupId>com.baomidou</groupId><artifactId>dynamic-datasource-spring-boot-starter</artifactId><version>${dynamic-datasource.version}</version></dependency><!-- mysql --><dependency><groupId>mysql</groupId><artifactId>mysql-connector-java</artifactId><scope>runtime</scope></dependency><!-- oracle --><dependency><groupId>com.oracle.database.jdbc</groupId><artifactId>ojdbc6</artifactId><version>${ojdbc.version}</version></dependency>
2. 创建相关配置文件
package com.aspire.sc.base.data.config;import javax.sql.DataSource;import org.springframework.beans.factory.annotation.Qualifier;//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;@Configuration
public class DataSourceConfig {@Bean(name = "master")@Qualifier("master")@Primary@ConfigurationProperties(prefix="spring.datasource.dynamic.datasource.master")public DataSource primaryDataSource(){return DataSourceBuilder.create().build();}@Bean(name = "slave")@Qualifier("slave")@ConfigurationProperties(prefix="spring.datasource.dynamic.datasource.slave")public DataSource slave(){return DataSourceBuilder.create().build();}@Bean(name = "oracleDataSource")@Qualifier("oracleDataSource")@ConfigurationProperties(prefix = "spring.datasource.dynamic.datasource.oracle")public DataSource oracleDataSource(){return DataSourceBuilder.create().build();}}
package com.aspire.sc.base.data.config;import com.aspire.common.dictenum.DictBaseEnum;
import com.aspire.common.dictenum.DictBaseItem;
import com.baomidou.mybatisplus.core.MybatisConfiguration;
import com.baomidou.mybatisplus.extension.plugins.PaginationInterceptor;
import com.baomidou.mybatisplus.extension.spring.MybatisSqlSessionFactoryBean;
import org.apache.ibatis.plugin.Interceptor;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.type.JdbcType;
import org.mybatis.spring.SqlSessionTemplate;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Primary;
import org.springframework.core.io.support.PathMatchingResourcePatternResolver;
import org.springframework.jdbc.datasource.DataSourceTransactionManager;import javax.sql.DataSource;/*** 数据库leadnews_article*/
@Configuration
public class MysqlDataSourceConfig {@Bean@Primarypublic SqlSessionFactory mysqlSqlSessionFactory(@Qualifier("master") DataSource dataSource) throws Exception {return createSqlSessionFactory(dataSource);}@Beanpublic SqlSessionFactory mysqlSlaveSqlSessionFactory(@Qualifier("slave") DataSource dataSource) throws Exception {return createSqlSessionFactory(dataSource);}private SqlSessionFactory createSqlSessionFactory(DataSource dataSource) throws Exception {MybatisSqlSessionFactoryBean sqlSessionFactory = new MybatisSqlSessionFactoryBean();sqlSessionFactory.setDataSource(dataSource);MybatisConfiguration configuration = new MybatisConfiguration();configuration.setJdbcTypeForNull(JdbcType.NULL);configuration.setMapUnderscoreToCamelCase(true);configuration.setCacheEnabled(false);// 添加分页功能PaginationInterceptor paginationInterceptor = new PaginationInterceptor();sqlSessionFactory.setPlugins(new Interceptor[]{paginationInterceptor});sqlSessionFactory.setMapperLocations(new PathMatchingResourcePatternResolver().getResources("classpath:mapper/*/*.xml"));sqlSessionFactory.setTypeHandlersPackage("com.aspire.common.constant");sqlSessionFactory.setTypeEnumsPackage("com.aspire.common.constant," +"com.aspire.sc.base.data.domain.*.pojo," +"com.aspire.sc.base.data.constant");return sqlSessionFactory.getObject();}@Bean@Primarypublic SqlSessionTemplate mysqlSqlSessionTemplate(@Qualifier("mysqlSqlSessionFactory") SqlSessionFactory sqlSessionFactory) throws Exception {return new SqlSessionTemplate(sqlSessionFactory);}@Bean(name = "masterTransactionManager")@Primarypublic DataSourceTransactionManager masterTransactionManager(@Qualifier("master") DataSource dataSource) {return new DataSourceTransactionManager(dataSource);}@Bean(name = "slaveTransactionManager")public DataSourceTransactionManager slaveTransactionManager(@Qualifier("slave") DataSource dataSource) {return new DataSourceTransactionManager(dataSource);}
}
package com.aspire.sc.base.data.config;import com.baomidou.mybatisplus.extension.spring.MybatisSqlSessionFactoryBean;
import org.apache.ibatis.session.SqlSessionFactory;
import org.mybatis.spring.SqlSessionFactoryBean;
import org.mybatis.spring.SqlSessionTemplate;
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.core.io.support.PathMatchingResourcePatternResolver;
import org.springframework.jdbc.datasource.DataSourceTransactionManager;import javax.sql.DataSource;@Configuration
//@MapperScan(basePackages = "com.aspire.sc.base.data.oracledomain.*", sqlSessionFactoryRef = "oracleSqlSessionFactory")
public class OracleDataSourceConfig {@Bean(name = "oracleSqlSessionFactory")public SqlSessionFactory oracleSqlSessionFactory(@Qualifier("oracleDataSource") DataSource dataSource) throws Exception {MybatisSqlSessionFactoryBean sqlSessionFactory = new MybatisSqlSessionFactoryBean();sqlSessionFactory.setDataSource(dataSource);sqlSessionFactory.setMapperLocations(new PathMatchingResourcePatternResolver().getResources("classpath:oraclemapper/*.xml"));return sqlSessionFactory.getObject();}@Beanpublic SqlSessionTemplate oracleSqlSessionTemplate(@Qualifier("oracleSqlSessionFactory") SqlSessionFactory sqlSessionFactory) throws Exception {return new SqlSessionTemplate(sqlSessionFactory);}@Bean(name = "oracleTransactionManager")public DataSourceTransactionManager oracleTransactionManager(@Qualifier("oracleDataSource") DataSource dataSource) {return new DataSourceTransactionManager(dataSource);}
}
启动文件加上:
import com.baomidou.dynamic.datasource.plugin.MasterSlaveAutoRoutingPlugin;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.boot.web.servlet.ServletComponentScan;
import org.springframework.cache.annotation.EnableCaching;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.transaction.annotation.EnableTransactionManagement;/*** @author wanggh*/
@EnableConfigurationProperties
@ComponentScan({"com.aspire"})
@EnableTransactionManagement(proxyTargetClass = true)@MapperScan(basePackages = {"com.aspire.sc.base.data.domain.*.mapper"}, sqlSessionFactoryRef = "mysqlSqlSessionFactory")
@MapperScan(basePackages = {"com.aspire.sc.base.data.oracledomain.mapper"}, sqlSessionFactoryRef = "oracleSqlSessionFactory")
@SpringBootApplication(exclude = {DataSourceAutoConfiguration.class})
@ServletComponentScan
@EnableCaching
public class MsBaseDataApplication {public static void main(String[] args) {SpringApplication.run(MsBaseDataApplication.class, args);}/*** 纯的读写分离环境,写操作全部是master,读操作全部是slave* 默认主库名称master,从库名称slave。* 不用加@DS注解*/@Beanpublic MasterSlaveAutoRoutingPlugin masterSlaveAutoRoutingPlugin() {return new MasterSlaveAutoRoutingPlugin();}}
3. 在相关目录下进行编码,不同路径会使用不同数据源

相关文章:
(详细)Springboot 整合动态多数据源 这里有mysql(分为master 和 slave) 和oracle,根据不同路径适配不同数据源
文章目录 Springboot 整合多动态数据源 这里有mysql(分为master 和 slave) 和oracle1. 引入相关的依赖2. 创建相关配置文件3. 在相关目录下进行编码,不同路径会使用不同数据源 Springboot 整合多动态数据源 这里有mysql(分为maste…...
mock可视化生成前端代码
介绍:mock是我们前后端分离的必要一环、ts、axios编写起来也很麻烦。我们就可以使用以下插件,来解决我们的问题。目前支持vite和webpack。(配置超级简单!) 欢迎小伙伴们提issues、我们共建。提升我们的开发体验。 vi…...
Spring Boot(6)解决ruoyi框架连续快速发送post请求时,弹出“数据正在处理,请勿重复提交”提醒的问题
一、整个前言 在基于 Ruoyi 框架进行系统开发的过程中,我们常常会遇到各种有趣且具有挑战性的问题。今天,我们就来深入探讨一个在实际开发中较为常见的问题:当连续快速发送 Post 请求时,前端会弹出 “数据正在处理,请…...
鸿蒙Harmony json转对象(1)
案例1 运行代码如下 上图的运行结果如下: 附加1 Json_msg interface 案例2 import {JSON } from kit.ArkTS; export interface commonRes {status: numberreturnJSON: ESObject;time: string } export interface returnRes {uid: stringuserType: number; }Entry Component …...
常见的RocketMQ面试题及其简要答案
以下是一些常见的RocketMQ面试题及其简要答案: 一、基础概念与架构 简述RocketMQ是什么,并说明其主要作用。 答案: RocketMQ:是阿里巴巴在2012年开源的一款分布式消息中间件,目前已经捐赠给Apache软件基金会ÿ…...
C#Object类型的索引,序列化和反序列化
前言 最近在编写一篇关于标准Mes接口框架的文章。其中有一个非常需要考究的内容时如果实现数据灵活和可使用性强。因为考虑数据灵活性,所以我一开始选取了Object类型作为数据类型,Object作为数据Value字段,String作为数据Key字段,…...
Unity3D项目开发中的资源加密详解
前言 在Unity3D游戏开发中,保护游戏资源不被非法获取和篡改是至关重要的一环。资源加密作为一种有效的技术手段,可以帮助开发者维护游戏的知识产权和安全性。本文将详细介绍Unity3D项目中如何进行资源加密,并提供相应的技术详解和代码实现。…...
微调Qwen2:7B模型,加入未知信息语料
对于QWen2这样的模型,在微调的时候,语料的投喂格式满足ChatML这样的格式!!! OpenAI - ChatML: 下面是ChatML格式的介绍: https://github.com/openai/openai-python/blob/release-v0.28.0/chatml.mdhttps://github.com/openai/openai-python/blob/release-v0.28.0/chat…...
【Ubuntu】安装SSH启用远程连接
【Ubuntu】安装OpenSSH启用远程连接 零、安装软件 使用如下代码安装OpenSSH服务端: sudo apt install openssh-server壹、启动服务 使用如下代码启动OpenSSH服务端: sudo systemctl start ssh贰、配置SSH(可跳过) 配置文件 …...
【理论】测试开发工程师进阶路线
一、腾讯与阿里的质量保证服务参考 阿里云效测试能力与架构 腾讯 WeTest 测试能力全景图 二、测试开发技术体系 1.用户端测试: Web/App 测试 Web/App 自动化测试 用户端专项测试 用户端安全测试 2.服务端测试: 接口协议与 Mock 接口自动化测试 服务端…...
【BQ3568HM开发板】如何在OpenHarmony上通过校园网的上网认证
引言 前面已经对BQ3568HM开发板进行了初步测试,后面我要实现MQTT的工作,但是遇到一个问题,就是开发板无法通过校园网的认证操作。未认证的话会,学校使用的深澜软件系统会屏蔽所有除了认证用的流量。好在我们学校使用的认证系统和…...
動態住宅IP提升網站訪問成功率
動態住宅IP通常與普通家庭用戶的網路連接相關聯。這種IP地址的特點在於,它是動態變化的,用戶在每次連接時可能會獲得不同的IP地址。這與靜態IP形成了鮮明對比,後者在連接期間保持不變。傳統上,IP地址分為住宅IP和數據中心IP兩類。…...
2024年博客之星主题创作|2024年蓝桥杯与数学建模年度总结与心得
引言 2024年,我在蓝桥杯编程竞赛和数学建模竞赛中投入了大量时间和精力,这两项活动不仅加深了我对算法、数据结构、数学建模方法的理解,还提升了我的解决实际问题的能力。从蓝桥杯的算法挑战到数学建模的复杂应用,我在这些竞赛中…...
Spring Boot/MVC
一、Spring Boot的创建 1.Spring Boot简化Spring程序的开发,使用注解和配置的方式开发 springboot内置了tomact服务器 tomact:web服务器,默认端口号8080,所以访问程序使用8080 src/main/java:Java源代码 src/main/resource:静态资源或配置文件,存放前端代码(js,css,html) s…...
由于请求的竞态问题,前端仔喜提了一个bug
在平常的开发过程中,你可能会遇到这样一个bug。 测试:我在测一个输入框搜索的功能时,告诉你通过输入框输入的内容,和最终通过输入内容搜索出来的结果对不上。 前端:我是通过调用后端接口拿到的数据,这明显…...
【Day25 LeetCode】贪心Ⅲ
一、贪心Ⅲ 1、加油站 134 这道题直接想法是采用二重循环暴力搜索,简单粗暴但是会超时,是因为以每个点为起点最坏的情况可能都要遍历完全部的序列,有大量重复的操作,那有没有优化的地方呢?有一个结论:如果…...
蓝桥杯练习日常|递归-进制转换
未完待续,,,,,, 目录 蓝桥云课760数的计算 一、递归 题目: 我的解题代码: 二、进制转换 任意进制转十进制: 十进制转换为其他进制: 进制蓝桥杯题目…...
AI Agent:深度解析与未来展望
一、AI Agent的前世:从概念到萌芽 (一)早期探索 AI Agent的概念可以追溯到20世纪50年代,早期的AI研究主要集中在简单的规则系统上,这些系统的行为是确定性的,输出由输入决定。随着时间的推移,…...
《SwinIR:使用Swin-Transformer图像恢复》学习笔记
paper:2108.10257 GitHub:GitHub - JingyunLiang/SwinIR: SwinIR: 使用 Swin Transformer 进行图像修复 (官方仓库) 目录 摘要 1、Introduction 2、Related Work 2.1 图像修复 2.2 视觉Transformer…...
如何在Nginx服务器上配置访问静态文件目录并提供文件下载功能
引言 在搭建网站的过程中,我们经常需要让访客通过URL直接访问或下载存储在服务器特定目录下的静态文件。本文将详细介绍如何在Nginx服务器环境中配置一个名为"download"的文件目录,以便用户能够通过浏览器访问并下载其中的手册和其他文档。 …...
救命!2026爆款PPT一键制作工具实测,新手也能5分钟出片,告别熬夜手搓无标题
作为常年和PPT打交道的AI博主,每天都能收到粉丝私信轰炸:“做PPT有没有捷径?”“AI能不能帮我快速出稿?”“新手零基础,半天排不出一页像样的版面”……懂的都懂!谁没为了一份PPT熬到凌晨?找模板…...
Phi-4-mini-reasoning保姆级教学:Windows WSL2环境部署全流程
Phi-4-mini-reasoning保姆级教学:Windows WSL2环境部署全流程 1. 模型介绍 Phi-4-mini-reasoning是微软推出的3.8B参数轻量级开源模型,专为数学推理、逻辑推导和多步解题等强逻辑任务设计。这个模型主打"小参数、强推理、长上下文、低延迟"的…...
手把手教你用HuggingFace+BGE模型搭建本地向量检索系统(附FAISS实战代码)
从零构建基于BGE模型的本地语义搜索系统:代码级实践指南 在信息爆炸的时代,如何快速从海量文本中精准找到相关内容?语义搜索技术正成为解决这一痛点的利器。不同于传统的关键词匹配,语义搜索能理解查询背后的意图,找到…...
最近在折腾语音端点检测的时候发现个有意思的方法——频带方差检测。这玩意儿特别适合对付环境噪声,原理简单粗暴但有效。今天咱们就手撕代码看看它怎么玩转语音段定位
基于matlab的频带方差端点检测,噪声频谱中,各频带之间变化很平缓,语音各频带之间变化较激烈。 据此特征,语音和噪声就极易区分。 计算短时频带方差,实质就是计算某一帧信号的各频带能量之间的方差。 这种以短时频带方差…...
ENVI实战:利用传感器波谱响应函数实现光谱曲线精准重采样
1. 为什么需要光谱重采样? 在遥感数据分析中,我们经常会遇到一个头疼的问题:不同传感器采集的光谱数据分辨率不一致。比如实验室用光谱仪测量的叶片反射率可能有上千个波段,而Landsat-8卫星只能获取11个波段的数据。这就好比用高清…...
找不到msvcr120.dll解决方法:2026年有效的一键修复与手动安装步骤
正玩着游戏或做着设计图,屏幕突然弹出“找不到msvcr120.dll”的提示,相信很多Windows用户都遇到过这种令人抓狂的时刻。这个错误意味着你的电脑缺少了某个软件或游戏运行所必需的“零件”。别担心,这个零件就是Microsoft Visual C 2013运行库…...
URP Scriptable Renderer Feature实战:从原理到自定义后处理
1. URP Scriptable Renderer Feature基础认知 第一次接触URP的Scriptable Renderer Feature时,我完全被各种专业术语搞晕了。后来在实际项目中反复折腾才发现,这东西本质上就是个"特效插件系统"。想象你正在玩一款射击游戏,当角色受…...
精密五金结构件配套
一、我们能为机器人行业提供什么?专注机器人非核心精密五金结构件配套,面向:工业机器人|协作机器人|人形机器人|AGV/AMR|末端执行器|减速器 / 伺服 / 模组|自动化集成工作…...
阿里小云KWS模型多语言支持实战:中英文混合唤醒
阿里小云KWS模型多语言支持实战:中英文混合唤醒 1. 引言 语音唤醒技术正在变得越来越智能,但有一个问题一直困扰着开发者:怎么让设备既能听懂中文,又能响应英文?想象一下,你对着智能音箱说"小云小云…...
3分钟破解百度网盘提取码难题:智能解析工具完全指南
3分钟破解百度网盘提取码难题:智能解析工具完全指南 【免费下载链接】baidupankey 项目地址: https://gitcode.com/gh_mirrors/ba/baidupankey 还在为百度网盘加密资源的提取码而烦恼吗?每次点击分享链接却卡在"请输入提取码"的弹窗前…...
