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

ops/2025/1/12 0:02:51/

如何搭建多数据源项目只要以下简单几步;

一. 创建核心在config.datasource文件夹里

二. 引入相对应的jar包

三. 创建数据库连接配置

四. 写逻辑代码进行验证

1.DataSource

java">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

java">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

java">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

java">package com.irootech.config.datasource;public enum DataSourceType {MYSQL,POSTGRES,PHOENIX,DORIS
}

5.DynamicDataSource

java">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

java">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

java">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

java">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


http://www.ppmy.cn/ops/149286.html

相关文章

基于Spring Boot的宠物健康顾问系统的设计与实现(LW+源码+讲解)

专注于大学生项目实战开发,讲解,毕业答疑辅导&#xff0c;欢迎高校老师/同行前辈交流合作✌。 技术范围&#xff1a;SpringBoot、Vue、SSM、HLMT、小程序、Jsp、PHP、Nodejs、Python、爬虫、数据可视化、安卓app、大数据、物联网、机器学习等设计与开发。 主要内容&#xff1a;…

【Uniapp-Vue3】uniapp创建组件

项目右键-->新建-->目录-->取名components 注意&#xff1a;名字一定要是components&#xff0c;小写 右键components文件-->新建组件-->输入组件名称-->勾选创建同名目录-->创建 注意&#xff1a;命名要么使用驼峰命名法&#xff0c;如userHeader&…

夯实前端基础之CSS篇

知识点概览 这一篇是讲CSS的&#xff0c;前面还有一篇总结HTML的哦~夯实前端基础之HTML篇&#xff0c;后面还会更新其他系列哦~ 一、必知必会 1. html中引入样式的几种方式&#xff1f; 内联样式&#xff08;html元素的style属性&#xff09;内部样式表&#xff08;style标签…

目标值子矩阵的数量

目标值子矩阵的数量 问题描述 小M最近在研究矩阵&#xff0c;他对矩阵中的子矩阵很感兴趣。给定一个矩阵 matrix 和一个目标值 target&#xff0c;他的任务是找到所有总和等于目标值的非空子矩阵的数量。子矩阵通过选择矩阵的某个矩形区域定义&#xff0c;形式为 (x1, y1, x2,…

adb remount 重新挂载 system时报错 Device or resource busy

问题&#xff1a; android 7.1.2系统中 system 属于只读权限&#xff0c;选用更改成读写权限。 操作&#xff1a;使用一下3个步骤后报错Device or resource busy。 1. adb shell 2. su 3. mount -o remount,rw /system 解决方案&#xff1a;执行以下5个步…

Ruby语言的软件开发工具

Ruby语言的软件开发工具概述 引言 Ruby是一种简单且功能强大的编程语言&#xff0c;它以优雅的语法和灵活性而闻名。自1995年首次发布以来&#xff0c;Ruby已经被广泛应用于各种开发领域&#xff0c;特别是Web开发。随着Ruby语言的普及&#xff0c;相关的开发工具也日益丰富。…

CAN总线入门指南:从原理到实践

1 CAN通信基础概述 CAN&#xff08;Controller Area Network&#xff09;是一种串行通信协议&#xff0c;由德国BOSCH公司于1986年专门为汽车分布式控制系统开发。它最初的目标是减少汽车中的线束数量&#xff0c;降低整车重量和成本。经过30多年的发展&#xff0c;CAN已经成为…

嵌入式驱动开发详解9(platform驱动)

文章目录 前言platform简介总线驱动设备设备树下的platform驱动在设备树中创建设备节点编写 platform 驱动 后续参考文献 前言 Linux 系统要考虑到驱动的可重用性&#xff0c;提出了驱动的分离与分层这样的软件思路&#xff0c;在这个思路下诞生了我们最常打交道的 platform 设…