spring boot druid mybatis 多数据源 配置
2023-09-27 14:22:28 时间
spring boot 在配置时做了很多简化配置的设置,但是简化的配置往往已牺牲一定的定制化,比如在数据源的配置时,spring boot 只提供4种数据库连接池的配置,其中并不支持常用的druid
阅读spring boot DataSourceBuilder 的源码可以发现 spring boot 提供的4种数据源类型并不是我们想要的
private static final String[] DATA_SOURCE_TYPE_NAMES = new String[] { "org.apache.tomcat.jdbc.pool.DataSource", "com.zaxxer.hikari.HikariDataSource", "org.apache.commons.dbcp.BasicDataSource", // deprecated "org.apache.commons.dbcp2.BasicDataSource" };
但是 DataSourceBuilder 提供了type方法来自定义DataSource类型
public DataSourceBuilder type(Class ? extends DataSource type) { this.type = type; return this; }
知道了方法,下面配置就简单许多了
首先是application.properties 文件的配置
spring.datasource.sso.url=jdbc:mysql://localhost:3306/sso?useSSL=false spring.datasource.sso.username=root spring.datasource.sso.password=root spring.datasource.sso.driver-class-name=com.mysql.jdbc.Driver spring.datasource.sso.max-idle=5 spring.datasource.sso.max-wait=10000 spring.datasource.sso.min-idle=1 spring.datasource.sso.initial-size=1 spring.datasource.sso.validation-query=SELECT 1 spring.datasource.sso.test-on-borrow=false spring.datasource.sso.test-while-idle=true spring.datasource.sso.time-between-eviction-runs-millis=18800 spring.datasource.message.url=jdbc:mysql://localhost:3306/message?useSSL=false spring.datasource.message.username=root spring.datasource.message.password=root spring.datasource.message.driver-class-name=com.mysql.jdbc.Driver spring.datasource.message.max-idle=5 spring.datasource.message.max-wait=10000 spring.datasource.message.min-idle=1 spring.datasource.message.initial-size=1 spring.datasource.message.validation-query=SELECT 1 spring.datasource.message.test-on-borrow=false spring.datasource.message.test-while-idle=true spring.datasource.message.time-between-eviction-runs-millis=18800
然后是具体的主数据源配置类
@Configuration @MapperScan(basePackages = {"org.vergil.demo.core.dao.mapper.sso"}, sqlSessionFactoryRef = "ssoSqlSessionFactory") public class SsoConfig { @Bean(name = "ssoDataSource") @ConfigurationProperties(prefix = "spring.datasource.sso") @Primary public DataSource ssoDataSource() { //指定使用DruidDataSource return DataSourceBuilder.create().type(com.alibaba.druid.pool.DruidDataSource.class).build(); @Bean(name = "ssoSqlSessionFactory") @Primary public SqlSessionFactory testSqlSessionFactory(@Qualifier("ssoDataSource") DataSource dataSource) throws Exception { SqlSessionFactoryBean bean = new SqlSessionFactoryBean(); bean.setDataSource(dataSource); // bean.setMapperLocations(new PathMatchingResourcePatternResolver().getResources("classpath:mapper/sso/*.xml")); return bean.getObject(); @Primary @Bean(name = "ssoTransactionManager") public DataSourceTransactionManager testTransactionManager(@Qualifier("ssoDataSource") DataSource dataSource) { return new DataSourceTransactionManager(dataSource); @Primary @Bean(name = "ssoSqlSessionTemplate") public SqlSessionTemplate testSqlSessionTemplate(@Qualifier("ssoSqlSessionFactory") SqlSessionFactory sqlSessionFactory) throws Exception { return new SqlSessionTemplate(sqlSessionFactory); }
@ConfigurationProperties(prefix = "spring.datasource.sso") 引入配置项
使用如下方式来创建DruidDataSource,简化配置
return DataSourceBuilder.create().type(com.alibaba.druid.pool.DruidDataSource.class).build();
第二数据源
@Configuration @MapperScan(basePackages = {"org.vergil.demo.core.dao.mapper.message"}, sqlSessionFactoryRef = "messageSqlSessionFactory") public class MessageConfig { @Bean(name = "messageDataSource") @ConfigurationProperties(prefix = "spring.datasource.message") public DataSource messageDataSource() { return DataSourceBuilder.create().type(com.alibaba.druid.pool.DruidDataSource.class).build(); @Bean(name = "messageSqlSessionFactory") public SqlSessionFactory testSqlSessionFactory(@Qualifier("messageDataSource") DataSource dataSource) throws Exception { SqlSessionFactoryBean bean = new SqlSessionFactoryBean(); bean.setDataSource(dataSource); // bean.setMapperLocations(new PathMatchingResourcePatternResolver().getResources("classpath:mapper/message/*.xml")); return bean.getObject(); @Bean(name = "messageTransactionManager") public DataSourceTransactionManager testTransactionManager(@Qualifier("messageDataSource") DataSource dataSource) { return new DataSourceTransactionManager(dataSource); @Bean(name = "messageSqlSessionTemplate") public SqlSessionTemplate testSqlSessionTemplate(@Qualifier("messageSqlSessionFactory") SqlSessionFactory sqlSessionFactory) throws Exception { return new SqlSessionTemplate(sqlSessionFactory); }
数据源配置完毕
每个数据源都会生成自己的sqlSession,相互独立
springboot+mybatis配置多数据源实战 一般一个项目中只会连接一个数据库.但是随着需求变更,会要求同一个项目中连接多个数据库,本文就讲一下如何在一个项目中对多个数据库进行连接.本文基于springboot+mybatis介绍如何进行多数据源连接(本文演示配置两个数据库,配置多个同理).
相关文章
- springboot项目:以run as-->spring boot app方式启动,配置热部署(亲测可用!!!)
- 在Spring Boot中使用内存数据库
- 在Spring Boot中加载初始化数据
- 自定义spring boot的自动配置
- Spring 框架基础(06):Mvc架构模式简介,执行流程详解
- Activiti7 与 Spring Boot 及 Spring Security 整合 踩坑记录
- Prometheus + Spring Boot 应用监控
- Spring Boot Cache配置 序列化成JSON字符串
- 《深入实践Spring Boot》一1.5 关于Spring Boot配置
- 开源UReport 整合到产品中实践简要:(一)spring boot下UReport安装与配置()
- CSDN日报191028:冷眼看中国IT20年发展史;Vue + Spring Boot 项目实战
- Spring Boot内嵌的tomcat日志
- 面试题:Spring Boot 自动配置原理
- Spring Boot 2.x基础教程:使用 Thymeleaf开发Web页面
- spring-boot-redis-cluster简单整合例子
- Spring boot——logback.xml 配置详解(二)
- Spring Boot 注入外部配置到应用内部的静态变量
- CentOS7 下 配置Docker远程访问 与 windows下使用maven构筑Spring Boot 的 Docker镜像到远程服务端
- Spring Boot—04文件上传
- Spring Boot 2 使用自定义配置
- spring boot高性能实现二维码扫码登录(中)——Redis版
- spring boot多数据源配置(mysql,redis,mongodb)实战
- 使用Spring Boot Actuator、Jolokia和Grafana实现准实时监控--转
- 曹工说Spring Boot源码(29)-- Spring 解决循环依赖为什么使用三级缓存,而不是二级缓存
- 曹工说Spring Boot源码(25)-- Spring注解扫描的瑞士军刀,ASM + Java Instrumentation,顺便提提Jar包破解
- spring boot 日志配置 默认的日志配置
- spring boot (5) 自定义配置
- spring boot (2) 配置swagger2核心配置 docket
- 12 Spring Boot密码加密算法
- spring boot 开静态资源访问,配置视图解析器
- Spring_Boot下Spring_Batch入门实例
- Spring Boot 2.X 加载 so 库