spring boot 配置多套数据源
问题:需要在多个数据库中查询数据,不适用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 配置多套数据源的更多相关文章
- Spring boot配置多个Redis数据源操作实例
原文:https://www.jianshu.com/p/c79b65b253fa Spring boot配置多个Redis数据源操作实例 在SpringBoot是项目中整合了两个Redis的操作实例 ...
- Spring Boot配置多数据源并实现Druid自动切换
原文:https://blog.csdn.net/acquaintanceship/article/details/75350653 Spring Boot配置多数据源配置yml文件主数据源配置从数据 ...
- 13、Spring Boot 2.x 多数据源配置
1.13 Spring Boot 2.x 多数据源配置 完整源码: Spring-Boot-Demos
- Spring Boot 2.x 多数据源配置之 MyBatis 篇
场景假设:现有电商业务,商品和库存分别放在不同的库 配置数据库连接 app: datasource: first: driver-class-name: com.mysql.cj.jdbc.Drive ...
- [转] Spring Boot配置多个DataSource
[From] https://www.liaoxuefeng.com/article/001484212576147b1f07dc0ab9147a1a97662a0bd270c20000 Sprin ...
- Spring Boot2 系列教程(二十五)Spring Boot 整合 Jpa 多数据源
本文是 Spring Boot 整合数据持久化方案的最后一篇,主要和大伙来聊聊 Spring Boot 整合 Jpa 多数据源问题.在 Spring Boot 整合JbdcTemplate 多数据源. ...
- Redis篇之操作、lettuce客户端、Spring集成以及Spring Boot配置
Redis篇之操作.lettuce客户端.Spring集成以及Spring Boot配置 目录 一.Redis简介 1.1 数据结构的操作 1.2 重要概念分析 二.Redis客户端 2.1 简介 2 ...
- Spring Boot配置,读取配置文件
Spring Boot配置,读取配置文件 一.配置Spring Boot 1.1 服务器配置 1.2 使用其他Web服务器 1.3 配置启动信息 1.4 配置浏览器显示ico 1.5 Yaml语法 1 ...
- Spring Boot -- 配置切换指南
一般在一个项目中,总是会有好多个环境.比如: 开发环境 -> 测试环境 -> 预发布环境 -> 生产环境 每个环境上的配置文件总是不一样的,甚至开发环境中每个开发者的环境可能也会有一 ...
- Spring Boot 配置优先级顺序
一般在一个项目中,总是会有好多个环境.比如: 开发环境 -> 测试环境 -> 预发布环境 -> 生产环境 每个环境上的配置文件总是不一样的,甚至开发环境中每个开发者的环境可能也会有一 ...
随机推荐
- Surp Suite入门
BurpSuite代理工具是以拦截代理的方式,拦截所有通过代理的网络流量,如客户端的请求数据.服务器端的返回信息等.Burp Suite主要拦截HTTP和HTTPS 协议的流量,通过拦截,Burp S ...
- easygui 之integerbox()、enterbox()、multenterbox() 三种输入函数的使用
1.integerbox()函数:只可输入整数的输入框,默认输入范围为0-99 integerbox(msg="", title=" ", default=No ...
- 11、ON DUPLICATE KEY UPDATE实现插入更新操作
一.插入与更新操作: MySQL中,采用ON DUPLICATE KEY UPDATE语句对不存在的数据进行INSERT插入操作,对已存在的数据进行UPDATE更新操作: 总结: 1.ON DUPLI ...
- 用python爬取网络文章----滴天髓
用python爬取网络文章真的很简单.主要分以下几个步骤 1.安装并导入相关模块. 这里我们要用到两个模块,分别是reqesets和lxml 安装命令pip install requests和pip ...
- session取不到值
今天鼓捣项目时出现了一个问题 项目重启后,设置session值后,第一次请求经过过滤器时 session取不到值,导致被拦截 经过半天的研究,终于...我请教了别人 把代码给了朋友,在朋友一段时间的琢 ...
- 《Effective C++》定制new和delete
Item49:了解new_handler的行为 当operator new抛出异常以反映出一个未获得满足的内存需求之前,它会先调用一个用户制定的错误处理函数,一个所谓的new-handler,为了制定 ...
- iOS如何实现自动化打包
iOS如何实现自动化打包 前言 在我们的日常开发工作中,避免不了会出现这样的场景:需求迭代开发完成之后,需要提供ipa包给QA同学进行测试,一般会执行如下流程:1.执行Git Pull命令,拉最新的代 ...
- 《爆肝整理》保姆级系列教程-玩转Charles抓包神器教程(5)-Charles如何设置捕获Https会话
1.简介 在大数据时代,互联网时代,个人信息安全尤为重要,网络安全在近日多起电信诈骗事情发酵下的情况下,引起国家,企业,个人对于互联网安全进一步的重视.而之前很多以http协议传输的网站出现的网站信息 ...
- Linux环境下:程序的链接, 装载和库[ELF文件详解]
编译过程拆解 预处理处理生成.i文件, .i文件还是源码文件 将所有的宏定义#define展开. 处理#if, #else, #endif等条件编译指令 处理#include, 原地插入文件 cpp ...
- 重学SpringBoot. step1 全注解的SpringBoot
参考:<深入浅出SpringBoot 2.x> 全注解的SpringBoot 用户可以通过注解将所需要的对象,存放到IOC容器中,然后SpringBoot可以根据这些需要使用的情况,自动注 ...