springboot 不同类型多数据源配置及使用
springboot多数据源配置:
datasource.master.jdbc=jdbc:mysql://localhost:3306/test?useUnicode=true&characterEncoding=UTF-8&useSSL=true
datasource.master.username=root
datasource.master.password=123456
datasource.master.driver-class-name=com.mysql.jdbc.Driver
datasource.master.url=jdbc:oracle:thin:@localhost:1521:test
datasource.master.username=acct
datasource.master.password=123456
datasource.master.driver-class-name=oracle.jdbc.driver.OracleDriver
datasource.master.max-idle=10
datasource.master.max-wait=10000
datasource.master.min-idle=5
datasource.master.initial-size=5
datasource.master.validation-query=SELECT 1
datasource.master.test-on-borrow=false
datasource.master.test-while-idle=true
datasource.master.time-between-eviction-runs-millis=18800
第一个数据源实现:
import javax.sql.DataSource;
import org.apache.ibatis.session.SqlSessionFactory;
import org.mybatis.spring.SqlSessionFactoryBean;
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.context.annotation.PropertySource;
import org.springframework.core.io.support.PathMatchingResourcePatternResolver;
import org.springframework.jdbc.datasource.DataSourceTransactionManager;
/**
* 主数据源配置
* @author zyl
*
*/
@Configuration
//读取自定义配置文件 datasource.properties
@PropertySource("classpath:config/datasource.properties")
//扫描 Mapper 接口并容器管理
@MapperScan(basePackages = MasterDataSourceConfig.PACKAGE, sqlSessionFactoryRef = "masterSqlSessionFactory")
public class MasterDataSourceConfig {
// 精确到 master 目录,以便跟其他数据源隔离
static final String PACKAGE = "com.acct.dao.master";
static final String TYPE_ALIASES_PACKAGE = "com.acct.domain.entity.master";
static final String MAPPER_LOCATION = "classpath:mapper/master/*.xml";
@Bean(name = "masterDataSource")
@Primary
@ConfigurationProperties(prefix = "datasource.master")
public DataSource masterDataSource() {
return DataSourceBuilder.create().build();
}
@Bean(name = "masterTransactionManager")
@Primary
public DataSourceTransactionManager masterTransactionManager() {
return new DataSourceTransactionManager(masterDataSource());
}
@Bean(name = "masterSqlSessionFactory")
@Primary
public SqlSessionFactory masterSqlSessionFactory(@Qualifier("masterDataSource") DataSource masterDataSource)
throws Exception {
final SqlSessionFactoryBean sessionFactory = new SqlSessionFactoryBean();
sessionFactory.setDataSource(masterDataSource);
sessionFactory.setTypeAliasesPackage(MasterDataSourceConfig.TYPE_ALIASES_PACKAGE);
sessionFactory.setMapperLocations(
new PathMatchingResourcePatternResolver().getResources(MasterDataSourceConfig.MAPPER_LOCATION));
return sessionFactory.getObject();
}
}
@Primary 注解默认为第一个数据源
第二个数据源实现:
import javax.sql.DataSource;
import org.apache.ibatis.session.SqlSessionFactory;
import org.mybatis.spring.SqlSessionFactoryBean;
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.PropertySource;
import org.springframework.core.io.support.PathMatchingResourcePatternResolver;
import org.springframework.jdbc.datasource.DataSourceTransactionManager;
/**
* 从数据源配置
* @author zyl
*
*/
@Configuration
//读取自定义配置文件 datasource.properties
@PropertySource("classpath:config/datasource.properties")
// 扫描 Mapper 接口并容器管理
@MapperScan(basePackages = ClusterDataSourceConfig.PACKAGE, sqlSessionFactoryRef = "clusterSqlSessionFactory")
public class ClusterDataSourceConfig {
// 精确到 cluster 目录,以便跟其他数据源隔离
static final String PACKAGE = "com.acct.dao.cluster";
static final String TYPE_ALIASES_PACKAGE = "com.acct.domain.entity.cluster";
static final String MAPPER_LOCATION = "classpath:mapper/cluster/*.xml";
@Bean(name = "clusterDataSource")
@ConfigurationProperties(prefix = "datasource.cluster")
public DataSource clusterDataSource() {
return DataSourceBuilder.create().build();
}
@Bean(name = "clusterTransactionManager")
public DataSourceTransactionManager clusterTransactionManager() {
return new DataSourceTransactionManager(clusterDataSource());
}
@Bean(name = "clusterSqlSessionFactory")
public SqlSessionFactory clusterSqlSessionFactory(@Qualifier("clusterDataSource") DataSource clusterDataSource)
throws Exception {
final SqlSessionFactoryBean sessionFactory = new SqlSessionFactoryBean();
sessionFactory.setDataSource(clusterDataSource);
sessionFactory.setTypeAliasesPackage(ClusterDataSourceConfig.TYPE_ALIASES_PACKAGE);
sessionFactory.setMapperLocations(
new PathMatchingResourcePatternResolver().getResources(ClusterDataSourceConfig.MAPPER_LOCATION));
return sessionFactory.getObject();
}
}
目录结构:

如有问题:请看我的下一篇文章:https://www.cnblogs.com/haoliyou/p/9604241.html

启动正常,ok!
springboot 不同类型多数据源配置及使用的更多相关文章
- 记springboot + MP +Hikari动态数据源配置
环境准备: springboot 2.1.6 mybatis-plus 数据库驱动 boot 自带hikari驱动 步骤1: 导入多数据源启动工具类 <!-- 多数据源支持 -->< ...
- springboot:mybatis多数据源配置
1.application.properties #CMS数据源(主库) spring.datasource.cms.driver-class-name=com.mysql.jdbc.Driver s ...
- SpringBoot入门之基于Druid配置Mybatis多数据源
上一篇了解了Druid进行配置连接池的监控和慢sql处理,这篇了解下使用基于基于Druid配置Mybatis多数据源.SpringBoot默认配置数据库连接信息时只需设置url等属性信息就可以了,Sp ...
- 【spring boot】12.spring boot对多种不同类型数据库,多数据源配置使用
2天时间,终于把spring boot下配置连接多种不同类型数据库,配置多数据源实现! ======================================================== ...
- 使用springboot + druid + mybatisplus完成多数据源配置
一. 简介 1. 版本 springboot版本为2.0.3.RELEASE,mybatisplus版本为2.1.9, druid版本为1.1.9,swagger版本为2.7.0 2. 项目地址 ...
- Spring-Boot 多数据源配置+动态数据源切换+多数据源事物配置实现主从数据库存储分离
一.基础介绍 多数据源字面意思,比如说二个数据库,甚至不同类型的数据库.在用SpringBoot开发项目时,随着业务量的扩大,我们通常会进行数据库拆分或是引入其他数据库,从而我们需要配置多个数据源. ...
- Springboot 多数据源配置,结合tk-mybatis
一.前言 作为一个资深的CRUD工程师,我们在实际使用springboot开发项目的时候,难免会遇到同时使用多个数据库的情况,比如前脚刚查询mysql,后脚就要查询sqlserver. 这时,我们很直 ...
- springboot v2.0.3版本多数据源配置
本篇分享的是springboot多数据源配置,在从springboot v1.5版本升级到v2.0.3时,发现之前写的多数据源的方式不可用了,捕获错误信息如: 异常:jdbcUrl is requir ...
- springboot之多数据源配置JdbcTemplate
springboot多数据源配置,代码如下 DataSourceConfig package com.rookie.bigdata.config; import org.springframework ...
随机推荐
- Vue插件配置和 后台交互
Vue插件配置和 后台交互 一.全局配置静态文件(csss, js..) 1.1 全局配置css文件 创建css文件 在main.js配置css文件 // 配置全局css样式 // 方式一 impor ...
- Python 进阶 - 面向对象
Python 面向对象 面向过程 把完成某个需求的所有步骤,从头到尾逐步实现 根据开发需求,将某些功能独立的代码封装成一个又一个函数 最后完成的代码,就是顺序地调用不同的函数 面向过程特点: 注重步骤 ...
- Intellij IDEA中mybatis-generator自动生成
一.在maven工程中的resource中创建generatorConfig.xml 二.配置generatorConfig.xml: <?xml version="1.0" ...
- drf框架的解析模块-异常处理模块-响应模块-序列化模块
解析模块 为什么要配置解析模块 (1).drf给我们通过了多种解析数据包方式的解析类. (2).我们可以通过配置来控制前台提交的那些格式的数据台解析,那些数据不解析. (3).全局配置就是针对一个视图 ...
- index|substr
#!/usr/bin/perl use strict; use warnings; $_ = 'duwanxkm,c.,df;oq123@#!@%$#^'; my $d ='d';my $o = 'o ...
- [转]Apache漏洞利用与安全加固实例分析
1.2 Apache文件解析特性 Apache对于文件名的解析是从后往前解析的,直到遇见一个它认识的文件类型为止.因此,如果web目录下存在以类似webshell.php.test这样格式命名的文件, ...
- [HNOI2019]JOJO(KMP自动机+主席树)
一道神仙题,考察选手对KMP的深入理解. 先考虑没有2操作的做法.设每一段为一个二元组(x,c),考虑一段前缀匹配后缀,除了第一段的字符,其他段的二元组(x,c)必须相等,所以可以将其视为特殊字符进行 ...
- Canal —— 基本概念及使用
参考文档 开源数据同步神器--canal [若泽大数据]大数据之实时数据源同步中间件--生产上Canal与Maxwell颠峰对决
- 编译原理_P1002
. 词法分析 1.1 词法记号及属性 词法记号.模式.词法单元 记号名 词法单元列举 模式的非形式描述 if if 字符i,f for for 字符f,o,r relation < ...
- jumpserver_跳板机实操
首先,jumpserver是什么呢? Jumpserver 是一款由Python编写开源的跳板机(堡垒机)系统,实现了跳板机应有的功能.基于ssh协议来管理,客户端无需安装agent. 特点: 完全开 ...