当前位置: 首页 > 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;}
}

http://www.lryc.cn/news/153763.html

相关文章:

  • 【JavaScript 16】对象继承 原型对象属性 原型链 构造函数属性 instanceof运算符 继承 多重继承 模块
  • 地下管线三维自动建模软件MagicPipe3D V3.0发布
  • 百度等8家企业首批上线大模型服务;大语言模型微调之道
  • 二、Mycat2 相关概念及读写分离
  • react利用wangEditor写评论和@功能
  • Android之布局转圆角
  • Linux的目录结构特点
  • 【算法与数据结构】654、LeetCode最大二叉树
  • 您必须尝试的 4 种经典特征提取技术!
  • Unity中Shader的遮罩的实现
  • 架构师成长之路|Redis key过期清除策略
  • ubuntu20.04使用privoxy进行http代理转http代理,并定制http代理头(hide-user-agent的使用方法)
  • 任意文件读取
  • 微信小程序餐饮外卖系统设计与实现
  • 一文速览嵌入式六大出口
  • 华为云云服务器评测 | 宝塔8.0镜像应用
  • 构建简单的Node.js HTTP服务器,发布公网远程访问的快速方法
  • ModaHub魔搭社区:向量数据库产业的现状与技术挑战
  • pmp和软件高项哪个含金量高?
  • 手把手教你用Vite构建第一个Vue3项目
  • 美创科技获通信网络安全服务能力评定(应急响应一级)认证!
  • 计算机视觉与人工智能在医美人脸皮肤诊断方面的应用
  • RCU501 RMP201-8 KONGSBERG 分布式处理单元
  • 说说 MVCC 的工作原理?
  • 微信小程序请求接口返回的二维码(图片),本地工具和真机测试都能显示,上线之后不显示问题
  • Python小知识 - 1. Python装饰器(decorator)
  • 如何访问GitHub
  • 【广州华锐互动】智能变电站AR仿真实训系统大大提高培训的效率和质量
  • 手写Mybatis:第11章-流程解耦,封装结果集处理器
  • 金融风控数据分析-信用评分卡建模(附数据集下载地址)