【经验分享】SpringCloud + MyBatis Plus 配置 MySQL,TDengine 双数据源

news/2024/10/19 2:26:07/

概述

因为项目中采集工厂中的设备码点的数据量比较大,需要集成TDengine时序数据库,所以需要设置双数据源

操作步骤

导入依赖

		<!-- 多数据源支持 --><dependency><groupId>com.baomidou</groupId><artifactId>dynamic-datasource-spring-boot-starter</artifactId><version>3.3.6</version></dependency><!-- taos连接驱动 --><dependency><groupId>com.taosdata.jdbc</groupId><artifactId>taos-jdbcdriver</artifactId><version>3.2.11</version></dependency>

 nacos 配置文件数据源修改

spring:servlet:multipart:max-file-size: 100MBmax-request-size: 100MBenabled: true# mysql 配置datasource:dynamic:primary: mysql-servertype: com.alibaba.druid.pool.DruidDataSourcemysql-server:driver-class-name: com.mysql.cj.jdbc.Driverjdbc-url: jdbc:mysql://ip:port/db?useUnicode=true&characterEncoding=UTF-8&autoReconnect=true&serverTimezone=Asia/Shanghaiusername: usernamepassword: passwordinitial-size: 10max-active: 100min-idle: 10max-wait: 60000pool-prepared-statements: truemax-pool-prepared-statement-per-connection-size: 20time-between-eviction-runs-millis: 60000min-evictable-idle-time-millis: 300000test-while-idle: truetest-on-borrow: falsetest-on-return: falsestat-view-servlet:enabled: trueurl-pattern: /druid/*filter:stat:log-slow-sql: trueslow-sql-millis: 1000merge-sql: falsewall:config:multi-statement-allow: true# TDengine 配置tdengine-server:driver-class-name: com.taosdata.jdbc.rs.RestfulDriverjdbc-url: jdbc:TAOS-RS://ip:port/db?timezone=UTC-8&charset=utf-8username: usernamepassword: passwordpool-name: Data_trans_HikariCPminimum-idle: 10 #最小空闲连接数量idle-timeout: 600000 #空闲连接存活最大时间,默认600000(10分钟)maximum-pool-size: 100 #连接池最大连接数,默认是10auto-commit: true  #此属性控制从池返回的连接的默认自动提交行为,默认值:truemax-lifetime: 1800000 #此属性控制池中连接的最长生命周期,值0表示无限生命周期,默认1800000即30分钟connection-timeout: 30000 #数据库连接超时时间,默认30秒,即30000

新增自定义数据源配置类

MySQL

java">/*** MySQL 双数据源配置* @author pumpkin* @date 2024/5/16 14:08*/
@Configuration
@MapperScan(basePackages = {"com.xxx.xxx.xxx.dao", "com.xxx.xxx.xxx.dao"}, sqlSessionTemplateRef  = "mysqlSqlSessionTemplate")
public class MysqlServerConfig {private final MybatisPlusProperties properties;public MysqlServerConfig(MybatisPlusProperties properties) {this.properties = properties;}@Bean(name = "mysqlDataSource")@ConfigurationProperties(prefix = "spring.datasource.mysql-server")@Primarypublic DataSource mysqlDataSource() {return DataSourceBuilder.create().build();}@Bean(name = "mysqlSqlSessionFactory")@Primarypublic SqlSessionFactory mysqlSqlSessionFactory(@Qualifier("mysqlDataSource") DataSource dataSource) throws Exception {
//        SqlSessionFactoryBean bean = new SqlSessionFactoryBean();MybatisSqlSessionFactoryBean bean = new MybatisSqlSessionFactoryBean();bean.setDataSource(dataSource);// 指定多个XML映射文件位置PathMatchingResourcePatternResolver resolver = new PathMatchingResourcePatternResolver();
//        bean.setMapperLocations(resolver.getResources("classpath*:/mapper/*.xml"));Resource[] resources1 = resolver.getResources("classpath*:mapper/**/*.xml");Resource[] resources2 = resolver.getResources("classpath*:mapper/*.xml");// 将多个资源数组合并为一个Resource[] mapperLocations = new Resource[resources1.length + resources2.length];System.arraycopy(resources1, 0, mapperLocations, 0, resources1.length);System.arraycopy(resources2, 0, mapperLocations, resources1.length, resources2.length);// 设置合并后的资源数组bean.setMapperLocations(mapperLocations);//        MybatisConfiguration configuration = this.properties.getConfiguration();
//        if (configuration == null && !StringUtils.hasText(this.properties.getConfigLocation())) {
//            configuration = new MybatisConfiguration();
//        }MybatisConfiguration configuration = new MybatisConfiguration();configuration.setMapUnderscoreToCamelCase(true);configuration.setDefaultFetchSize(100);configuration.setDefaultStatementTimeout(30);bean.setConfiguration(configuration);return bean.getObject();}@Bean(name = "mysqlTransactionManager")@Primarypublic DataSourceTransactionManager mysqlTransactionManager(@Qualifier("mysqlDataSource") DataSource dataSource) {return new DataSourceTransactionManager(dataSource);}@Bean(name = "mysqlSqlSessionTemplate")@Primarypublic SqlSessionTemplate mysqlSqlSessionTemplate(@Qualifier("mysqlSqlSessionFactory") SqlSessionFactory sqlSessionFactory) throws Exception {return new SqlSessionTemplate(sqlSessionFactory);}
}

TDengine

java">/*** TDengine 双数据源配置* @author pumpkin* @date 2024/5/16 14:08*/
@Configuration
@MapperScan(basePackages = {"com.xxx.xxx.xxx.tdengine"}, sqlSessionTemplateRef  = "tdengineSqlSessionTemplate")
public class TDengineServerConfig {@Bean(name = "tdengineDataSource")@ConfigurationProperties(prefix = "spring.datasource.tdengine-server")public DataSource tdengineDataSource() {return DataSourceBuilder.create().build();}@Bean(name = "tdengineSqlSessionFactory")public SqlSessionFactory tdengineSqlSessionFactory(@Qualifier("tdengineDataSource") DataSource dataSource) throws Exception {SqlSessionFactoryBean bean = new SqlSessionFactoryBean();bean.setDataSource(dataSource);bean.setMapperLocations(new PathMatchingResourcePatternResolver().getResources("classpath:mapper/tdengine/*.xml"));return bean.getObject();}@Bean(name = "tdengineTransactionManager")public DataSourceTransactionManager tdengineTransactionManager(@Qualifier("tdengineDataSource") DataSource dataSource) {return new DataSourceTransactionManager(dataSource);}@Bean(name = "tdengineSqlSessionTemplate")public SqlSessionTemplate tdengineSqlSessionTemplate(@Qualifier("tdengineSqlSessionFactory") SqlSessionFactory sqlSessionFactory) throws Exception {return new SqlSessionTemplate(sqlSessionFactory);}}

将访问对应数据源的 Mapper 类放在对应的包下,使用 DAO 或者 Mapper 层的方法的时候就会操作对应的数据源了


http://www.ppmy.cn/news/1470432.html

相关文章

宕机了, redis如何保证数据不丢?

前言 如果有人问你&#xff1a;"你会把 Redis 用在什么业务场景下&#xff1f;" 我想你大概率会说&#xff1a;"我会把它当作缓存使用&#xff0c;因为它把后端数据库中的数据存储在内存中&#xff0c;然后直接从内存中读取数据&#xff0c;响应速度会非常快。…

ES的高可用

es最小高可用集群组成 At least three master-eligible nodes&#xff08;至少三个符合主节点条件的节点&#xff09;At least two nodes of each role&#xff08;每个角色至少有两个节点&#xff09;At least two copies of each shard (one primary and one or more replica…

融资A轮B轮是什么意思?

环境&#xff1a; 融资 问题描述&#xff1a; 融资A轮B轮是什么意思 解决方案&#xff1a; 在A轮融资之前&#xff0c;通常有以下几轮融资阶段&#xff1a; 种子轮&#xff08;Seed Round&#xff09;&#xff1a;这是企业生命周期中最早的融资阶段&#xff0c;通常发生在…

Linux系统ubuntu20.04 无人机PX4 开发环境搭建(失败率很低)

Linux系统ubuntu20.04 无人机PX4 开发环境搭建 PX4固件下载开发环境搭建MAVROS安装安装地面站QGC PX4固件下载 PX4的源码处于GitHub&#xff0c;因为众所周知的原因git clone经常失败&#xff0c;此处从Gitee获取PX4源码和依赖模块。 git clone https://gitee.com/voima/PX4-…

java的构造方法——无参构造方法

java在实例化一个对象后&#xff0c;如果需要为这个对象的属性赋值&#xff0c;就必须直接访问对象属性或者调用setter的方法。 如果要在实例化对象时为这个对象的属性赋值&#xff0c;可以通过构造方法实现。 构造方法是一个特殊的成员方法&#xff0c;定义时需要注意以下几…

Day1:基础语法

今日目标&#xff1a;理解什么是变量、掌握常用的数据类型、学会数据类型转换 一、JavaScript 介绍 1. JavaScript 基础知识 主要讲解 &#xff1a;JavaScript 是什么、书写位置、注释、结束符、输入和输出语法、字面量。 1.1 JavaScript 是什么 是一种运行在客户端(浏览器…

响应式网页开发方法与实践

随着移动设备的普及和多样化&#xff0c;响应式网页开发已成为现代网页设计的主流趋势。响应式网页&#xff08;Responsive Web Design, RWD&#xff09;是一种网页设计技术&#xff0c;其核心思想是通过灵活的布局和媒体查询&#xff0c;使网页能够适应不同设备和屏幕尺寸&…

QT利用QGraphicsDropShadowEffect效果及自定义按钮来实现一个炫酷键盘

1、效果 2、核心代码 #include "widget.h" #include "ui_widget.h"Widget::Widget(QWidget *parent): QWidget(parent<