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 ...
随机推荐
- Linux学习(二) --- 常用命令
[TOC] 一.常用命令 1.目录 cd切换 cd 路径 切换到指定路径 cd .. 上一级 mkdir:创建目录 mkdir 目录名 pwd:查看 2.查看 ll命令:查询目录内容 ll 查看当前( ...
- Python模块——json
简介 json全名是JavaScript Object Notation(即:Javascript对象标记).它是JavaScript的子集,JSON是轻量级的文本数据交换格式.前端和后端进行数据交互 ...
- 2019收藏盘点(编程语言/AI/面试/实用工具)
2020.1.5更新 我看过的后面会加上评价 编程学习 java开源项目汇总: https://github.com/Snailclimb/awesome-java 大数据学习入门: https:// ...
- Proe 导出PDF Vb.net
Private Sub btnStart_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSt ...
- 吴裕雄--天生自然 PYTHON3开发学习:条件控制
if condition_1: statement_block_1 elif condition_2: statement_block_2 else: statement_block_3 var1 = ...
- 903A. Hungry Student Problem#饥饿的学生(暴力&双层枚举)
题目出处:http://codeforces.com/problemset/problem/903/A 题目大意就是:一个数能否用正整数个另外两个数合成 #include<iostream> ...
- nginx简单安装
虚拟机首先要求ping www.baidu.com 下载: 解压: 创建用户: [root@nginx ~]# useradd -M -s /sbin/nologin nginx-M 不创建加目录 ...
- Sequence Diagram时序图 - 应该是最简洁有力的业务了
直接看UML吧,一目了然,不用解释.自信男人,无须多言. 这是用ListView显示Post的流程. 这是Uppdate User Profile的流程.自信男人,无须多言.
- [USACO09DEC]雪橇Bobsledding(贪心)
https://www.luogu.org/problem/P2968 题目描述 Bessie has entered a bobsled competition because she hopes ...
- poj-3657 Haybale Guessing(二分答案+并查集)
http://poj.org/problem?id=3657 下方有中文版,不想看英文的可直接点这里看中文版题目 Description The cows, who always have an in ...