Spring Boot Mybatis 多数据源配置
来源:SegmentFault
2023-01-24 14:59:14
0浏览
收藏
编程并不是一个机械性的工作,而是需要有思考,有创新的工作,语法是固定的,但解决问题的思路则是依靠人的思维,这就需要我们坚持学习和更新自己的知识。今天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: 500
2.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软件连接本地数据库失败

- 下一篇
- Mybatis【13】-- Mybatis动态Sql标签的使用
评论列表
-
- 无辜的白昼
- 这篇技术文章真是及时雨啊,太详细了,很有用,已加入收藏夹了,关注作者大大了!希望作者大大能多写数据库相关的文章。
- 2023-01-25 02:25:07
查看更多
最新文章
-
- 数据库 · MySQL | 1小时前 | mysql 字符集 中文乱码 utf8mb4 utf8mb4_unicode_ci
- MySQL中文乱码解决方案与字符集修改命令大全
- 339浏览 收藏
-
- 数据库 · MySQL | 1天前 | 索引 数据类型 字符集 存储引擎 CREATETABLE
- MySQL新建表操作指南与建表技巧
- 462浏览 收藏
-
- 数据库 · MySQL | 1个月前 | 条件判断
- CASEWHEN条件判断的嵌套使用详解与实战场景分析
- 469浏览 收藏
-
- 数据库 · MySQL | 1个月前 | java php
- CSV文件批量导入MySQL的性能优化秘籍大揭秘
- 289浏览 收藏
-
- 数据库 · MySQL | 1个月前 |
- GaleraCluster多主集群配置与冲突解决攻略
- 239浏览 收藏
-
- 数据库 · MySQL | 1个月前 | 窗口函数实战
- MySQL窗口函数实战案例深度剖析
- 315浏览 收藏
查看更多
课程推荐
-
- 前端进阶之JavaScript设计模式
- 设计模式是开发人员在软件开发过程中面临一般问题时的解决方案,代表了最佳的实践。本课程的主打内容包括JS常见设计模式以及具体应用场景,打造一站式知识长龙服务,适合有JS基础的同学学习。
- 542次学习
-
- GO语言核心编程课程
- 本课程采用真实案例,全面具体可落地,从理论到实践,一步一步将GO核心编程技术、编程思想、底层实现融会贯通,使学习者贴近时代脉搏,做IT互联网时代的弄潮儿。
- 508次学习
-
- 简单聊聊mysql8与网络通信
- 如有问题加微信:Le-studyg;在课程中,我们将首先介绍MySQL8的新特性,包括性能优化、安全增强、新数据类型等,帮助学生快速熟悉MySQL8的最新功能。接着,我们将深入解析MySQL的网络通信机制,包括协议、连接管理、数据传输等,让
- 497次学习
-
- JavaScript正则表达式基础与实战
- 在任何一门编程语言中,正则表达式,都是一项重要的知识,它提供了高效的字符串匹配与捕获机制,可以极大的简化程序设计。
- 487次学习
-
- 从零制作响应式网站—Grid布局
- 本系列教程将展示从零制作一个假想的网络科技公司官网,分为导航,轮播,关于我们,成功案例,服务流程,团队介绍,数据部分,公司动态,底部信息等内容区块。网站整体采用CSSGrid布局,支持响应式,有流畅过渡和展现动画。
- 484次学习
查看更多
AI推荐
-
- 笔灵AI生成答辩PPT
- 探索笔灵AI生成答辩PPT的强大功能,快速制作高质量答辩PPT。精准内容提取、多样模板匹配、数据可视化、配套自述稿生成,让您的学术和职场展示更加专业与高效。
- 20次使用
-
- 知网AIGC检测服务系统
- 知网AIGC检测服务系统,专注于检测学术文本中的疑似AI生成内容。依托知网海量高质量文献资源,结合先进的“知识增强AIGC检测技术”,系统能够从语言模式和语义逻辑两方面精准识别AI生成内容,适用于学术研究、教育和企业领域,确保文本的真实性和原创性。
- 29次使用
-
- AIGC检测-Aibiye
- AIbiye官网推出的AIGC检测服务,专注于检测ChatGPT、Gemini、Claude等AIGC工具生成的文本,帮助用户确保论文的原创性和学术规范。支持txt和doc(x)格式,检测范围为论文正文,提供高准确性和便捷的用户体验。
- 35次使用
-
- 易笔AI论文
- 易笔AI论文平台提供自动写作、格式校对、查重检测等功能,支持多种学术领域的论文生成。价格优惠,界面友好,操作简便,适用于学术研究者、学生及论文辅导机构。
- 43次使用
-
- 笔启AI论文写作平台
- 笔启AI论文写作平台提供多类型论文生成服务,支持多语言写作,满足学术研究者、学生和职场人士的需求。平台采用AI 4.0版本,确保论文质量和原创性,并提供查重保障和隐私保护。
- 36次使用
查看更多
相关文章
-
- golang MySQL实现对数据库表存储获取操作示例
- 2022-12-22 499浏览
-
- 搞一个自娱自乐的博客(二) 架构搭建
- 2023-02-16 244浏览
-
- B-Tree、B+Tree以及B-link Tree
- 2023-01-19 235浏览
-
- mysql面试题
- 2023-01-17 157浏览
-
- MySQL数据表简单查询
- 2023-01-10 101浏览