springboot 多数据源配置,postgresql+mysql为例
由于项目需要,利用springboot配置多数据源,postgresql+mysql;
1.首先导入需要的依赖包,包的版本根据需要选择,本项目是spring-boot-starter-parent 2.7
<dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-data-jdbc</artifactId>
</dependency>
<dependency><groupId>org.postgresql</groupId><artifactId>postgresql</artifactId><scope>runtime</scope>
</dependency>
<dependency><groupId>mysql</groupId><artifactId>mysql-connector-java</artifactId><scope>runtime</scope>
</dependency>
<dependency><groupId>org.mybatis.spring.boot</groupId><artifactId>mybatis-spring-boot-starter</artifactId><version>2.2.2</version>
</dependency>
2.在配置文件applicaiton.yaml中,配置好数据库的相关信息,postgresql默认端口5432,mysql默认端口3306.
spring:datasource:##多数据源配置##postgresqldb1:jdbc-url: jdbc:postgresql://ip:port/postgres?serverTimezone=UTC&useUnicode=true&characterEncoding=utf8&useSSL=falseusername: postgrespassword: postgresdriver-class-name: org.postgresql.Driver##mysqldb2:jdbc-url: jdbc:mysql://ip:port/electricity?serverTimezone=UTC&useUnicode=true&characterEncoding=utf8&useSSL=falseusername: rootpassword: rootdriver-class-name: com.mysql.cj.jdbc.Driver
3.添加两个配置类,分别对两个数据源进行配置,DataSourceConfig1和DataSourceConfig2,
DataSourceConfig1.java示例代码
package com.cocc.electricity.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.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 javax.sql.DataSource;/*** @Description postgreSql 数据库配置* @Author hhs* @Date 2022/6/9 9:06*/
@Configuration
@MapperScan(basePackages = "com.cocc.electricity.mapper.db1",sqlSessionFactoryRef = "db1SqlSessionFactory")
public class DataSourceConfig1 {@Primary@Bean("db1DataSource")@ConfigurationProperties(prefix = "spring.datasource.db1")public DataSource getDb1DataSource(){return DataSourceBuilder.create().build();}@Primary@Bean("db1SqlSessionFactory")public SqlSessionFactory db1SqlSessionFactory(@Qualifier("db1DataSource") DataSource dataSource) throws Exception{SqlSessionFactoryBean bean = new SqlSessionFactoryBean();bean.setDataSource(dataSource);bean.setMapperLocations(new PathMatchingResourcePatternResolver().getResources("classpath*:/mapper/db1/*.xml"));return bean.getObject();}@Primary@Bean("db1SqlSessionTemplate")public SqlSessionTemplate db1SqlSessionTemplate(@Qualifier("db1SqlSessionFactory") SqlSessionFactory sqlSessionFactory){return new SqlSessionTemplate(sqlSessionFactory);}}
DataSourceConfig2.java示例代码
package com.cocc.electricity.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.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 javax.sql.DataSource;/*** @Description mysql数据库配置* @Author hhs* @Date 2022/6/9 9:06*/
@Configuration
@MapperScan(basePackages = "com.cocc.electricity.mapper.db2",sqlSessionFactoryRef = "db2SqlSessionFactory")
public class DataSourceConfig2 {@Bean("db2DataSource")@ConfigurationProperties(prefix = "spring.datasource.db2")public DataSource getDb2DataSource(){return DataSourceBuilder.create().build();}@Bean("db2SqlSessionFactory")public SqlSessionFactory db2SqlSessionFactory(@Qualifier("db2DataSource") DataSource dataSource) throws Exception{SqlSessionFactoryBean bean = new SqlSessionFactoryBean();bean.setDataSource(dataSource);bean.setMapperLocations(new PathMatchingResourcePatternResolver().getResources("classpath*:/mapper/db2/*.xml"));return bean.getObject();}@Bean("db2SqlSessionTemplate")public SqlSessionTemplate db2SqlSessionTemplate(@Qualifier("db2SqlSessionFactory") SqlSessionFactory sqlSessionFactory){return new SqlSessionTemplate(sqlSessionFactory);}
}
4.配置完成,可以写两个mapper,查询数据库数据,进行调试。
springboot 多数据源配置,postgresql+mysql为例相关推荐
- Springboot多数据源配置详解
Springboot多数据源配置详解 概念 配置 多数据源使用 概念 一般来说,我们正常的业务只涉及一个数据源,在特定的业务场景中需要使用多个数据源的情况,就需要配置多个数据源来满足特定的业务需求.本 ...
- springboot 多数据源配置的几种方式
springboot多数据源配置的三种方式 application.yml配置 1.@Ds("配置数据源名称") 引入依赖 <dependency> <group ...
- java spring多数据源配置文件_基于注解实现SpringBoot多数据源配置
1.功能介绍 在实际的开发中,同一个项目中使用多个数据源是很常见的场景.最近在学习的过程中使用注解的方式实现了一个Springboot项目多数据源的功能.具体实现方式如下. 2.在applicatio ...
- SpringBoot 之数据源配置
文章目录 市面上的几种数据源比对 SpringBoot自动装配DataSource原理 HiKariCP 数据源配置 Druid 数据源配置 SpringBoot集成Druid连接池 Druid 多数 ...
- SpringBoot多数据源配置
作者平台: | CSDN:blog.csdn.net/qq_41153943 | 掘金:juejin.cn/user/651387- | 知乎:www.zhihu.com/people/1024- | ...
- springBoot 双数据源配置 (Oracle+ SQL sever)主数据源配置setMapperLocations多个路径
1.添加pom依赖 (部分) <!--添加 阿里连接池 支持--><dependency><groupId>com.alibaba</groupId>& ...
- SpringBoot排除数据源配置,配置决定行为
项目场景: 最近接到一个需求,大概意思呢就是数据库连接不够用了,部分服务(分服务)就不需要配置数据源了,需要跟db打交道的地方全部改成rpc去调用具备db能力的服务(总服务) 问题描述: 其实需要改的 ...
- springboot 多数据源配置与使用
多数据源配置 这个是springboot1版本的 spingboot2移步 > springboot2.x jpa接入多数据源 application.properties 配置两个数据库 #数 ...
- 数据源(DataSource)是什么以及SpringBoot中数据源配置
数据源 数据源,简单理解为数据源头,提供了应用程序所需要数据的位置.数据源保证了应用程序与目标数据之间交互的规范和协议,它可以是数据库,文件系统等等.其中数据源定义了位置信息,用户验证信息和交互时所需 ...
最新文章
- gomod和govendor的简单理解
- 大数据热门职业薪酬榜 Hadoop人才居首
- [转载]Ubuntu安装配置Mysql
- Java编译器API
- mysql count转字符串_MySQL字符串函数
- 量子计算机网络指数时间,科普:量子计算机是这样计算的
- day9 java的实例语句块和代码执行顺序
- 从零开始--系统深入学习android(实践-让我们开始写代码-新手指南-3.Hello,本地化)...
- 用js来实现添加学生信息表
- 图片怎么转换为jpg格式的?照片如何在线转格式?
- 程序员代码面试指南刷题--第五章.字符串的调整II
- STM32F407ZET6的RTC闹钟二次设置笔记
- CSS/HTML 网页添加网易云歌曲插件
- 详解C# 枚举高级用法之Description
- mysql 优化思路_Mysql优化思路
- 思岚科技亮相工博会,加速技术下沉,赋能产业新发展
- DISABLE BUTTON
- 体面 用计算机弹调子,怎么把计算器调成音符
- VS2019编译器修改背景壁纸(写代码背景不再单薄,试试换一张清晰的美女照片)
- 很全面的Matlab绘图(含代码!收藏版!)