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

SpringBoot Mybatis 多数据源 MySQL+Oracle+Redis

 一、背景

在SpringBoot Mybatis 项目中,需要连接 多个数据源,连接多个数据库,需要连接一个MySQL数据库和一个Oracle数据库和一个Redis

二、依赖 pom.xml

 <dependencies><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-web</artifactId></dependency><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-test</artifactId><scope>test</scope></dependency><!-- MySQL --><dependency><groupId>mysql</groupId><artifactId>mysql-connector-java</artifactId><version>8.0.26</version></dependency><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-jdbc</artifactId></dependency><dependency><groupId>org.mybatis.spring.boot</groupId><artifactId>mybatis-spring-boot-starter</artifactId><version>1.3.2</version></dependency><!-- Oracle --><dependency><groupId>com.oracle.database.jdbc</groupId><artifactId>ojdbc8</artifactId><version>19.8.0.0</version></dependency><!-- Redis --><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-data-redis</artifactId><version>2.4.4</version></dependency><!-- lombok --><dependency><groupId>org.projectlombok</groupId><artifactId>lombok</artifactId><version>1.18.16</version><scope>provided</scope></dependency><dependency><groupId>javax.persistence</groupId><artifactId>javax.persistence-api</artifactId><version>2.2</version></dependency><!--swagger2--><dependency><groupId>io.springfox</groupId><artifactId>springfox-swagger2</artifactId><version>2.9.2</version></dependency><!--swagger-ui--><dependency><groupId>io.springfox</groupId><artifactId>springfox-swagger-ui</artifactId><version>2.9.2</version></dependency><!-- https://mvnrepository.com/artifact/cn.easyproject/orai18n --><dependency><groupId>cn.easyproject</groupId><artifactId>orai18n</artifactId><version>12.1.0.2.0</version></dependency></dependencies>

三、项目结构

四、application.yml

spring.datasource.url数据库的JDBC URL

spring.datasource.jdbc-url用来重写自定义连接池

Hikari没有url属性,但是有jdbcUrl属性,在这中情况下必须使用jdbc_url

server:port: 8080spring:datasource:primary:jdbc-url: jdbc:mysql://localhost:3306/database_nameusername: rootpassword: 123456driver-class-name: com.mysql.cj.jdbc.Driversecondary:jdbc-url: jdbc:oracle:thin:@localhost:1521/ORCLusername: rootpassword: 123456driver-class-name: oracle.jdbc.driver.OracleDriver    

五、MySQL配置类

MysqlDataSourceConfig

使用注解@Primary配置默认数据源

package com.example.multipledata.config.mysqlconfig;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.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 = MysqlDataSourceConfig.PACKAGE, sqlSessionFactoryRef = "mysqlSqlSessionFactory")
public class MysqlDataSourceConfig {static final String PACKAGE = "com.example.multipledata.mapper.mysqlmapper";static final String MAPPER_LOCATION = "classpath*:mapper/mysqlmapper/*.xml";@Primary@Bean(name = "mysqlDataSource")@ConfigurationProperties(prefix = "spring.datasource.primary")public DataSource mysqlDataSource() {return DataSourceBuilder.create().build();}@Primary@Bean(name = "mysqlTransactionManager")public DataSourceTransactionManager mysqlTransactionManager() {return new DataSourceTransactionManager((mysqlDataSource()));}@Primary@Bean(name = "mysqlSqlSessionFactory")public SqlSessionFactory mysqlSqlSessionFactory(@Qualifier("mysqlDataSource") DataSource mysqlDatasource) throws Exception {final SqlSessionFactoryBean sessionFactory = new SqlSessionFactoryBean();sessionFactory.setDataSource(mysqlDatasource);sessionFactory.setMapperLocations(new PathMatchingResourcePatternResolver().getResources(MysqlDataSourceConfig.MAPPER_LOCATION));return sessionFactory.getObject();}
}

六、Oracle配置类

OracleDataSourceConfig

package com.example.multipledata.config.oracleconfig;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.boot.jdbc.DataSourceBuilder;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.io.support.PathMatchingResourcePatternResolver;
import org.springframework.jdbc.datasource.DataSourceTransactionManager;import javax.sql.DataSource;@Configuration
@MapperScan(basePackages = OracleDataSourceConfig.PACKAGE, sqlSessionFactoryRef = "oracleSqlSessionFactory")
public class OracleDataSourceConfig {static final String PACKAGE = "com.example.multipledata.mapper.oraclemapper";static final String MAPPER_LOCATION = "classpath*:mapper/oraclemapper/*.xml";@Bean(name = "oracleDataSource")@ConfigurationProperties(prefix = "spring.datasource.secondary")public DataSource oracleDataSource() {return DataSourceBuilder.create().build();}@Bean(name = "oracleTransactionManager")public DataSourceTransactionManager oracleTransactionManager() {return new DataSourceTransactionManager(oracleDataSource());}@Bean(name = "oracleSqlSessionFactory")public SqlSessionFactory oracleSqlSessionFactory(@Qualifier("oracleDataSource") DataSource oracleDataSource) throws Exception {final SqlSessionFactoryBean sessionFactory = new SqlSessionFactoryBean();sessionFactory.setDataSource(oracleDataSource);sessionFactory.setMapperLocations(new PathMatchingResourcePatternResolver().getResources(OracleDataSourceConfig.MAPPER_LOCATION));return sessionFactory.getObject();}
}

原文地址:

https://www.cnblogs.com/windy-xmwh/p/14748567.html

七、增加Redis连接

注意,我的Redis连接,使用了密码

7.1 新增依赖 pom.xml

pom.xml

        <!-- Redis --><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-data-redis</artifactId><version>2.4.4</version></dependency><dependency><groupId>io.lettuce</groupId><artifactId>lettuce-core</artifactId></dependency>

7.2 配置Redis连接  application.yml

  spring:redis:host: hostport: 6379password: 1

7.3 Redis 配置文件 RedisConfig

在config目录下,新增RedisConfig文件

package com.example.kyjjserver.config;import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.connection.RedisConnectionFactory;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.serializer.GenericJackson2JsonRedisSerializer;
import org.springframework.data.redis.serializer.StringRedisSerializer;
import org.springframework.data.redis.connection.lettuce.LettuceConnectionFactory;@Configuration
public class RedisConfig {@Value("${spring.redis.host}")private String redisHost;@Value("${spring.redis.port}")private int redisPort;@Value("${spring.redis.password}")private String redisPassword;@Beanpublic RedisConnectionFactory redisConnectionFactory() {LettuceConnectionFactory lettuceConnectionFactory = new LettuceConnectionFactory(redisHost, redisPort);lettuceConnectionFactory.setPassword(redisPassword);return lettuceConnectionFactory;}@Beanpublic RedisTemplate<String, Object> redisTemplate(RedisConnectionFactory redisConnectionFactory) {RedisTemplate<String, Object> redisTemplate = new RedisTemplate<>();redisTemplate.setConnectionFactory(redisConnectionFactory);redisTemplate.setKeySerializer(new StringRedisSerializer());redisTemplate.setValueSerializer(new GenericJackson2JsonRedisSerializer());redisTemplate.setHashKeySerializer(new StringRedisSerializer());redisTemplate.setHashValueSerializer(new GenericJackson2JsonRedisSerializer());redisTemplate.afterPropertiesSet();return redisTemplate;}
}

7.4 操作Redis

7.4.1 封装操作方法

在service目录下,新增RedisService文件

package com.example.kyjjserver.service;import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;import java.util.Set;@Service
public class RedisService {private final RedisTemplate<String, Object> redisTemplate;@Autowiredpublic RedisService(RedisTemplate<String, Object> redisTemplate) {this.redisTemplate = redisTemplate;}public void deleteData(String key) {redisTemplate.delete(key);}public void deleteDataAll(String pattern) {Set<String> keys = redisTemplate.keys("*" + pattern + "*");if (keys != null) {redisTemplate.delete(keys);}}
}

7.4.2 调用方法,操作Redis库

1、注入

2、调用

@Component
public class DeleteRedisInfo {@Autowiredprivate RedisService redisService;@Transactionalpublic AAA deleteUser(xxx xxx){// 删除手机号redisService.deleteDataAll(xxx);return xxx;}
}

相关文章:

SpringBoot Mybatis 多数据源 MySQL+Oracle+Redis

一、背景 在SpringBoot Mybatis 项目中&#xff0c;需要连接 多个数据源&#xff0c;连接多个数据库&#xff0c;需要连接一个MySQL数据库和一个Oracle数据库和一个Redis 二、依赖 pom.xml <dependencies><dependency><groupId>org.springframework.boot&l…...

【JavaScript 16】对象继承 原型对象属性 原型链 构造函数属性 instanceof运算符 继承 多重继承 模块

对象继承 原型对象概述instanceof运算符构造函数的继承多重继承模块 A 对象通过继承 B 对象&#xff0c;就能 直接拥有 B 对象的所有属性和方法&#xff08;利于代码复用&#xff09; 大部分面向对象的编程语言都是通过类&#xff08;class&#xff09;实现对象的继承 但 传统…...

地下管线三维自动建模软件MagicPipe3D V3.0发布

2023年9月1日经纬管网建模系统MagicPipe3D V3.0正式发布&#xff0c;该版本经过众多用户应用和反馈&#xff0c;在三维地下管线建模效果、效率、适配性等方面均有显著提升&#xff01;MagicPipe3D本地离线参数化构建地下管网模型&#xff08;包括管道、接头、附属设施等&#x…...

百度等8家企业首批上线大模型服务;大语言模型微调之道

&#x1f989; AI新闻 &#x1f680; 百度等8家企业首批上线大模型服务 摘要&#xff1a;百度、字节、中科院旗下8家企业/机构的大模型通过备案&#xff0c;正式面向公众提供服务。百度旗下AI大模型产品文心一言率先开放&#xff0c;用户可下载App或登录官网体验。百川智能也…...

二、Mycat2 相关概念及读写分离

第三章 Mycat2 相关概念 3.1 概念描述 1、分库分表 按照一定规则把数据库中的表拆分为多个带有数据库实例,物理库,物理表访问路 径的分表。 解读&#xff1a;分库&#xff1a;一个电商项目&#xff0c;分为用户库、订单库等等。 分表&#xff1a;一张订单表数据数百万&#xff…...

react利用wangEditor写评论和@功能

先引入wangeditor写评论功能 import React, { useEffect, useState, useRef, forwardRef, useImperativeHandle } from react; import wangeditor/editor/dist/css/style.css; import { Editor, Toolbar } from wangeditor/editor-for-react; import { Button, Card, Col, For…...

Android之布局转圆角

Android之布局转圆角 文章目录 Android之布局转圆角说明一、效果图二、实现步骤1.自定义RoundRelativeLayout2.使用 总结 说明 很多需求比较无语&#xff0c;需要某个布局转圆角&#xff0c;像个显眼包一样&#xff0c;所以为了满足显眼包&#xff0c;必须整呐提示&#xff1a…...

Linux的目录结构特点

Linux的目录结构特点 1、使用树形目录结构来组织和管理文件。 2、整个系统只有一个根目录&#xff08;树根&#xff09;&#xff0c;Linux的根目录用“/”表示。 3、其他所有分区以及外部设备&#xff08;如硬盘&#xff0c;光驱等&#xff09;都是以根目录为起点&#xff0…...

【算法与数据结构】654、LeetCode最大二叉树

文章目录 一、题目二、解法三、完整代码 所有的LeetCode题解索引&#xff0c;可以看这篇文章——【算法和数据结构】LeetCode题解。 一、题目 二、解法 思路分析&#xff1a;【算法与数据结构】106、LeetCode从中序与后序遍历序列构造二叉树这两道题有些类似&#xff0c;相关代…...

您必须尝试的 4 种经典特征提取技术!

一、说明 特征提取如何实现&#xff1f;其手段并不是很多&#xff0c;有四个基本方法&#xff0c;作为AI工程师不能不知。因此&#xff0c;本篇将对四种特征提取给出系统的方法。 二、概述 图像分类长期以来一直是计算机视觉领域的热门话题&#xff0c;并希望能够保持这种状态。…...

Unity中Shader的遮罩的实现

文章目录 前言一、遮罩效果的实现主要是使用对应的纹理实现的&#xff0c;在属性中暴露对应的遮罩纹理&#xff0c;对其进行采样后&#xff0c;最后相乘输出即可二、如果需要像和主要纹理一样流动&#xff0c;则需要使用和_Time篇一样的方法实现流动即可 前言 Unity中Shader的…...

架构师成长之路|Redis key过期清除策略

Eviction policies maxmemory 100mb 当我们设置的内存达到指定的内存量时,清除策略的配置方式决定了默认行为。Redis可以为可能导致使用更多内存的命令返回错误,也可以在每次添加新数据时清除一些旧数据以返回到指定的限制。 当达到最大内存限制时,Redis所遵循的确切行为是…...

ubuntu20.04使用privoxy进行http代理转http代理,并定制http代理头(hide-user-agent的使用方法)

#sudo apt-get update;sudo apt install -y privoxy #sudo apt remove privoxyprivoxy --version; rootfv-az1239-825:/tmp# privoxy --version Privoxy version 3.0.28 (https://www.privoxy.org/) rootfv-az1239-825:/tmp# 安装完毕后,先停止服务,修改配置文件,再启动服…...

任意文件读取

文章目录 渗透测试漏洞原理任意文件读取1. 任意文件读取概述1.1 漏洞成因1.2 漏洞危害1.3 漏洞分类1.4 任意文件读取1.4.1 文件读取1.4.2 任意文件读取1.4.3 权限问题 1.5 任意文件下载1.5.1 一般情况1.5.2 PHP实现1.5.3 任意文件下载 2. 任意文件读取攻防2.1 路径过滤2.1.1 过…...

微信小程序餐饮外卖系统设计与实现

摘 要 随着现在的“互联网”的不断发展。现在传统的餐饮业也朝着网络化的方向不断的发展。现在线上线下的方式来实现餐饮的获客渠道增加&#xff0c;可以更好地帮助餐饮企业实现更多、更广的获客需求&#xff0c;实现更好的餐饮销售。截止到2021年末&#xff0c;我国的外卖市场…...

一文速览嵌入式六大出口

嵌入式行业的前景确实十分广阔&#xff0c;并且在许多领域都发挥着重要作用。以下是一些关键点&#xff0c;说明嵌入式系统的发展潜力和前途&#xff1a; 1. 物联网&#xff08;IoT&#xff09;&#xff1a;嵌入式系统是实现智能家居、智能城市、智能工厂等物联网设备的核心。物…...

华为云云服务器评测 | 宝塔8.0镜像应用

目录 &#x1f352;写在前面 &#x1f352;产品优势 &#x1f352;购买服务器 &#x1f352;服务器配置 &#x1f352;登录宝塔页面 &#x1f990;博客主页&#xff1a;大虾好吃吗的博客 &#x1f990;专栏地址&#xff1a;闲谈专栏地址 写在前面 云耀云服务器L实例是新一代开箱…...

构建简单的Node.js HTTP服务器,发布公网远程访问的快速方法

文章目录 前言1.安装Node.js环境2.创建node.js服务3. 访问node.js 服务4.内网穿透4.1 安装配置cpolar内网穿透4.2 创建隧道映射本地端口 5.固定公网地址 前言 Node.js 是能够在服务器端运行 JavaScript 的开放源代码、跨平台运行环境。Node.js 由 OpenJS Foundation&#xff0…...

ModaHub魔搭社区:向量数据库产业的现状与技术挑战

I. 向量数据库的崛起 什么是向量数据库 在过去的一段时间里,向量数据库逐渐在数据库领域崭露头角。那么,什么是向量数据库呢?简单来说,向量数据库是一种专门设计用来处理向量数据的数据库。这些向量数据可以是物理测量、机器学习模型输出、地理空间数据等。向量数据库使用…...

pmp和软件高项哪个含金量高?

人们常将PMP和高项放在一起比较&#xff0c;因为这两种证书都适用于项目经理职位&#xff0c;它们有高达60%的知识点重合度。在决定考哪一种证书之前&#xff0c;人们常常会感到困惑。 下面看一下pmp和软考高项的差异。 发证机构不同 PMP(Project Management Professional)是…...

JSON处理效率倍增:探索JSON Viewer的3个鲜为人知实用功能

JSON处理效率倍增&#xff1a;探索JSON Viewer的3个鲜为人知实用功能 【免费下载链接】json-viewer It is a Chrome extension for printing JSON and JSONP. 项目地址: https://gitcode.com/gh_mirrors/js/json-viewer 在数据驱动开发的时代&#xff0c;高效处理JSON数…...

洛谷-入门5-字符串3

P1553 数字反转&#xff08;升级版&#xff09;题目背景以下为原题面&#xff0c;仅供参考:给定一个数&#xff0c;请将该数各个位上数字反转得到一个新数。这次与 NOIp2011 普及组第一题不同的是&#xff1a;这个数可以是小数&#xff0c;分数&#xff0c;百分数&#xff0c;整…...

基于Vue的川汇水产养殖管理系统[vue]-计算机毕业设计源码+LW文档

摘要&#xff1a;随着水产养殖业的快速发展&#xff0c;传统的管理方式已难以满足现代化水产养殖的需求。本文介绍了一款基于Vue框架开发的川汇水产养殖管理系统&#xff0c;该系统旨在提高水产养殖管理的效率和精准度。系统涵盖了系统用户管理、水质管理、药品管理、设备管理、…...

2026算力大劫:全球开发者都在问:廉价算力到底去哪了?哪里的token性价比最高?

▶︎点击这里查看最新套餐https://coding.dongyao.ren/ 1. 2026&#xff1a;被“刺客”化的算力账单 进入2026年&#xff0c;AIGC行业并没有迎来预想中的“算力普惠”。相反&#xff0c;随着GPT-5.5等万亿参数模型成为企业刚需&#xff0c;以及北美云巨头在2026年第一季度集体…...

256K上下文颠覆智能编程:Qwen3-Coder重构全栈开发效率范式

256K上下文颠覆智能编程&#xff1a;Qwen3-Coder重构全栈开发效率范式 【免费下载链接】Qwen3-Coder-30B-A3B-Instruct 项目地址: https://ai.gitcode.com/hf_mirrors/unsloth/Qwen3-Coder-30B-A3B-Instruct 问题发现&#xff1a;传统AI编程助手的三大痛点 2025年Stac…...

OpenStack Train版三节点部署全攻略:从CentOS 7.6配置到Dashboard上线

OpenStack Train版三节点部署实战&#xff1a;从CentOS 7.6到Dashboard的完整指南 当企业需要构建私有云平台时&#xff0c;OpenStack作为最成熟的开源IaaS解决方案之一&#xff0c;其灵活性和可扩展性备受青睐。本文将带您完成一个生产级的三节点OpenStack Train版部署&#x…...

B站视频字幕抓取实战:Tampermonkey搭配GreasyFork脚本,5分钟搞定CC字幕导出

B站视频字幕高效提取指南&#xff1a;Tampermonkey与GreasyFork脚本深度应用 每次观看B站优质内容时&#xff0c;那些精心制作的字幕是否让你想保存下来反复学习&#xff1f;传统录屏或手动抄写效率低下&#xff0c;而专业工具又过于复杂。本文将带你探索浏览器脚本的魔法世界&…...

别再手动改Hosts了!用K8S Gateway API轻松搞定基于请求头的AB测试(OpenResty实战)

告别手动配置&#xff1a;基于K8S Gateway API的智能AB测试实战指南 每次功能迭代时&#xff0c;你是否还在反复修改本地Hosts文件来切换测试环境&#xff1f;或是为了验证某个接口在不同版本间的表现差异&#xff0c;不得不频繁重启服务或调整代理配置&#xff1f;这种低效的手…...

别再折腾官方源了!用XianDian-IaaS-v2.2在CentOS7上30分钟搞定OpenStack最小化部署

30分钟极速部署OpenStack&#xff1a;XianDian-IaaS在CentOS7上的实战指南 OpenStack作为开源云计算平台的标杆&#xff0c;其强大的灵活性和模块化设计吸引了大量企业用户。但官方部署流程的复杂性往往让初学者望而却步——依赖项冲突、版本兼容性问题、繁琐的配置步骤&#x…...

Ceph存储集群搭建:如何选择RAID卡模式(HBA vs IT vs non-RAID)

Ceph存储集群搭建&#xff1a;RAID卡模式选择与性能优化实战指南 在构建企业级Ceph存储集群时&#xff0c;硬件配置的每一个细节都可能成为性能瓶颈或稳定性隐患。其中&#xff0c;RAID控制器的工作模式选择——HBA、IT与non-RAID之间的差异&#xff0c;往往被许多初次部署Ceph…...