springboot mybatis 多数据源整合
1、在application.properties中配置两个数据库:
# db01 database
spring.datasource.db01.jdbc-url=jdbc:oracle:thin:@127.0.0.1:1521/orcl
spring.datasource.db01.username=lxw
spring.datasource.db01.password=lxw@123
spring.datasource.db01.driverClassName=oracle.jdbc.driver.OracleDriver
# db02 database
spring.datasource.db02.jdbc-url=jdbc:oracle:thin:@127.0.0.1:1521/orcl
spring.datasource.db02.username=lxw02
spring.datasource.db02.password=lxw02@123
spring.datasource.db02.driverClassName=oracle.jdbc.driver.OracleDriver
2、建立连个数据源的配置文件:
第一个配置文件:
package com.lxw.lxwDemo.datasource;
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.SpringBootConfiguration;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.boot.jdbc.DataSourceBuilder;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Primary;
import org.springframework.core.io.support.PathMatchingResourcePatternResolver;
import javax.sql.DataSource;
//表示这个类为一个配置类
@SpringBootConfiguration
//配置mybatis的接口类放的地方
@MapperScan(basePackages = "com.lxw.lxwDemo.mapper.db01", sqlSessionFactoryRef = "db01SqlSessionFactory")
public class db01DataSourceConfig {
//将这个对象放入Spring容器中
@Bean(name = "db01DataSource")
//表示这个数据源是默认数据源
@Primary
//读取application.properties中的配置参数映射成为一个对象
//prefix表示参数的前缀
@ConfigurationProperties(prefix = "spring.datasource.db01")
public DataSource getDateSource1() {
return DataSourceBuilder.create().build();
}
@Bean(name = "db01SqlSessionFactory")
//表示这个数据源是默认数据源
@Primary
//@Qualifier表示查找Spring容器中名字为test1DataSource的对象
public SqlSessionFactory db01SqlSessionFactory(@Qualifier("db01DataSource") DataSource datasource)
throws Exception {
SqlSessionFactoryBean bean = new SqlSessionFactoryBean();
bean.setDataSource(datasource);
bean.setMapperLocations(
//设置mybatis的xml所在位置
new PathMatchingResourcePatternResolver().getResources("classpath*:mapper/db01/*Mapper.xml"));
return bean.getObject();
}
@Bean("db01SqlSessionTemplate")
// 表示这个数据源是默认数据源
@Primary
public SqlSessionTemplate db01sqlsessiontemplate(
@Qualifier("db01SqlSessionFactory") SqlSessionFactory sessionfactory) {
return new SqlSessionTemplate(sessionfactory);
}
}
第二个配置文件:
package com.lxw.lxwDemo.datasource;
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.SpringBootConfiguration;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.boot.jdbc.DataSourceBuilder;
import org.springframework.context.annotation.Bean;
import org.springframework.core.io.support.PathMatchingResourcePatternResolver;
import javax.sql.DataSource;
//表示这个类为一个配置类
@SpringBootConfiguration
//配置mybatis的接口类放的地方
@MapperScan(basePackages = "com.lxw.lxwDemo.mapper.db02", sqlSessionFactoryRef = "db02SqlSessionFactory")
public class db02DataSourceConfig {
@Bean(name = "db02DataSource")
@ConfigurationProperties(prefix = "spring.datasource.db02")
public DataSource getDateSource2() {
return DataSourceBuilder.create().build();
}
@Bean(name = "db02SqlSessionFactory")
public SqlSessionFactory db02SqlSessionFactory(@Qualifier("db02DataSource") DataSource datasource)
throws Exception {
SqlSessionFactoryBean bean = new SqlSessionFactoryBean();
bean.setDataSource(datasource);
bean.setMapperLocations(
new PathMatchingResourcePatternResolver().getResources("classpath*:mapper/db02/*Mapper.xml"));
return bean.getObject();
}
@Bean("db02SqlSessionTemplate")
public SqlSessionTemplate db02sqlsessiontemplate(
@Qualifier("db02SqlSessionFactory") SqlSessionFactory sessionfactory) {
return new SqlSessionTemplate(sessionfactory);
}
}
项目结构

springboot mybatis 多数据源整合的更多相关文章
- spring-boot (四) springboot+mybatis多数据源最简解决方案
学习文章来自:http://www.ityouknow.com/spring-boot.html 配置文件 pom包就不贴了比较简单该依赖的就依赖,主要是数据库这边的配置: mybatis.confi ...
- springboot + mybatis + 多数据源
此文已由作者赵计刚薪授权网易云社区发布. 欢迎访问网易云社区,了解更多网易技术产品运营经验 在实际开发中,我们一个项目可能会用到多个数据库,通常一个数据库对应一个数据源. 代码结构: 简要原理: 1) ...
- 第九章 springboot + mybatis + 多数据源 (AOP实现)
在第八章 springboot + mybatis + 多数据源代码的基础上,做两点修改 1.ShopDao package com.xxx.firstboot.dao; import org.spr ...
- 【第九章】 springboot + mybatis + 多数据源 (AOP实现)
在第八章 springboot + mybatis + 多数据源代码的基础上,做两点修改 1.ShopDao package com.xxx.firstboot.dao; import org.spr ...
- springboot+mybatis多数据源的事务问题
1.springboot+mybatis实现多数据源后,针对单个数据源我们可以使用@Transactional(name="xxxTransactionManager") 来指定使 ...
- springboot mybatis 多数据源配置
首先导入mybatis等包,这里就不多说. 下面是配置多数据源和mybatis,每个数据源对应一套mybatis模板 数据源1: package com.aaaaaaa.config.datasour ...
- 第八章 springboot + mybatis + 多数据源(转载)
本篇博客转发自:http://www.cnblogs.com/java-zhao/p/5413845.html 在实际开发中,我们一个项目可能会用到多个数据库,通常一个数据库对应一个数据源. 代码结构 ...
- 【第八章】 springboot + mybatis + 多数据源
在实际开发中,我们一个项目可能会用到多个数据库,通常一个数据库对应一个数据源. 代码结构: 简要原理: 1)DatabaseType列出所有的数据源的key---key 2)DatabaseConte ...
- 第八章 springboot + mybatis + 多数据源2(解决循环引用)
解决了循环引用 1.application.properties #the first datasource jdbc.names:1,2 jdbc1.driverClassName = com.my ...
- spring boot(七):springboot+mybatis多数据源最简解决方案
说起多数据源,一般都来解决那些问题呢,主从模式或者业务比较复杂需要连接不同的分库来支持业务.我们项目是后者的模式,网上找了很多,大都是根据jpa来做多数据源解决方案,要不就是老的spring多数据源解 ...
随机推荐
- OneCloud记录
配置信息 S805, 1G RAM, 8G ROM, USB2.0 * 2, 1GB LAN, SD Cardreader S805参数: 32-bit, ARMv7-A, Cortex-A5, 1. ...
- 基于tensorflow的RBF神经网络案例
1 前言 在使用RBF神经网络实现函数逼近中,笔者介绍了使用 Matlab 训练RBF神经网络.本博客将介绍使用 tensorflow 训练RBF神经网络.代码资源见:RBF案例(更新版) 这几天,笔 ...
- 如何基于three.js(webgl)引擎架构,实现3D密集架库房,3D档案室(3d机器人取档、机器人盘点、人工查档、设备巡检)
前言: 这是最好的时代,也是最坏的时代:是充满挑战的时代,也是充满机遇的时代.是科技飞速的时代,也是无限可能的时代. 近年来,人工智能(AI)技术的飞速发展已经席卷了全球,不断突破着技术边界,为各行 ...
- pta-神坛
[神坛]pta *相邻两条边围成的三角形面积会是最小的 极角排序+叉积计算三角形面积 #include<bits/stdc++.h> #define int long long using ...
- [BUUCTF][Web][极客大挑战 2019]Havefun 1
打开靶机的URL,看到一个页面 右键查看源代码,看到有用信息 <html> ... <!-- $cat=$_GET['cat']; echo $cat; if($cat=='dog' ...
- 如何避免Git合并远程分支时出现可读性差的日志
问题及现象 当某一分支(假设为main)的本地仓库和远程仓库都基于同一个提交进行了修改,并分别创建了新的提交时,在本地执行git push origin main会提示先要执行git pull合并远程 ...
- Jenkins下载插件报错
只要看日志报了什么错 下载超时 更新代理源https://mirrors.tuna.tsinghua.edu.cn/jenkins/updates/update-center.json 报unable ...
- React 中 Ref 引用
不要因为别人的评价而改变自己的想法,因为你的生活是你自己的. 1. React 中 Ref 的应用 1.1 给标签设置 ref 给标签设置 ref,ref="username", ...
- 无所不谈,百无禁忌,Win11本地部署无内容审查中文大语言模型CausalLM-14B
目前流行的开源大语言模型大抵都会有内容审查机制,这并非是新鲜事,因为之前chat-gpt就曾经被"玩"坏过,如果没有内容审查,恶意用户可能通过精心设计的输入(prompt)来操纵L ...
- web模块化
CommonJS-----是一种后端js规范,是nodejs遵循的一种编写js模块的规范引入模块-------require('模块路径')定义模块 ------ exports.模块名= funct ...