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

SpringBoot实现mysql与clickhouse多数据源

一、我们来实现一个mysql与clickhouse多数据源配置

二、数据源配置

# 指定服务名称
spring:application:name: demobigdatadatasource:driver-class-name: com.mysql.jdbc.Driverurl: jdbc:mysql://127.0.0.1:3306/db?createDatabaseIfNotExist=true&useUnicode=true&characterEncoding=utf-8&serverTimezone=Asia/Shanghai&useSSL=false&zeroDateTimeBehavior=convertToNull&allowMultiQueries=true&useAffectedRows=true&allowPublicKeyRetrieval=trueusername: rootpassword: 123456type: com.alibaba.druid.pool.DruidDataSourcedruid:# 数据源  clickhouseclickhouse:driverClassName: com.clickhouse.jdbc.ClickHouseDriverurl: jdbc:clickhouse://192.168.42.142:8123/bigdatausername: defaultpassword: 123456initialSize: 10maxActive: 100minIdle: 10maxWait: 6000

三、MysqlDuridConfig


import javax.sql.DataSource;import org.apache.ibatis.session.SqlSessionFactory;
import org.mybatis.spring.SqlSessionFactoryBean;
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.Resource;
import org.springframework.core.io.support.PathMatchingResourcePatternResolver;
import org.springframework.jdbc.datasource.DataSourceTransactionManager;import com.alibaba.druid.pool.DruidDataSource;@Configuration
@MapperScan(basePackages = "org.demoflowable.mysql.dao")
public class MysqlDuridConfig {@javax.annotation.ResourceMysqlJdbcParamConfig mysqlJdbcParamConfig;@Bean("mysqlDataSource")@Primarypublic DataSource mysqlDataSource() {DruidDataSource datasource = new DruidDataSource();datasource.setUrl(mysqlJdbcParamConfig.getUrl());datasource.setDriverClassName(mysqlJdbcParamConfig.getDriverClassName());datasource.setUsername(mysqlJdbcParamConfig.getUsername());datasource.setPassword(mysqlJdbcParamConfig.getPassword());return datasource;}@Bean("mysqlTransactionManager")@Primarypublic DataSourceTransactionManager getDataSourceTransactionManager(@Qualifier("mysqlDataSource") DataSource dataSource) {return new DataSourceTransactionManager(dataSource);}@Bean("sqlSessionFactory")@Primarypublic SqlSessionFactory getSqlSessionManager(@Qualifier("mysqlDataSource") DataSource dataSource)throws Exception {SqlSessionFactoryBean bean = new SqlSessionFactoryBean();bean.setDataSource(dataSource);Resource[] resource = new PathMatchingResourcePatternResolver().getResources("classpath:META-INF/mapper/mysql/*.xml");bean.setMapperLocations(resource);Resource configLocation=new PathMatchingResourcePatternResolver().getResource("classpath:META-INF/spring/mybatis-config.xml");bean.setConfigLocation(configLocation);return bean.getObject();}
}

四、MysqlJdbcParamConfig


import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Component;@Component
@ConfigurationProperties(prefix = "spring.datasource")
public class MysqlJdbcParamConfig {private String driverClassName;private String url;private String username;private String password;public String getDriverClassName() {return driverClassName;}public void setDriverClassName(String driverClassName) {this.driverClassName = driverClassName;}public String getUrl() {return url;}public void setUrl(String url) {this.url = url;}public String getUsername() {return username;}public void setUsername(String username) {this.username = username;}public String getPassword() {return password;}public void setPassword(String password) {this.password = password;}
}

五、ClickHouseJdbcParamConfig


import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Component;@Component
@ConfigurationProperties(prefix = "spring.datasource.druid.clickhouse")
public class ClickHouseJdbcParamConfig {private String driverClassName;private String url;private Integer initialSize;private Integer maxActive;private Integer minIdle;private Integer maxWait;private String username;private String password;public String getDriverClassName() {return driverClassName;}public void setDriverClassName(String driverClassName) {this.driverClassName = driverClassName;}public String getUrl() {return url;}public void setUrl(String url) {this.url = url;}public Integer getInitialSize() {return initialSize;}public void setInitialSize(Integer initialSize) {this.initialSize = initialSize;}public Integer getMaxActive() {return maxActive;}public void setMaxActive(Integer maxActive) {this.maxActive = maxActive;}public Integer getMinIdle() {return minIdle;}public void setMinIdle(Integer minIdle) {this.minIdle = minIdle;}public Integer getMaxWait() {return maxWait;}public void setMaxWait(Integer maxWait) {this.maxWait = maxWait;}public String getUsername() {return username;}public void setUsername(String username) {this.username = username;}public String getPassword() {return password;}public void setPassword(String password) {this.password = password;}
}

六、ClickHouseConfig 


import org.apache.ibatis.session.SqlSessionFactory;
import org.mybatis.spring.SqlSessionFactoryBean;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.io.Resource;
import org.springframework.core.io.support.PathMatchingResourcePatternResolver;
import org.springframework.jdbc.datasource.DataSourceTransactionManager;import com.alibaba.druid.pool.DruidDataSource;@Configuration
@ConfigurationProperties
@MapperScan(basePackages = "org.demoflowable.clickhouse.dao", sqlSessionFactoryRef = "clickhouseSqlSessionFactory")
public class ClickHouseConfig {@javax.annotation.Resourceprivate ClickHouseJdbcParamConfig jdbcParamConfig;@Bean("clickDataSource")public DataSource dataSource() {DruidDataSource datasource = new DruidDataSource();datasource.setUrl(jdbcParamConfig.getUrl());datasource.setDriverClassName(jdbcParamConfig.getDriverClassName());datasource.setInitialSize(jdbcParamConfig.getInitialSize());datasource.setMinIdle(jdbcParamConfig.getMinIdle());datasource.setMaxActive(jdbcParamConfig.getMaxActive());datasource.setMaxWait(jdbcParamConfig.getMaxWait());datasource.setUsername(jdbcParamConfig.getUsername());datasource.setPassword(jdbcParamConfig.getPassword());return datasource;}@Bean("clickTransactionManager")public DataSourceTransactionManager getDataSourceTransactionManager(@Qualifier("clickDataSource") DataSource dataSource) {return new DataSourceTransactionManager(dataSource);}@Bean("clickhouseSqlSessionFactory")public SqlSessionFactory getSqlSessionManager(@Qualifier("clickDataSource") DataSource dataSource)throws Exception {SqlSessionFactoryBean bean = new SqlSessionFactoryBean();bean.setDataSource(dataSource);Resource[] resource = new PathMatchingResourcePatternResolver().getResources("classpath:META-INF/mapper/clickhouse/*.xml");bean.setMapperLocations(resource);return bean.getObject();}
}

七、启动类Application


import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;/*** @Description: 启动类*/
@SpringBootApplication
@ComponentScan({ "org.demoflowable" })
public class Application {/*** @Title: main* @Description: 启动类* @param args* @date 2023-11-08 01:49:23*/public static void main(String[] args) {SpringApplication.run(Application.class, args);}}

相关文章:

SpringBoot实现mysql与clickhouse多数据源

一、我们来实现一个mysql与clickhouse多数据源配置 二、数据源配置 # 指定服务名称 spring:application:name: demobigdatadatasource:driver-class-name: com.mysql.jdbc.Driverurl: jdbc:mysql://127.0.0.1:3306/db?createDatabaseIfNotExisttrue&useUnicodetrue&…...

为什么是LangChain?

文章目录 一、前言二、认识langchain1. langchain的主要组成2. 总览LangChain2. LangChain的六大核心模块1. Models:模型统一接口2. Prompts:管理 LLM 输入3. Chains:将 LLM 与其他组件相结合,执行多个chain4. Indexes&#xff1a…...

Labview的分支判断

和其他的编程语言一样的。都会有switch,case, if ,else; 再combo box中实现 再后台程序中对应的写上逻辑就好了。...

蓝桥杯双周赛算法心得——串门(双链表数组+双dfs)

大家好,我是晴天学长,树和dfs的结合,其邻接表的存图方法也很重要。需要的小伙伴可以关注支持一下哦!后续会继续更新的。💪💪💪 1) .串门 2) .算法思路 串门(怎么存图很关键&#xf…...

mysql 配置主从复制 及 Slave_SQL_Running = no问题排查

一、配置主数据库 1、在mysql 配置文件my.cnf中设置主数据库配置 server-id1 //唯一的标示符 log-binmysql-bin //开启二进制日志2、重启数据库 3、安全规范的写法是新建一个用户给这个用户复制的权限(直接用root也可以不建议) CREATE USER repl% IDEN…...

再获5G RedCap能力认证!宏电5G RedCap工业智能网关通过中国联通5G物联网OPENLAB开放实验室测试验证

​近日,中国联通5G物联网OPENLAB开放实验室携手宏电股份完成5G RedCap工业智能网关端到端的测试验证,并颁发OPENLAB实验室面向RedCap终端的认证证书,为RedCap产业规模推广、全行业赋能打下坚实基础。 中国联通5G物联网OPENLAB开放实验室是中国…...

牛客--汽水瓶python

某商店规定:三个空汽水瓶可以换一瓶汽水,允许向老板借空汽水瓶(但是必须要归还)。 小张手上有n个空汽水瓶,她想知道自己最多可以喝到多少瓶汽水。 数据范围:输入的正整数满足 1≤n≤100 注意&#xff…...

TSINGSEE智能分析网关V4车辆结构化数据检测算法及车辆布控

车辆结构化视频AI检测技术,可通过AI识别对视频图像中划定区域内的出现的车辆进行检测、抓拍和识别,系统通过视频采集设备获取车辆特征信息,经过预处理之后,接入AI识别算法并与车辆底库进行对比,快速识别车辆身份和属性…...

git解决冲突的方法。

1、 cherry-pick git fetch ssh://jingyou.caigerrit.transtekcorp.com:29418/leshan refs/changes/23/34123/3 && git cherry-pick FETCH_HEAD2、 文件解冲突! 3、 cherry-pick完整。 git cherry-pick --continue4、查看状态。 5、 push。 git push o…...

[MT8766][Android12] 取消WIFI热点超过10分钟没有连接自动关闭设定

文章目录 开发平台基本信息问题描述解决方法 开发平台基本信息 芯片: MT8766 版本: Android 12 kernel: msm-4.19 问题描述 之前有个需求要设备默认开启WIFI热点,默认开启usb共享网络;而热点在原生的设定里面有个超时机制,如果在限定时间内…...

智能中仍存在着许多未被发现的逻辑

自然规律不仅包括精确的也包括模糊的,即模糊的基本自然律意味着自然界中的现象与规律并不是绝对精确的,存在一定的模糊性和不确定性。因此,用数学来完全描述和预测这些现象可能会有限制。 智能与人工智能(AI)抑或智能化…...

基于公共业务提取的架构演进——外部依赖防腐篇

背景 有了前两篇的帐号权限提取和功能设置提取的架构演进后,有一个问题就紧接着诞生了,对于诸多业务方来说,关键数据源的迁移如何在各个产品落地? 要知道这些数据都很关键: - 对于帐号,获取不到帐号信息是…...

uniapp小程序接入腾讯云【增强版人脸核身接入】

文档地址:https://cloud.tencent.com/document/product/1007/56812 企业申请注册这边就不介绍了,根据官方文档去申请注册。 申请成功后,下载【微信小程序sdk】 一、解压sdk,创建wxcomponents文件夹 sdk解压后发现是原生小程序代…...

Sass 最基础的语法

把每个点最简单的部分记录一下,方便自己查找 官方文档链接 Sass 笔记 1. & 父选择器,编译后为父选择器2. : 嵌套属性3. $ 变量3.1 数据类型3.2 变量赋值3.3. 数组3.4. map 4. 算数运算符5. #{}插值语法5.1 可以在选择器或属性名中使用变量5.2 将有引…...

2023年11月数据库流行度最新排名

点击查看最新数据库流行度最新排名(每月更新) 2023年11月数据库流行度最新排名 TOP DB顶级数据库索引是通过分析在谷歌上搜索数据库名称的频率来创建的 一个数据库被搜索的次数越多,这个数据库就被认为越受欢迎。这是一个领先指标。原始数…...

JavaEE-部署项目到服务器

本部分内容为:安装依赖:JDK,Tomcat,Mysql;部署项目到服务器 什么是Tomcat Tomcat简单的说就是一个运行JAVA的网络服务器,底层是Socket的一个程序,它也是JSP和Serlvet的一个容器。 为什么我们需要…...

计算机网络期末复习-Part1

1、列举几种接入网技术:ADSL,HFC,FTTH,LAN,WLAN ADSL(Asymmetric Digital Subscriber Line):非对称数字用户线路。ADSL 是一种用于通过电话线连接到互联网的技术,它提供…...

Redis系列-Redis过期策略以及内存淘汰机制【6】

目录 Redis系列-Redis过期策略以及内存淘汰机制【6】redis过期策略内存淘汰机制算法LRU算法LFU 其他场景对过期key的处理FAQ为什么不用定时删除策略? Ref 个人主页: 【⭐️个人主页】 需要您的【💖 点赞关注】支持 💯 Redis系列-Redis过期策略以及内存淘…...

多语言翻译软件 Mate Translate mac中文版特色功能

Mate Translate for Mac是一款多语言翻译软件,Mate Translate mac可以帮你翻译超过100种语言的单词和短语,使用文本到语音转换,并浏览历史上已经完成的翻译。你还可以使用Control S在弹出窗口中快速交换语言。 Mate Translate Mac版特色功能…...

Python GUI标准库tkinter实现与记事本相同菜单的文本编辑器(一)

介绍: Windows操作系统中自带了一款记事本应用程序,通常用于记录文字信息,具有简单文本编辑功能。Windows的记事本可以新建、打开、保存文件,有复制、粘贴、删除等功能,还可以设置字体类型、格式和查看日期时间等。 …...

rknn优化教程(二)

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

PHP和Node.js哪个更爽?

先说结论,rust完胜。 php:laravel,swoole,webman,最开始在苏宁的时候写了几年php,当时觉得php真的是世界上最好的语言,因为当初活在舒适圈里,不愿意跳出来,就好比当初活在…...

Opencv中的addweighted函数

一.addweighted函数作用 addweighted()是OpenCV库中用于图像处理的函数,主要功能是将两个输入图像(尺寸和类型相同)按照指定的权重进行加权叠加(图像融合),并添加一个标量值&#x…...

Leetcode 3577. Count the Number of Computer Unlocking Permutations

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

【android bluetooth 框架分析 04】【bt-framework 层详解 1】【BluetoothProperties介绍】

1. BluetoothProperties介绍 libsysprop/srcs/android/sysprop/BluetoothProperties.sysprop BluetoothProperties.sysprop 是 Android AOSP 中的一种 系统属性定义文件(System Property Definition File),用于声明和管理 Bluetooth 模块相…...

新能源汽车智慧充电桩管理方案:新能源充电桩散热问题及消防安全监管方案

随着新能源汽车的快速普及,充电桩作为核心配套设施,其安全性与可靠性备受关注。然而,在高温、高负荷运行环境下,充电桩的散热问题与消防安全隐患日益凸显,成为制约行业发展的关键瓶颈。 如何通过智慧化管理手段优化散…...

leetcodeSQL解题:3564. 季节性销售分析

leetcodeSQL解题:3564. 季节性销售分析 题目: 表:sales ---------------------- | Column Name | Type | ---------------------- | sale_id | int | | product_id | int | | sale_date | date | | quantity | int | | price | decimal | -…...

JVM暂停(Stop-The-World,STW)的原因分类及对应排查方案

JVM暂停(Stop-The-World,STW)的完整原因分类及对应排查方案,结合JVM运行机制和常见故障场景整理而成: 一、GC相关暂停​​ 1. ​​安全点(Safepoint)阻塞​​ ​​现象​​:JVM暂停但无GC日志,日志显示No GCs detected。​​原因​​:JVM等待所有线程进入安全点(如…...

嵌入式常见 CPU 架构

架构类型架构厂商芯片厂商典型芯片特点与应用场景PICRISC (8/16 位)MicrochipMicrochipPIC16F877A、PIC18F4550简化指令集,单周期执行;低功耗、CIP 独立外设;用于家电、小电机控制、安防面板等嵌入式场景8051CISC (8 位)Intel(原始…...

SpringAI实战:ChatModel智能对话全解

一、引言:Spring AI 与 Chat Model 的核心价值 🚀 在 Java 生态中集成大模型能力,Spring AI 提供了高效的解决方案 🤖。其中 Chat Model 作为核心交互组件,通过标准化接口简化了与大语言模型(LLM&#xff0…...