springBoot+ druid配置多数据源

devtools/2024/9/24 9:41:44/

springBoot+ druid配置多数据源
1.在yml加:

spring:#1.JDBC数据源datasource:druid:first:username: PYpassword: ral2024url: jdbc:mysql://localhost:3306/mysql?serverTimezone=UTC&characterEncoding=utf8&useUnicode=true&useSSL=falsedriver-class-name: com.mysql.jdbc.Driver#初始化连接池的连接数量 大小,最小,最大initial-size: 5min-idle: 5max-active: 20#配置获取连接等待超时的时间max-wait: 60000#配置间隔多久才进行一次检测,检测需要关闭的空闲连接,单位是毫秒time-between-eviction-runs-millis: 60000# 配置一个连接在池中最小生存的时间,单位是毫秒min-evictable-idle-time-millis: 30000# 配置一个连接在池中最大生存的时间,单位是毫秒max-evictable-idle-time-millis: 3000000validation-query: SELECT 1 FROM usertest-while-idle: truetest-on-borrow: truetest-on-return: false# 是否缓存preparedStatement,也就是PSCache  官方建议MySQL下建议关闭   个人建议如果想用SQL防火墙 建议打开pool-prepared-statements: truemax-pool-prepared-statement-per-connection-size: 20# 配置监控统计拦截的filters,去掉后监控界面sql无法统计,'wall'用于防火墙filters: stat,wall,slf4jfilter:stat:merge-sql: trueslow-sql-millis: 5000second:username: L2_USER_Z1password: L2_USER_Z1url: jdbc:oracle:thin:@172.16.200.60:1521:PYCXMES.PYGT.COMdriver-class-name: oracle.jdbc.OracleDriver#初始化连接池的连接数量 大小,最小,最大initial-size: 5min-idle: 5max-active: 20#配置获取连接等待超时的时间max-wait: 600000#配置间隔多久才进行一次检测,检测需要关闭的空闲连接,单位是毫秒time-between-eviction-runs-millis: 60000# 配置一个连接在池中最小生存的时间,单位是毫秒min-evictable-idle-time-millis: 30000# 配置一个连接在池中最大生存的时间,单位是毫秒max-evictable-idle-time-millis: 3000000validation-query: SELECT 1 FROM usertest-while-idle: truetest-on-borrow: truetest-on-return: false# 是否缓存preparedStatement,也就是PSCache  官方建议MySQL下建议关闭   个人建议如果想用SQL防火墙 建议打开pool-prepared-statements: truemax-pool-prepared-statement-per-connection-size: 20# 配置监控统计拦截的filters,去掉后监控界面sql无法统计,'wall'用于防火墙filters: stat,wall,slf4jfilter:stat:merge-sql: true###slow-sql-millis: 5000

2.新建一个config文件
里面有3个类,分别:DruidConfigFirst ,DruidConfigSecond ,TwoDataSourceConfig
结构如图:
在这里插入图片描述

package com.alnan.config;import org.apache.ibatis.session.SqlSessionFactory;
import org.mybatis.spring.SqlSessionFactoryBean;
import org.mybatis.spring.SqlSessionTemplate;
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.support.PathMatchingResourcePatternResolver;
import org.springframework.jdbc.datasource.DataSourceTransactionManager;import javax.sql.DataSource;@Configuration
@MapperScan(basePackages = "com.alnan.dao.mysql", sqlSessionTemplateRef  = "firstSqlSessionTemplate")
public class DruidConfigFirst {@Bean(name = "firstSqlSessionFactory")@Primarypublic SqlSessionFactory sqlSessionFactory(@Qualifier("firstDataSource") DataSource dataSource) throws Exception {SqlSessionFactoryBean bean = new SqlSessionFactoryBean();bean.setDataSource(dataSource);return bean.getObject();}@Bean(name = "firstTransactionManager")@Primarypublic DataSourceTransactionManager transactionManager(@Qualifier("firstDataSource") DataSource dataSource) {return new DataSourceTransactionManager(dataSource);}@Bean(name = "firstSqlSessionTemplate")@Primarypublic SqlSessionTemplate sqlSessionTemplate(@Qualifier("firstSqlSessionFactory") SqlSessionFactory sqlSessionFactory) throws Exception {return new SqlSessionTemplate(sqlSessionFactory);}
}
package com.alnan.config;import org.apache.ibatis.session.SqlSessionFactory;
import org.mybatis.spring.SqlSessionFactoryBean;
import org.mybatis.spring.SqlSessionTemplate;
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.core.io.support.PathMatchingResourcePatternResolver;
import org.springframework.jdbc.datasource.DataSourceTransactionManager;import javax.sql.DataSource;@Configuration
@MapperScan(basePackages = "com.alnan.dao.oracle", sqlSessionTemplateRef  = "secondSqlSessionTemplate")
public class DruidConfigSecond {@Bean(name = "secondSqlSessionFactory")public SqlSessionFactory sqlSessionFactory(@Qualifier("secondDataSource") DataSource dataSource) throws Exception {SqlSessionFactoryBean bean = new SqlSessionFactoryBean();bean.setDataSource(dataSource);return bean.getObject();}@Bean(name = "secondTransactionManager")public DataSourceTransactionManager transactionManager(@Qualifier("secondDataSource") DataSource dataSource) {return new DataSourceTransactionManager(dataSource);}@Bean(name = "secondSqlSessionTemplate")public SqlSessionTemplate sqlSessionTemplate(@Qualifier("secondSqlSessionFactory") SqlSessionFactory sqlSessionFactory) throws Exception {return new SqlSessionTemplate(sqlSessionFactory);}}
package com.alnan.config;import com.alibaba.druid.spring.boot.autoconfigure.DruidDataSourceBuilder;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Primary;import javax.sql.DataSource;@Configuration
public class TwoDataSourceConfig {@Primary@Bean(name = "firstDataSource")@ConfigurationProperties("spring.datasource.druid.first")public DataSource first() {return DruidDataSourceBuilder.create().build();}@Bean(name = "secondDataSource")@ConfigurationProperties("spring.datasource.druid.second")public DataSource second() {return DruidDataSourceBuilder.create().build();}
}

然后怎么用呢:
在这里插入图片描述
dao 里面分开对应mysql的Mapper,oracle的Mapper

应用:正常注入就行,他会自己去对应数据库
在这里插入图片描述
3.pom:

 <dependency><groupId>com.alibaba</groupId><artifactId>druid-spring-boot-starter</artifactId><version>1.1.10</version></dependency>

http://www.ppmy.cn/devtools/96755.html

相关文章

【数据结构】归并排序

1、介绍 归并排序&#xff08;merge sort&#xff09;是一种基于分治策略的排序算法&#xff0c;包含“划分”和“合并”阶段。 划分阶段&#xff1a;通过递归不断地将数组从中点处分开&#xff0c;将长数组的排序问题转换为短数组的排序问题。 合并阶段&#xff1a;当子数组…

1.Linux_常识

UNIX、Linux、GNU 1、UNIX UNIX是一个分时操作系统&#xff0c;特点是多用户、多任务 实时操作系统&#xff1a;来了请求就去解决请求 分时操作系统&#xff1a;来了请求先存着&#xff0c;通过调度轮到执行时执行 2、Linux Linux是一个操作系统内核 发行版本&#xff1…

C++ //练习 17.5 重写findBook,令其返回一个pair,包含一个索引和一个迭代器pair。

C Primer&#xff08;第5版&#xff09; 练习 17.5 练习 17.5 重写findBook&#xff0c;令其返回一个pair&#xff0c;包含一个索引和一个迭代器pair。 环境&#xff1a;Linux Ubuntu&#xff08;云服务器&#xff09; 工具&#xff1a;vim 代码块 vector<vector<Sal…

测试流程自动化实践!

测试流程自动化的最佳实践涉及多个方面&#xff0c;旨在提高测试效率、确保测试质量&#xff0c;并降低测试成本。以下是一些关键的实践方法&#xff1a; 1. 明确测试目标 确定测试范围&#xff1a;在开始自动化测试之前&#xff0c;需要明确哪些功能、模块或场景需要被测试。…

Keepalived 高可用集群详解和配置

Keepalived 高可用集群 集群类型 1、LB&#xff08;Load Balance&#xff09;&#xff1a;负载均衡 LVS&#xff1a;四层负载均衡 HAProxy&#xff1a;七层/四层 负载均衡 nginx&#xff1a;七层负载均衡 (http/upstream,stream/upstream) 2、HA&#xff08;High Availa bili…

C#高级应用

C# 特性&#xff08;Attribute&#xff09; 特性&#xff08;Attribute&#xff09;是用于在运行时传递程序中各种元素&#xff08;比如类、方法、结构、枚举、组件等&#xff09;的行为信息的声明性标签。您可以通过使用特性向程序添加声明性信息。一个声明性标签是通过放置在…

依赖倒置原则详解

依赖倒置原则详解 一、引言 在大型系统架构设计中&#xff0c;依赖倒置原则&#xff08;Dependency Inversion Principle&#xff0c;DIP&#xff09;被广泛视为增强系统灵活性和可维护性的核心原则之一。最近在架构设计审查中&#xff0c;我们经常遇到由于依赖关系设计不当导…

机器学习:逻辑回归原理、参数介绍和优缺点

1、概念 逻辑回归是一种统计方法&#xff0c;用于分析一个或多个自变量&#xff08;解释变量&#xff09;与一个二元因变量&#xff08;响应变量&#xff09;之间的关系。虽然称为“回归”&#xff0c;但逻辑回归实际上是一种分类算法&#xff0c;因为它的输出是类别标签&#…