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

基于SpringBoot+Druid实现多数据源:注解+编程式

前言

本博客姊妹篇

  • 基于SpringBoot+Druid实现多数据源:原生注解式
  • 基于SpringBoot+Druid实现多数据源:注解+编程式
  • 基于SpringBoot+Druid实现多数据源:baomidou多数据源

一、功能描述

  • 配置方式:配置文件中配置默认数据源,使用数据库存储其他数据源配置
  • 使用方式:使用注解切换数据源,编程式切换数据源

二、代码实现

2.1 配置

# spring配置
spring:# 数据源配置datasource:type: com.alibaba.druid.pool.DruidDataSourcedruid:driver-class-name: com.mysql.cj.jdbc.Driverurl: jdbc:mysql://127.0.0.1:3306/boot_codegen?useUnicode=true&characterEncoding=UTF8&serverTimezone=GMT%2B8&useSSL=falseusername: rootpassword: rootinitial-size: 10min-idle: 10max-active: 100max-wait: 60000time-between-eviction-runs-millis: 60000min-evictable-idle-time-millis: 300000validation-query: select 1test-while-idle: truetest-on-borrow: falsetest-on-return: falsepool-prepared-statements: truemax-pool-prepared-statement-per-connection-size: 20web-stat-filter:enabled: trueurl-pattern: /*exclusions: '*.js,*.css,*.gif,*.png,*.jpg,*.ico,/druid/*'stat-view-servlet:enabled: trueurl-pattern: /druid/*login-username: adminlogin-password: 123456filter:stat:enabled: truelog-slow-sql: trueslow-sql-millis: 1000merge-sql: truewall:enabled: trueconfig:multi-statement-allow: true

2.2 配置类

package com.qiangesoft.datasourcepro.core;import com.alibaba.druid.pool.DruidDataSource;
import com.alibaba.druid.spring.boot.autoconfigure.DruidDataSourceBuilder;
import lombok.extern.slf4j.Slf4j;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Primary;import javax.sql.DataSource;
import java.util.HashMap;
import java.util.Map;/*** 多数据源配置** @author qiangesoft* @date 2024-03-14*/
@Slf4j
@Configuration
public class DataSourceConfiguration {@Beanpublic DruidDataSource defaultDataSource() {return DruidDataSourceBuilder.create().build();}@Bean@Primarypublic DynamicDataSource dynamicDataSource(DruidDataSource defaultDataSource) {Map<Object, Object> targetDataSources = new HashMap<>();targetDataSources.put("default", defaultDataSource);return new DynamicDataSource(defaultDataSource, targetDataSources);}
}

2.3 多数据源扩展实现

package com.qiangesoft.datasourcepro.core;import com.alibaba.druid.pool.DruidDataSource;
import com.qiangesoft.datasourcepro.entity.BcgDataSource;
import com.qiangesoft.datasourcepro.service.IBcgDataSourceService;
import com.qiangesoft.datasourcepro.utils.SpringUtil;
import org.springframework.boot.jdbc.DataSourceBuilder;
import org.springframework.jdbc.datasource.lookup.AbstractRoutingDataSource;import java.util.Map;/*** 动态数据源** @author qiangesoft* @date 2024-03-14*/
public class DynamicDataSource extends AbstractRoutingDataSource {private Map<Object, Object> dynamicTargetDataSources;private Object dynamicDefaultTargetDataSource;public DynamicDataSource(DruidDataSource defaultTargetDataSource, Map<Object, Object> targetDataSources) {super.setDefaultTargetDataSource(defaultTargetDataSource);super.setTargetDataSources(targetDataSources);super.afterPropertiesSet();this.dynamicTargetDataSources = targetDataSources;this.dynamicDefaultTargetDataSource = defaultTargetDataSource;}@Overrideprotected Object determineCurrentLookupKey() {String datasourceId = DataSourceContext.getDataSource();if (datasourceId != null && this.dynamicTargetDataSources.containsKey(datasourceId)) {return datasourceId;}return null;}/*** 添加数据源** @param dataSourceId*/public void addDataSource(String dataSourceId) {if ("default".equals(dataSourceId)) {return;}int initialSize = ((DruidDataSource) dynamicDefaultTargetDataSource).getInitialSize();BcgDataSource bcgDataSource = SpringUtil.getBean(IBcgDataSourceService.class).getById(Long.valueOf(dataSourceId));if (bcgDataSource == null) {throw new RuntimeException("数据源配置不存在");}String datasourceId = String.valueOf(bcgDataSource.getId());Map<Object, Object> dynamicTargetDataSources = this.dynamicTargetDataSources;if (!dynamicTargetDataSources.containsKey(datasourceId)) {DruidDataSource dataSourceInstance = DataSourceBuilder.create().driverClassName(bcgDataSource.getDriverClassName()).url(bcgDataSource.getUrl()).username(bcgDataSource.getUsername()).password(bcgDataSource.getPassword()).type(DruidDataSource.class).build();dynamicTargetDataSources.put(datasourceId, dataSourceInstance);// 关键一步:将TargetDataSources中的连接信息放入resolvedDataSources管理super.afterPropertiesSet();}}
}

2.4 切面

package com.qiangesoft.datasourcepro.core;import lombok.extern.slf4j.Slf4j;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Pointcut;
import org.aspectj.lang.reflect.MethodSignature;
import org.springframework.core.annotation.AnnotationUtils;
import org.springframework.core.annotation.Order;
import org.springframework.stereotype.Component;import java.util.Objects;/*** 多数据源处理** @author qiangesoft* @date 2024-03-14*/
@Slf4j
@Order(1)
@Aspect
@Component
public class DataSourceAspect {/*** 切点*/@Pointcut("@annotation(com.qiangesoft.datasourcepro.core.DataSource)")public void pointCut() {}/*** 通知** @param joinPoint* @return* @throws Throwable*/@Around("pointCut()")public Object around(ProceedingJoinPoint joinPoint) throws Throwable {DataSource dataSource = this.getDataSource(joinPoint);if (dataSource == null) {DataSourceContext.setDataSource("default");} else {DataSourceContext.setDataSource(dataSource.value());}try {return joinPoint.proceed();} finally {DataSourceContext.removeDataSource();}}/*** 获取数据源** @param joinPoint* @return*/public DataSource getDataSource(ProceedingJoinPoint joinPoint) {MethodSignature signature = (MethodSignature) joinPoint.getSignature();// 方法上查找注解DataSource dataSource = AnnotationUtils.findAnnotation(signature.getMethod(), DataSource.class);if (Objects.nonNull(dataSource)) {return dataSource;}// 类上查找注解return AnnotationUtils.findAnnotation(signature.getDeclaringType(), DataSource.class);}
}

2.5 线程本地变量

package com.qiangesoft.datasourcepro.core;import com.qiangesoft.datasourcepro.utils.SpringUtil;/*** 数据源上下文** @author qiangesoft* @date 2024-03-14*/
public class DataSourceContext {/*** 线程本地变量:数据源*/private static final ThreadLocal<String> CONTEXT_HOLDER = new ThreadLocal<>();/*** 设置数据源的变量*/public static void setDataSource(String dataSourceId) {SpringUtil.getBean(DynamicDataSource.class).addDataSource(dataSourceId);CONTEXT_HOLDER.set(dataSourceId);}/*** 获得数据源的变量*/public static String getDataSource() {return CONTEXT_HOLDER.get();}/*** 清空数据源变量*/public static void removeDataSource() {CONTEXT_HOLDER.remove();}
}

2.6 使用

package com.qiangesoft.datasourcepro.service.impl;import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.qiangesoft.datasourcepro.core.DataSource;
import com.qiangesoft.datasourcepro.core.DataSourceContext;
import com.qiangesoft.datasourcepro.entity.BcgDataSource;
import com.qiangesoft.datasourcepro.mapper.BcgDataSourceMapper;
import com.qiangesoft.datasourcepro.service.IBcgDataSourceService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;import java.util.List;/*** <p>* 数据源 服务实现类* </p>** @author qiangesoft* @since 2024-03-13*/
@Slf4j
@RequiredArgsConstructor
@Service
public class BcgBcgDataSourceServiceImpl extends ServiceImpl<BcgDataSourceMapper, BcgDataSource> implements IBcgDataSourceService {@Overridepublic List<BcgDataSource> changeDataSource(String datasourceId) {try {DataSourceContext.setDataSource(datasourceId);return this.list();} catch (Exception e) {e.printStackTrace();throw new RuntimeException("数据源切换失败");} finally {DataSourceContext.removeDataSource();}}@DataSource(value = "1")@Overridepublic List<BcgDataSource> changeDataSourceByAnnotation() {return this.list();}
}

相关文章:

基于SpringBoot+Druid实现多数据源:注解+编程式

前言 本博客姊妹篇 基于SpringBootDruid实现多数据源&#xff1a;原生注解式基于SpringBootDruid实现多数据源&#xff1a;注解编程式基于SpringBootDruid实现多数据源&#xff1a;baomidou多数据源 一、功能描述 配置方式&#xff1a;配置文件中配置默认数据源&#xff0c…...

已解决org.apache.zookeeper.KeeperException.BadVersionException异常的正确解冲方法,亲测有效!!!

已解决org.apache.zookeeper.KeeperException.BadVersionException异常的正确解冲方法&#xff0c;亲测有效&#xff01;&#xff01;&#xff01; 目录 问题分析 报错原因 解决思路 解决方法 总结 博主v&#xff1a;XiaoMing_Java 问题分析 在使用Apache ZooKeeper进行…...

数据结构:堆

堆的概念 1.堆是一个完全二叉树 2.小堆(任何一个父亲<孩子),大堆(任何一个父亲>孩子) 堆的结构 物理结构:数组 逻辑结构:二叉树 #pragma once #include<assert.h> #include<iostream> typedef int HPDataType; typedef struct Heap {HPDataType* _a;int…...

CSS中三栏布局的实现

三栏布局一般指的是页面中一共有三栏&#xff0c;左右两栏宽度固定&#xff0c;中间自适应的布局&#xff0c;三栏布局的具体实现&#xff1a; 利用绝对定位&#xff0c;左右两栏设置为绝对定位&#xff0c;中间设置对应方向大小的margin的值。 .outer {position: relative;h…...

Linux搭建我的世界(MC)整合包服务器,All the Mods 9(ATM9)整合包开服教程

Linux使用MCSM面板搭建我的世界(Minecraft)整合包服务器&#xff0c;MC开服教程&#xff0c;All the Mods 9(ATM9)整合包搭建服务器的教程。 本教程使用Docker来运行mc服&#xff0c;可以方便切换不同Java版本&#xff0c;方便安装多个mc服版本。 视频教程&#xff1a;https:…...

让数据在业务间高效流转,镜舟科技与NineData完成产品兼容互认

近日&#xff0c;镜舟科技与NineData完成产品兼容测试。在经过联合测试后&#xff0c;镜舟科技旗下产品与NineData云原生智能数据管理平台完全兼容&#xff0c;整体运行高效稳定。 镜舟科技致力于帮助中国企业构建卓越的数据分析系统&#xff0c;打造独具竞争力的“数据护城河”…...

2.1HTML5基本结构

HTML5实际上不算是一种编程语言&#xff0c;而是一种标记语言。HTML5文件是由一系列成对出现的元素标签嵌套组合而成&#xff0c;这些标签以<元素名>的形式出现&#xff0c;用于标记文本内容的含义。浏览器通过元素标签解析文本内容并将结果显示在网页上&#xff0c;而元…...

设置浏览器显示小于12px以下字体

问题 我们在项目开发过程中有时候会遇到设计师给的小于12px的字体&#xff0c;IE、火狐浏览器、移动端等小于12px的字号大小还是可以正常显示的&#xff0c;但是谷歌浏览器上显示字体最小为12px&#xff0c;css设置font-size&#xff1a;10px&#xff0c;运行代码显示结果仍然…...

web蓝桥杯真题:成语学习

代码&#xff1a; //TODO 点击文字后&#xff0c;在idiom从左到右第一个空的位置加上改文字 getSingleWord(val) {let index this.idiom.indexOf() //从左往右查询空字符串this.$set(this.idiom, index, val) //响应式更新 },// TODO 校验成语是否输入正确答案 confirm…...

外包干了5天,技术明显退步。。。。。

先说一下自己的情况&#xff0c;本科生&#xff0c;19年通过校招进入南京某软件公司&#xff0c;干了接近2年的功能测试&#xff0c;今年年初&#xff0c;感觉自己不能够在这样下去了&#xff0c;长时间呆在一个舒适的环境会让一个人堕落!而我已经在一个企业干了2年的功能测试&…...

Vue:自定义消息通知组件

一、效果描述 在JS中使用一个Message函数&#xff0c;弹出一个自定义的消息框。 效果体验&#xff1a;缓若江海凝清光 二、实现方式 1.新建一个消息组件 2.新建一个js文件&#xff0c;新建一个需要导出函数 3.在函数中新建一个Vue实例&#xff0c;并将消息组件挂载上去。…...

2023 收入最高的十大编程语言

本期共享的是 —— 地球上目前已知超过 200 种可用的编程语言&#xff0c;了解哪些语言在 2023 为开发者提供更高的薪水至关重要。 过去一年里&#xff0c;我分析了来自地球各地超过 1000 万个开发职位空缺&#xff0c;辅助我们了解市场&#xff0c;以及人气最高和收入最高的语…...

Github 2024-03-11 开源项目周报 Top15

根据Github Trendings的统计&#xff0c;本周(2024-03-11统计)共有15个项目上榜。根据开发语言中项目的数量&#xff0c;汇总情况如下&#xff1a; 开发语言项目数量Python项目4TypeScript项目3Jupyter Notebook项目3C#项目1HTML项目1CSS项目1Dart项目1Lua项目1Shell项目1Rust…...

【DAY10 软考中级备考笔记】数据结构 图

数据结构 图 3月11日 – 天气&#xff1a;晴 晚上无线网络突然不能用了&#xff0c;花费好久弄这个&#xff0c;耽误了一些时间 1. 图的定义 这里需要注意完全图的定义&#xff0c;以及完全图的边数 这里需要注意连通图和连通分量的概念。 2. 图的存储结构 图有两种存储结构&a…...

java-ssm-jsp基于java的餐厅点餐系统的设计与实现

java-ssm-jsp基于java的餐厅点餐系统的设计与实现 获取源码——》公主号&#xff1a;计算机专业毕设大全...

蓝桥杯(1):python排序

1 基础 1.1 输出 1.1.1 去掉输出的空格 print("Hello","World",123,sep"") print("hello",world,123,sep) print(hello,world,123) #输出结果 #HelloWorld123 #helloworld123 #hello world 123 1.1.2 以不同的方式结尾 print(&quo…...

SpringMVC请求、响应和拦截器的使用

SpringMVC请求 RequestMapping注解 RequestMapping注解的作用是建立请求URL和处理方法之间的对应关系 RequestMapping注解可以作用在方法和类上 1. 作用在类上&#xff1a;第一级的访问目录 2. 作用在方法上&#xff1a;第二级的访问目录 3. 细节&#xff1a;路径可以不编写…...

基于springboot+layui仓库管理系统设计和实现

基于 java springbootlayui仓库管理系统设计和实现 博主介绍&#xff1a;多年java开发经验&#xff0c;专注Java开发、定制、远程、文档编写指导等,csdn特邀作者、专注于Java技术领域 作者主页 央顺技术团队 Java毕设项目精品实战案例《1000套》 欢迎点赞 收藏 ⭐留言 文末获取…...

【开源-土拨鼠充电系统】鸿蒙 HarmonyOS 4.0+微信小程序+云平台

本人自己开发的开源项目&#xff1a;土拨鼠充电系统 ✍GitHub开源项目地址&#x1f449;&#xff1a;https://github.com/cheinlu/groundhog-charging-system ✍Gitee开源项目地址&#x1f449;&#xff1a;https://gitee.com/cheinlu/groundhog-charging-system ✨踩坑不易&am…...

[抽象]工厂模式([Abstract] Factory)——创建型模式

[抽象]工厂模式——创建型模式 什么是抽象工厂&#xff1f; 抽象工厂模式是一种创建型设计模式&#xff0c;让你能够保证在客户端程序中创建一系列有依赖的对象组时&#xff0c;无需关心这些对象的类型。 具体来说&#xff1a; 对象的创建与使用分离&#xff1a; 抽象工厂模…...

多模态2025:技术路线“神仙打架”,视频生成冲上云霄

文&#xff5c;魏琳华 编&#xff5c;王一粟 一场大会&#xff0c;聚集了中国多模态大模型的“半壁江山”。 智源大会2025为期两天的论坛中&#xff0c;汇集了学界、创业公司和大厂等三方的热门选手&#xff0c;关于多模态的集中讨论达到了前所未有的热度。其中&#xff0c;…...

Leetcode 3577. Count the Number of Computer Unlocking Permutations

Leetcode 3577. Count the Number of Computer Unlocking Permutations 1. 解题思路2. 代码实现 题目链接&#xff1a;3577. Count the Number of Computer Unlocking Permutations 1. 解题思路 这一题其实就是一个脑筋急转弯&#xff0c;要想要能够将所有的电脑解锁&#x…...

最新SpringBoot+SpringCloud+Nacos微服务框架分享

文章目录 前言一、服务规划二、架构核心1.cloud的pom2.gateway的异常handler3.gateway的filter4、admin的pom5、admin的登录核心 三、code-helper分享总结 前言 最近有个活蛮赶的&#xff0c;根据Excel列的需求预估的工时直接打骨折&#xff0c;不要问我为什么&#xff0c;主要…...

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

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

多种风格导航菜单 HTML 实现(附源码)

下面我将为您展示 6 种不同风格的导航菜单实现&#xff0c;每种都包含完整 HTML、CSS 和 JavaScript 代码。 1. 简约水平导航栏 <!DOCTYPE html> <html lang"zh-CN"> <head><meta charset"UTF-8"><meta name"viewport&qu…...

NXP S32K146 T-Box 携手 SD NAND(贴片式TF卡):驱动汽车智能革新的黄金组合

在汽车智能化的汹涌浪潮中&#xff0c;车辆不再仅仅是传统的交通工具&#xff0c;而是逐步演变为高度智能的移动终端。这一转变的核心支撑&#xff0c;来自于车内关键技术的深度融合与协同创新。车载远程信息处理盒&#xff08;T-Box&#xff09;方案&#xff1a;NXP S32K146 与…...

C/C++ 中附加包含目录、附加库目录与附加依赖项详解

在 C/C 编程的编译和链接过程中&#xff0c;附加包含目录、附加库目录和附加依赖项是三个至关重要的设置&#xff0c;它们相互配合&#xff0c;确保程序能够正确引用外部资源并顺利构建。虽然在学习过程中&#xff0c;这些概念容易让人混淆&#xff0c;但深入理解它们的作用和联…...

MySQL 主从同步异常处理

阅读原文&#xff1a;https://www.xiaozaoshu.top/articles/mysql-m-s-update-pk MySQL 做双主&#xff0c;遇到的这个错误&#xff1a; Could not execute Update_rows event on table ... Error_code: 1032是 MySQL 主从复制时的经典错误之一&#xff0c;通常表示&#xff…...

用鸿蒙HarmonyOS5实现国际象棋小游戏的过程

下面是一个基于鸿蒙OS (HarmonyOS) 的国际象棋小游戏的完整实现代码&#xff0c;使用Java语言和鸿蒙的Ability框架。 1. 项目结构 /src/main/java/com/example/chess/├── MainAbilitySlice.java // 主界面逻辑├── ChessView.java // 游戏视图和逻辑├── …...

P10909 [蓝桥杯 2024 国 B] 立定跳远

# P10909 [蓝桥杯 2024 国 B] 立定跳远 ## 题目描述 在运动会上&#xff0c;小明从数轴的原点开始向正方向立定跳远。项目设置了 $n$ 个检查点 $a_1, a_2, \cdots , a_n$ 且 $a_i \ge a_{i−1} > 0$。小明必须先后跳跃到每个检查点上且只能跳跃到检查点上。同时&#xff0…...