问题:需要在多个数据库中查询数据,不适用sql中的use语句。

导包 pom.xml

<!-- mybatis,使用mybatis-plus也行,虽然plus的单表查询很强,在大部分情况下需要编写复杂繁琐的sql语句,用不上plus -->
<!-- 连接池用mybatis或者spring boot自带的就行。 -->
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>2.2.2</version>
</dependency>
<!-- jdbc,连接的数据库是微软的sql server -->
<dependency>
<groupId>com.microsoft.sqlserver</groupId>
<artifactId>mssql-jdbc</artifactId>
<version>7.4.1.jre8</version>
<scope>runtime</scope>
</dependency>

配置数据源 application.yml

spring:
#数据源
datasource:
#url1
db1:
jdbc-url: jdbc:sqlserver://192.168.2.1:1433;databasename=TonsOfficeA # 高版本的spring使用jdbc-url,不能使用url
username: sa
password: xxx
driver-class-name: com.microsoft.sqlserver.jdbc.SQLServerDriver
#url2
db2:
jdbc-url: jdbc:sqlserver://192.168.100.159:1433;databasename=AgentFlow
username: sa
password: xxx
driver-class-name: com.microsoft.sqlserver.jdbc.SQLServerDriver

配置类 DataSourceConfig1

package com.tons.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.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.core.io.support.PathMatchingResourcePatternResolver; import javax.sql.DataSource; @Configuration
// 扫描com.tons.mapper.db1包下所有@mapper修饰的类,使用db1SqlSessionFactory创建sqlSession(db1SqlSessionFactory使用了db1DataSource作为数据源,并扫描classpath*:mapper/db1/*.xml的mapper.xml文件)
@MapperScan(basePackages = "com.tons.mapper.db1", sqlSessionFactoryRef = "db1SqlSessionFactory")
public class DataSourceConfig1 { // 读取application.yml中的配置参数映射成为 db1DataSource
@Bean("db1DataSource")
@ConfigurationProperties(prefix = "spring.datasource.db1")
public DataSource getDb1DataSource(){
return DataSourceBuilder.create().build();
} // 配置sql会话工厂
@Bean("db1SqlSessionFactory")
public SqlSessionFactory db1SqlSessionFactory(@Qualifier("db1DataSource") DataSource dataSource) throws Exception {
SqlSessionFactoryBean bean = new SqlSessionFactoryBean();
bean.setDataSource(dataSource);
// 这里配置mapper文件配置,简单说就 mapper/db1/下面的所有.xml使用db1DataSource做数据源
bean.setMapperLocations(new PathMatchingResourcePatternResolver().getResources("classpath*:mapper/db1/*.xml"));
return bean.getObject();
} // 配置sql会话模板
@Bean("db1SqlSessionTemplate")
public SqlSessionTemplate db1SqlSessionTemplate(@Qualifier("db1SqlSessionFactory") SqlSessionFactory sqlSessionFactory){
return new SqlSessionTemplate(sqlSessionFactory);
}
}

DataSourceConfig2

package com.tons.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.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.core.io.support.PathMatchingResourcePatternResolver; import javax.sql.DataSource; @Configuration
@MapperScan(basePackages = "com.tons.mapper.db2", sqlSessionFactoryRef = "db2SqlSessionFactory")
public class DataSourceConfig2 { @Bean("db2DataSource")
@ConfigurationProperties(prefix = "spring.datasource.db2")
public DataSource getDb1DataSource(){
return DataSourceBuilder.create().build();
} @Bean("db2SqlSessionFactory")
public SqlSessionFactory db1SqlSessionFactory(@Qualifier("db2DataSource") DataSource dataSource) throws Exception {
SqlSessionFactoryBean bean = new SqlSessionFactoryBean();
bean.setDataSource(dataSource);
bean.setMapperLocations(new PathMatchingResourcePatternResolver().getResources("classpath*:mapper/db2/*.xml"));
return bean.getObject();
} @Bean("db2SqlSessionTemplate")
public SqlSessionTemplate db1SqlSessionTemplate(@Qualifier("db2SqlSessionFactory") SqlSessionFactory sqlSessionFactory){
return new SqlSessionTemplate(sqlSessionFactory);
}
}

使用就和单数据源一样,mapper接口和mapper.xml文件对应使用即可

spring boot 配置多套数据源的更多相关文章

  1. Spring boot配置多个Redis数据源操作实例

    原文:https://www.jianshu.com/p/c79b65b253fa Spring boot配置多个Redis数据源操作实例 在SpringBoot是项目中整合了两个Redis的操作实例 ...

  2. Spring Boot配置多数据源并实现Druid自动切换

    原文:https://blog.csdn.net/acquaintanceship/article/details/75350653 Spring Boot配置多数据源配置yml文件主数据源配置从数据 ...

  3. 13、Spring Boot 2.x 多数据源配置

    1.13 Spring Boot 2.x 多数据源配置 完整源码: Spring-Boot-Demos

  4. Spring Boot 2.x 多数据源配置之 MyBatis 篇

    场景假设:现有电商业务,商品和库存分别放在不同的库 配置数据库连接 app: datasource: first: driver-class-name: com.mysql.cj.jdbc.Drive ...

  5. [转] Spring Boot配置多个DataSource

    [From]  https://www.liaoxuefeng.com/article/001484212576147b1f07dc0ab9147a1a97662a0bd270c20000 Sprin ...

  6. Spring Boot2 系列教程(二十五)Spring Boot 整合 Jpa 多数据源

    本文是 Spring Boot 整合数据持久化方案的最后一篇,主要和大伙来聊聊 Spring Boot 整合 Jpa 多数据源问题.在 Spring Boot 整合JbdcTemplate 多数据源. ...

  7. Redis篇之操作、lettuce客户端、Spring集成以及Spring Boot配置

    Redis篇之操作.lettuce客户端.Spring集成以及Spring Boot配置 目录 一.Redis简介 1.1 数据结构的操作 1.2 重要概念分析 二.Redis客户端 2.1 简介 2 ...

  8. Spring Boot配置,读取配置文件

    Spring Boot配置,读取配置文件 一.配置Spring Boot 1.1 服务器配置 1.2 使用其他Web服务器 1.3 配置启动信息 1.4 配置浏览器显示ico 1.5 Yaml语法 1 ...

  9. Spring Boot -- 配置切换指南

    一般在一个项目中,总是会有好多个环境.比如: 开发环境 -> 测试环境 -> 预发布环境 -> 生产环境 每个环境上的配置文件总是不一样的,甚至开发环境中每个开发者的环境可能也会有一 ...

  10. Spring Boot 配置优先级顺序

    一般在一个项目中,总是会有好多个环境.比如: 开发环境 -> 测试环境 -> 预发布环境 -> 生产环境 每个环境上的配置文件总是不一样的,甚至开发环境中每个开发者的环境可能也会有一 ...

随机推荐

  1. 从Qt到C#,通过COM组件达成跨语言跨平台链接,或者说从托管到非托管的思路

    从Qt到C#,通过COM组件达成跨语言跨平台链接,或者说从非托管到托管 写在前面 c#真的是一种非常蛋疼的语言,和别的语言兼容性差,界面开发效率也不是很高,但是胜在库功能强大,对windows的兼容好 ...

  2. 使用 SmartIDE 开发golang项目

    目录 概述 架构 开发视图 快速开始 安装 SmartIDE CLI 环境 启动 创建环境 安装工具 调试 基本调试 Start 命令调试 很荣幸在去年加入到 SmartIDE 产品组,从事开发工作, ...

  3. context状态树

    provider customer 父组件 创建context对象并导出 export const AddContext = React.createContext<any>({}) 导出 ...

  4. .Net执行SQL/存储过程之易用轻量工具

    支持.Net/.Net Core/.Net Framework,可以部署在Docker, Windows, Linux, Mac. 由于该工具近来被广东省数个公司2B项目采用,且表现稳定,得到良好验证 ...

  5. Mybatis用List接收返回值

    Mybatis 用 List 接收返回值 以 List<Map<String, Object>> 为例 1.XML内 resultType 为单条记录对应类型,设置成 java ...

  6. CVE-2020-1957

    漏洞名称 Apache Shiro 认证绕过漏洞 CVE-2020-1957 利用条件 Apache Shiro < 1.5.1 漏洞原理 Apache Shiro 是一款开源安全框架,提供身份 ...

  7. Kafka初学习

    Kafka初学习   摘要:在之前的消息队列学习中,我已经了解了消息队列的基本概念以及基本用法,同时也了解到了市面上的几款消息队列中间件,其中我了解到了卡夫卡这款消息队列中间件是一款最为快速的消息队列 ...

  8. Java学习记录:2022年1月13日(其二)

    Java学习记录:2022年1月13日(其二) ​ 摘要:本篇笔记主要记录了在设计类时的一些注意事项,类加载时类中各个部分的加载顺序以及继承和多态的知识. 目录 Java学习记录:2022年1月13日 ...

  9. 《机器人SLAM导航核心技术与实战》第1季:第4章_机器人传感器

    <机器人SLAM导航核心技术与实战>第1季:第4章_机器人传感器 视频讲解 [第1季]4.第4章_机器人传感器-视频讲解 [第1季]4.1.第4章_机器人传感器_惯性测量单元-视频讲解 [ ...

  10. 保姆级手把手图文并茂教你配置MAC系统Flutter环境

    Flutter 是什么 Flutter是Google开源的构建用户界面(UI)工具包,帮助开发者通过一套代码库高效构建多平台精美应用,支持移动.Web.桌面和嵌入式平台.Flutter 开源.免费,拥 ...