Spring Boot Mybatis 多数据源配置
来源:SegmentFault
2023-01-24 14:59:14
0浏览
收藏
所属专题:Spring Boot 4 生产迁移与服务工程专题
- 从官方升级路径、Web 开发到数据、 安全与部署
编程并不是一个机械性的工作,而是需要有思考,有创新的工作,语法是固定的,但解决问题的思路则是依靠人的思维,这就需要我们坚持学习和更新自己的知识。今天golang学习网就整理分享《Spring Boot Mybatis 多数据源配置》,文章讲解的知识点主要包括MySQL、Java、mybatis,如果你对数据库方面的知识点感兴趣,就不要错过golang学习网,在这可以对大家的知识积累有所帮助,助力开发能力的提升。
在 spring boot 项目中配置多个数据源的情形在开发中经常会遇见,本文以 Spring Boot + MyBatis 的方式实现 mysql + Postgresql 双数据源项目搭建,具体详细代码请参考:
https://gitee.com/senn-wen/my...
一、依赖配置
在 pom.xml 文件中引入
org.postgresql postgresql runtime mysql mysql-connector-java runtime
二、Spring boot 配置
2.1 application.yml 配置
配置需要连接的数据库连接参数,下面的样例配置了 postgresql 和 mysql ,因为我选择了hikariCP 作为连接池,因此在下面配置了下 hikari,如果是线上的服务建议根据实际的服务器性能大小设置好该参数。记住你连接参数的配置路径,后续要使用到。
spring:
datasource:
postgresql:
driver-class-name: org.postgresql.Driver
url: jdbc:postgresql://url:port/database
username: postGIS
password: postGIS
mysql:
driver-class-name: com.mysql.cj.jdbc.Driver
url: jdbc:mysql://url:port/database
username: root
password: root
hikari:
maximum-pool-size: 3
connection-timeout: 5002.2 排除Spring datasource 自动化配置依赖
因为 Spring Boot 会进行自动装配你的配置,为了使你自定义配置生效,需求在@SpringBootApplication 中排除掉和 datasoure 相关的配置。主要有三个:
- 数据源:DataSourceAutoConfiguration.class
- 事务:DataSourceTransactionManagerAutoConfiguration.class
- Jdbc模板:JdbcTemplateAutoConfiguration.class
@SpringBootApplication(exclude = {
DataSourceAutoConfiguration.class,
DataSourceTransactionManagerAutoConfiguration.class,
JdbcTemplateAutoConfiguration.class
})
public class PostgresqlApplication {
public static void main(String[] args) {
SpringApplication.run(PostgresqlApplication.class, args);
}
}三、数据源配置
做完以上基础配置后,就需要配置具体的Bean了,每个数据源的建议独立配置便于后期维护。数据源配置要经历两个主要的步骤:
- 源数据的配置(读取配置文件、生成新的DataSource)
- 指定ORM 框架在什么地方使用该数据源
这两个是框架无法赋予你的,只能手动配置。
3.1 MySQL 数据源配置
package com.senn.postgresql.config;
import com.zaxxer.hikari.HikariDataSource;
import lombok.extern.slf4j.Slf4j;
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.autoconfigure.jdbc.DataSourceProperties;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.DependsOn;
import org.springframework.context.annotation.Primary;
import org.springframework.jdbc.datasource.DataSourceTransactionManager;
import javax.sql.DataSource;
/**
* @author senn
* @version 1.0.0
* @ClassName MsqlDataSourceConfig.java
* @Description mysql 数据库配置
* @createTime 2021年01月15日 09:02:00
*/
@Configuration
@Slf4j
@MapperScan(basePackages = "com.senn.postgresql.common.mapper.mysql",
sqlSessionFactoryRef = "mysqlDataSourceFactory")
public class MsqlDataSourceConfig {
/**
* @description mysql DataSource Properties 配置
* @updateTime 2021/1/15 9:04
*/
@Bean(name = "mysqlDataSourceProperties")
@ConfigurationProperties("spring.datasource.mysql")
public DataSourceProperties mysqlDataSourceProperties() {
return new DataSourceProperties();
}
/**
* @description mysql DataSource 源配置
* @updateTime 2021/1/15 9:11
*/
@Bean(name = "mysqlDataSource")
@ConfigurationProperties("spring.datasource.mysql.configuration")
public javax.sql.DataSource mysqlDataSource() {
DataSourceProperties dataSourceProperties = mysqlDataSourceProperties();
log.info(dataSourceProperties.getUrl());
// 数据库连接池设置为 Hikari
return mysqlDataSourceProperties().initializeDataSourceBuilder().type(HikariDataSource.class).build();
}
// 以下内容和 mybatis 相关
/**
* @description mysql 工厂配置
* @updateTime 2021/1/15 9:18
*/
@Bean("mysqlDataSourceFactory")
@DependsOn("mysqlDataSource")
public SqlSessionFactory dataSourceFactory() throws Exception {
SqlSessionFactoryBean factoryBean = new SqlSessionFactoryBean();
factoryBean.setDataSource(mysqlDataSource());
return factoryBean.getObject();
}
/**
* @description mysql session 模板配置
* @updateTime 2021/1/15 9:22
*/
@Bean("mysqlSqlSessionTemplate")
@DependsOn("mysqlDataSourceFactory")
public SqlSessionTemplate sqlSessionTemplate(@Qualifier("mysqlDataSourceFactory") SqlSessionFactory sessionFactory) {
return new SqlSessionTemplate(sessionFactory);
}
/**
* @description mysql 事务配置
* @updateTime 2021/1/15 9:22
*/
@Bean(name = "mysqlTransactionManager")
@DependsOn("mysqlDataSource")
public DataSourceTransactionManager mysqlTransactionManager(@Qualifier("mysqlDataSource") DataSource dataSource) {
return new DataSourceTransactionManager(dataSource);
}
}Postgresql 数据源配置
package com.senn.postgresql.config;
import com.zaxxer.hikari.HikariDataSource;
import lombok.extern.slf4j.Slf4j;
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.autoconfigure.jdbc.DataSourceProperties;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.DependsOn;
import org.springframework.context.annotation.Primary;
import org.springframework.jdbc.datasource.DataSourceTransactionManager;
import javax.sql.DataSource;
/**
* @author senn
* @version 1.0.0
* @ClassName DataSourceConfig.java
* @Description psotgresql 数据库配置
* @createTime 2021年01月14日 15:56:00
*/
@Configuration
@Slf4j
@MapperScan(basePackages = "com.senn.postgresql.common.mapper.postgresql",
sqlSessionFactoryRef = "postgresqlDataSourceFactory")
public class PostgresqlDataSourceConfig {
/**
* @description postgresql DataSource Properties 配置
* @updateTime 2021/1/15 9:04
*/
@Bean(name = "postgresqlDataSourceProperties")
@ConfigurationProperties("spring.datasource.postgresql")
public DataSourceProperties postgresqlDataSourceProperties() {
return new DataSourceProperties();
}
/**
* @description postgresql DataSource 源配置
* @param
* @updateTime 2021/1/15 9:11
* @throws
*/
@Bean(name = "postgresqlDataSource")
@ConfigurationProperties("spring.datasource.postgresql.configuration")
public DataSource postgresqlDataSource() {
DataSourceProperties dataSourceProperties = postgresqlDataSourceProperties();
log.info(dataSourceProperties.getUrl());
// 数据库连接池设置为 Hikari
return postgresqlDataSourceProperties().initializeDataSourceBuilder().type(HikariDataSource.class).build();
}
// 以下内容和 mybatis 相关
/**
* @description postgresql 工程配置
* @updateTime 2021/1/15 9:18
*/
@Bean("postgresqlDataSourceFactory")
@DependsOn("postgresqlDataSource")
public SqlSessionFactory dataSourceFactory() throws Exception {
SqlSessionFactoryBean factoryBean = new SqlSessionFactoryBean();
factoryBean.setDataSource(postgresqlDataSource());
return factoryBean.getObject();
}
/**
* @description postgresql session 模板配置
* @updateTime 2021/1/15 9:22
*/
@Bean("postgresqlSqlSessionTemplate")
@Primary
@DependsOn("postgresqlDataSourceFactory")
public SqlSessionTemplate sqlSessionTemplate(@Qualifier("postgresqlDataSourceFactory") SqlSessionFactory sessionFactory) {
return new SqlSessionTemplate(sessionFactory);
}
/**
* @description postgresql 事务配置
* @updateTime 2021/1/15 9:22
*/
@Bean(name = "postgresqlTransactionManager")
@DependsOn("postgresqlDataSource")
public DataSourceTransactionManager fawkesTransactionManager(@Qualifier("postgresqlDataSource") DataSource dataSource) {
return new DataSourceTransactionManager(dataSource);
}
}@Senn · 森
今天关于《Spring Boot Mybatis 多数据源配置》的内容介绍就到此结束,如果有什么疑问或者建议,可以在golang学习网公众号下多多回复交流;文中若有不正之处,也希望回复留言以告知!
版本声明
本文转载于:SegmentFault 如有侵犯,请联系study_golang@163.com删除
Mac 安装mysql的踩坑:使用可视化sequel pro软件连接本地数据库失败
- 上一篇
- Mac 安装mysql的踩坑:使用可视化sequel pro软件连接本地数据库失败
- 下一篇
- Mybatis【13】-- Mybatis动态Sql标签的使用
评论列表
-
- 无辜的白昼
- 这篇技术文章真是及时雨啊,太详细了,很有用,已加入收藏夹了,关注作者大大了!希望作者大大能多写数据库相关的文章。
- 2023-01-25 02:25:07
查看更多
最新文章
-
- 数据库 · MySQL | 1天前 | MySQL · 执行计划 · 慢查询 · MySQL EXPLAIN ANALYZE MySQL估算行数实际行数 MySQL执行计划耗时 MySQL慢查询诊断 MySQL TREE执行计划
- MySQL EXPLAIN ANALYZE对比估算行数与实际耗时的实现方法
- 262浏览 收藏
-
- 数据库 · MySQL | 2天前 |
- MySQL CHECK 约束上线前清理不符合约束的旧数据的实现方法
- 475浏览 收藏
-
- 数据库 · MySQL | 2天前 |
- MySQL 在线 DDL评估加索引时的锁与空间的实现方法
- 202浏览 收藏
-
- 数据库 · MySQL | 2天前 | MySQL · ROW_NUMBER PARTITION BY MySQL 窗口函数 每组最新记录 分组取最新
- MySQL 窗口函数按分组取每组最新记录的实现方法
- 185浏览 收藏
-
- 数据库 · MySQL | 2天前 | MySQL · mysql 临时表 Performance Schema temptable_max_ram tmp_table_size
- MySQL temptable_max_ram 观察临时表内存阈值如何设置
- 409浏览 收藏
-
- 数据库 · MySQL | 2天前 | MySQL · 索引优化 · mysql Invisible Index 索引可见性
- MySQL invisible index 试验结束后如何恢复可见
- 118浏览 收藏
-
- 数据库 · MySQL | 2天前 |
- MySQL 角色继承权限后 SHOW GRANTS 如何解读
- 101浏览 收藏
-
- 数据库 · MySQL | 2天前 |
- MySQL 复制状态中 Retrieved_Gtid_Set 如何辅助定位缺口
- 221浏览 收藏
-
- 数据库 · MySQL | 2天前 | MySQL · mysql Performance Schema events_statements 平均耗时
- MySQL Performance Schema events_statements 如何找平均耗时
- 125浏览 收藏
-
- 数据库 · MySQL | 2天前 |
- MySQL LOAD DATA 导入 TSV 时如何处理字段内制表符
- 253浏览 收藏
查看更多
课程推荐
-
- 前端进阶之JavaScript设计模式
- 设计模式是开发人员在软件开发过程中面临一般问题时的解决方案,代表了最佳的实践。本课程的主打内容包括JS常见设计模式以及具体应用场景,打造一站式知识长龙服务,适合有JS基础的同学学习。
- 543次学习
-
- GO语言核心编程课程
- 本课程采用真实案例,全面具体可落地,从理论到实践,一步一步将GO核心编程技术、编程思想、底层实现融会贯通,使学习者贴近时代脉搏,做IT互联网时代的弄潮儿。
- 516次学习
-
- 简单聊聊mysql8与网络通信
- 如有问题加微信:Le-studyg;在课程中,我们将首先介绍MySQL8的新特性,包括性能优化、安全增强、新数据类型等,帮助学生快速熟悉MySQL8的最新功能。接着,我们将深入解析MySQL的网络通信机制,包括协议、连接管理、数据传输等,让
- 500次学习
-
- JavaScript正则表达式基础与实战
- 在任何一门编程语言中,正则表达式,都是一项重要的知识,它提供了高效的字符串匹配与捕获机制,可以极大的简化程序设计。
- 487次学习
-
- 从零制作响应式网站—Grid布局
- 本系列教程将展示从零制作一个假想的网络科技公司官网,分为导航,轮播,关于我们,成功案例,服务流程,团队介绍,数据部分,公司动态,底部信息等内容区块。网站整体采用CSSGrid布局,支持响应式,有流畅过渡和展现动画。
- 485次学习
查看更多
AI推荐
-
- PubMedQA
- 深入了解PubMedQA生物医学问答数据集,涵盖其核心功能、使用方法及在临床决策、药物研发等场景的应用,助力提升NLP模型性能。
- 82次使用
-
- H2O EvalGPT
- H2O EvalGPT是H2O.ai推出的开源LLM评估平台,提供详细的大模型性能排行榜、行业特定基准测试及A/B测试功能,助您快速选择最适合项目的高性能大语言模型。
- 166次使用
-
- LMArena
- LMArena是加州大学伯克利分校推出的AI模型匿名评测平台。通过盲测投票机制,用户可对比不同大模型回答并生成实时排行榜,助力开发者优化模型及用户选择最佳AI工具。
- 108次使用
-
- HELM
- 深入了解斯坦福推出的HELM(Holistic Evaluation of Language Models)大模型评测体系。本文解析其核心功能、安装配置步骤及应用场景,涵盖准确性、公平性、鲁棒性等多维度指标,助力开发者全面优化语言模型性能。
- 83次使用
-
- CMMLU
- 深入了解CMMLU中文评估基准,涵盖67个学科主题,提供数据集下载、Zero-shot/Five-shot评估方法及排行榜,助力优化中文语言模型性能。
- 60次使用
查看更多
相关文章
-
- MySQL 明明加了索引,为什么查询还是很慢?先查这 6 个点
- 2026-06-27 374浏览
-
- golang MySQL实现对数据库表存储获取操作示例
- 2022-12-22 499浏览
-
- golang 基于 mysql 简单实现分布式读写锁
- 2023-01-07 384浏览
-
- 详解如何利用GORM实现MySQL事务
- 2023-01-07 184浏览
-
- Go语言实现操作MySQL的基础知识总结
- 2023-01-23 265浏览

