本文主要介绍如何在一个springboot项目配置两个数据源(mysql和oracle);

1、引进相关依赖

<!-- https://mvnrepository.com/artifact/mysql/mysql-connector-java -->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<scope>runtime</scope>
</dependency>
<!--oracle 驱动 -->
<dependency>
<groupId>com.oracle</groupId>
<artifactId>ojdbc6</artifactId>
<version>11.1.0.7.0</version>
</dependency>

当ojdbc驱动包版本过低,会报如下图所示错误,即驱动jar与数据库版本不兼容:

2、于applicationContext.yml中配置数据源连接参数:

     Spring:
datasource:
base:
driver-class-name: com.mysql.jdbc.Driver
jdbc-url: ${base.db.url}
username: ${base.db.username}
password: ${base.db.password}
oa:
driver-class-name: oracle.jdbc.driver.OracleDriver
jdbc-url: ${oa.db.urL}
username: ${oa.db.username}
password: ${oa.db.password}
hikari:
max-lifetime: 60000
login-timeout: 5
validation-timeout: 3000
connection-timeout: 60000
idle-timeout: 60000

3、多数据源配置文件,读取对应连接参数

Package com.**.config;

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 javax.sql.DataSource; public class MultiDataSourceConfig { @Bean (name = "primaryDataSource")
@Qualifier("primaryDataSource")
@Primary //定义主数据源
@ConfigurationProperties (prefix = "spring.datasource.Base")
public DataSource primaryDataSource () { return DataSourceBuilder.create().build (); } @Bean (name = "secondaryDataSource")
@Qualifier ("secondaryDataSource")
@ConfigurationProperties (prefix = "spring.datasource.oa")
public DataSource secondaryDataSource () { return DataSourceBuilder.create().build (); } }

3、主数据源配置,指定扫描对应mapper文件以及对应xml文件,在使用mapper包下的mapper文件时会自动使用对应sql会话

Package com.**.config;

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.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Primary;
import org.springframeworkcore.io.support.PathMatchingResourcePatternResolver;
import org.spr ingframework.jdbc.datasource.DataSourceTransactionManager;
import org.springframework.transaction.PlatformTransactionManager;
import javax.sql.DataSource; @Configuration
@MapperScan (basePackages = "com.**.mapper", sqlSessionTemplateRef = "primarySqlSessionTemplate")
public class SqlSessionTemplate1 { @Bean (name = "primarySqlSessionFactory")
@Primary
public SqlSessionFactory primarySqlSessionFactory (@Qualifier("primaryDataSource") DataSource dataSource)
throws Exception{
SqLSessionFactoryBean bean = new SqlSessionFactoryBean ();
bean.setDataSource (dataSource);
bean.setMapperLocations (new PathMatchingResourcePatternResolver().getResources (locationPattern: "classpath: mybatis/mapper/*. XmL"));
return bean.getObject();
} /**
* 配置声明式事务管理器
*/
@Bean (name = "primaryTransactionManager")
@Primary
public PlatformTransactionManager primaryTransactionManager (@Qualifier("primaryDataSource") DataSource dataSource) {
return new DataSourceTransactionManager (dataSource);
} @Bean (name = "primarySqlSessionTemplate")
@Primary
public SqlSessionTemplatel primarySqlSessionTemplate(@Qualifier("primarySqlSesionFactory") SqlSessionFactory sqlSessionFactory) throws Exception {
return new SqlSessionTemplatel(sqlSessionFactory);
}
}

4、第二数据源配置,指定扫描对应mapper文件以及对应xml文件,在使用mapper包下的mapper文件时会自动使用对应sql会话

Package com.**.config;

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.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.secondary;
import org.springframeworkcore.io.support.PathMatchingResourcePatternResolver;
import org.spr ingframework.jdbc.datasource.DataSourceTransactionManager;
import org.springframework.transaction.PlatformTransactionManager;
import javax.sql.DataSource; @Configuration
@MapperScan (basePackages = "com.**.oraclemapper", sqlSessionTemplateRef = "secondarySqlSessionTemplate")
public class SqlSessionTemplate2 { @Bean (name = "secondarySqlSessionFactory")
public SqlSessionFactory secondarySqlSessionFactory (@Qualifier("secondaryDataSource") DataSource dataSource)
throws Exception{
SqLSessionFactoryBean bean = new SqlSessionFactoryBean ();
bean.setDataSource (dataSource);
bean.setMapperLocations (new PathMatchingResourcePatternResolver().getResources (locationPattern: "classpath: mybatis/oraclemapper/*. XmL"));
return bean.getObject();
} /**
* 配置声明式事务管理器
*/
@Bean (name = "secondaryTransactionManager")
public PlatformTransactionManager secondaryTransactionManager (@Qualifier("secondaryDataSource") DataSource dataSource) {
return new DataSourceTransactionManager (dataSource);
} @Bean (name = "secondarySqlSessionTemplate")
public SqlSessionTemplatel secondarySqlSessionTemplate(@Qualifier("secondarySqlSesionFactory") SqlSessionFactory sqlSessionFactory) throws Exception {
return new SqlSessionTemplatel(sqlSessionFactory);
}
}

至此,在service层可以像单数据源一样使用了。

springboot —— 多数据源的更多相关文章

  1. Spring-Boot配置文件数据源配置项

    Spring-Boot配置文件数据源配置项(常用配置项为红色) 参数 介绍 spring.datasource.continue-on-error = false 初始化数据库时发生错误时,请勿停止 ...

  2. SpringBoot多数据源动态切换数据源

    1.配置多数据源 spring: datasource: master: password: erp_test@abc url: jdbc:mysql://127.0.0.1:3306/M201911 ...

  3. SpringBoot学习笔记(三):SpringBoot集成Mybatis、SpringBoot事务管理、SpringBoot多数据源

    SpringBoot集成Mybatis 第一步我们需要在pom.xml里面引入mybatis相关的jar包 <dependency> <groupId>org.mybatis. ...

  4. 搞定SpringBoot多数据源(1):多套源策略

    目录 1. 引言 2. 运行环境 3. 多套数据源 3.1 搭建 Spring Boot 工程 3.1.1 初始化 Spring Boot 工程 3.1.2 添加 MyBatis Plus 依赖 3. ...

  5. 搞定SpringBoot多数据源(2):动态数据源

    目录 1. 引言 2. 动态数据源流程说明 3. 实现动态数据源 3.1 说明及数据源配置 3.1.1 包结构说明 3.1.2 数据库连接信息配置 3.1.3 数据源配置 3.2 动态数据源设置 3. ...

  6. 搞定SpringBoot多数据源(3):参数化变更源

    目录 1. 引言 2. 参数化变更源说明 2.1 解决思路 2.2 流程说明 3. 实现参数化变更源 3.1 改造动态数据源 3.1.1 动态数据源添加功能 3.1.2 动态数据源配置 3.2 添加数 ...

  7. SpringBoot多数据源:动态数据源

    目录 1. 引言 2. 动态数据源流程说明 3. 实现动态数据源 3.1 说明及数据源配置 3.1.1 包结构说明 3.1.2 数据库连接信息配置 3.1.3 数据源配置 3.2 动态数据源设置 3. ...

  8. Springboot 多数据源配置,结合tk-mybatis

    一.前言 作为一个资深的CRUD工程师,我们在实际使用springboot开发项目的时候,难免会遇到同时使用多个数据库的情况,比如前脚刚查询mysql,后脚就要查询sqlserver. 这时,我们很直 ...

  9. springBoot多数据源(不同类型数据库)项目

    一个基于springboot的多数据源(mysql.sqlserver)项目,先看看项目结构,注意dao层 多数据源mysql配置代码: package com.douzi.robotcenter.c ...

  10. springboot 双数据源+aop动态切换

    # springboot-double-dataspringboot-double-data 应用场景 项目需要同时连接两个不同的数据库A, B,并且它们都为主从架构,一台写库,多台读库. 多数据源 ...

随机推荐

  1. flex定位下overflow失效的问题研究

    概述 这是我在写移动端页面遇到的问题及解决方法,记录下来供以后开发时参考,相信对其他人也有用. 问题 之前写移动端页面,有一个顶条是导航条,需要固定在页面顶部,并且里面的元素需要可以左右滚动. 但是当 ...

  2. postgresql-tps

    tps TPS就是每秒事务数,但是事务是基于虚拟用户数的,假如1个虚拟用户在1秒内完成1笔事务,那么TPS明显就是1:如果 某笔业务响应时间是1ms,那么1个用户在1秒内能完成1000笔事务,TPS就 ...

  3. 【xsy1130】tree 树形dp+期望dp

    题目写得不清不楚的... 题目大意:给你一棵$n$个节点的树,你会随机选择其中一个点作为根,随后随机每个点深度遍历其孩子的顺序. 下面给你一个点集$S$,问你遍历完$S$中所有点的期望时间,点集S中的 ...

  4. (转)pathlib路径库使用详解

    原文:https://xin053.github.io/2016/07/03/pathlib%E8%B7%AF%E5%BE%84%E5%BA%93%E4%BD%BF%E7%94%A8%E8%AF%A6 ...

  5. Python:SQLMap的工作流程

    流程图 代码解析 后面补充…… 版权 作   者:曾是土木人 新浪微博:http://weibo.com/cstmr 转载请注明出处:http://www.cnblogs.com/hongfei/p/ ...

  6. spring jpa : 多条件查询

    https://www.cnblogs.com/Donnnnnn/p/6277872.html 方式一: 第一步:EmpAccNumService package com.payease.scford ...

  7. C# 泛型类在使用中约束

    首先看一下泛型的基本语法 访问修饰符 返回类型 泛型方法名 <T>(T 参数)   1):无法在泛型方法内部给任何 T 类型创建实例的对象,因为在泛型方法内部不知道传进来的对象有哪些构造函 ...

  8. ASP.NET Core 1.0 基础与应用启动

    .NET Core http://dotnet.github.io/[https://github.com/dotnet/coreclr] ASP.NET Core 1.0 https://get.a ...

  9. JavaScript -- TextArea

    -----054-TextArea.html----- <!DOCTYPE html> <html> <head> <meta http-equiv=&quo ...

  10. zepto中的touch库与fastclick

    1. touch库实现了什么和引入背景 click事件在移动端上会有 300ms 的延迟,同时因为需要 长按 , 双触击 等富交互,所以我们通常都会引入类似 zepto 这样的库.zepto 中tou ...