看完这篇,搞定SpringBoot多数据源
添加多数据源配置
spring.datasource.platform.jdbc-url=jdbc:mysql://localhost:3306/platform
spring.datasource.platform.username=root
spring.datasource.platform.password=root
spring.datasource.platform.driver-class-name=com.mysql.cj.jdbc.Driverspring.datasource.mall.jdbc-url=jdbc:mysql://localhost:3306/mall
spring.datasource.mall.username=root
spring.datasource.mall.password=root
spring.datasource.mall.driver-class-name=com.mysql.cj.jdbc.Drivermybatis.mapper-locations=classpath:mapper/**.xml
初始化数据源与mybatis配置
- 新建一个多数据源的配置类
@Configuration
public class DataSourceConfig {@Primary@Bean@ConfigurationProperties(prefix = "spring.datasource.platform")public DataSource platformDataSource() {return DataSourceBuilder.create().build();}@Bean@ConfigurationProperties(prefix = "spring.datasource.mall")public DataSource mallDataSource() {return DataSourceBuilder.create().build();}
}
- 分别创建两个数据源的
mybatis
配置
platform
数据源JPA
配置
@Configuration
@MapperScan(basePackages = "com.example.refreshcommodity.mapper.platform",sqlSessionFactoryRef = "sqlSessionFactoryPlatform",sqlSessionTemplateRef = "sqlSessionTemplatePlatform")
public class PlatformConfig {private DataSource platformDataSource;public PlatformConfig(@Qualifier("platformDataSource") DataSource platformDataSource) {this.platformDataSource = platformDataSource;}@Beanpublic SqlSessionFactory sqlSessionFactoryPlatform() throws Exception {SqlSessionFactoryBean bean = new SqlSessionFactoryBean();bean.setDataSource(platformDataSource);bean.setMapperLocations(new PathMatchingResourcePatternResolver().getResources("classpath*:mapper/platform/*.xml"));return bean.getObject();}@Beanpublic SqlSessionTemplate sqlSessionTemplatePlatform() throws Exception {return new SqlSessionTemplate(sqlSessionFactoryPlatform());}
}
mall
数据源JPA
配置
@Configuration
@MapperScan(basePackages = "com.example.refreshcommodity.mapper.mall",sqlSessionFactoryRef = "sqlSessionFactoryMall",sqlSessionTemplateRef = "sqlSessionTemplateMall")
public class MallConfig {private DataSource mallDataSource;public MallConfig(@Qualifier("mallDataSource") DataSource mallDataSource) {this.mallDataSource = mallDataSource;}@Beanpublic SqlSessionFactory sqlSessionFactoryMall() throws Exception {SqlSessionFactoryBean bean = new SqlSessionFactoryBean();bean.setDataSource(mallDataSource);bean.setMapperLocations(new PathMatchingResourcePatternResolver().getResources("classpath*:mapper/mall/*.xml"));return bean.getObject();}@Beanpublic SqlSessionTemplate sqlSessionTemplateMall() throws Exception {return new SqlSessionTemplate(sqlSessionFactoryMall());}
}
- 创建
mapper
接口
- 在
com.example.refreshcommodity.mapper.mall
目录下创建mall
数据源的接口 - 在
com.example.refreshcommodity.mapper.platform
目录下创建platform
数据源的接口
最后的目录结构如下:
测试
编写测试类验证
MallMapper.java
代码
public interface MallMapper {List<Map<String,Object>> getSchoolList();}
MallMapper.xml
代码
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd" >
<mapper namespace="com.example.refreshcommodity.mapper.mall.MallMapper"><select id="getSchoolList" resultType="java.util.Map" parameterType="java.util.List">select * from school</select></mapper>
PlatformMapper.java
代码
public interface PlatformMapper {List<Map<String,Object>> getUserList();}
PlatformMapper.xml
代码
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd" >
<mapper namespace="com.example.refreshcommodity.mapper.platform.PlatformMapper"><select id="getUserList" resultType="java.util.Map" parameterType="java.util.List">select * from user</select></mapper>
MyController.java
代码
@RestController
public class MyController {@AutowiredPlatformMapper platformMapper;@AutowiredMallMapper mallMapper;@GetMapping("/test")public String test() {List<Map<String, Object>> schoolList = mallMapper.getSchoolList();List<Map<String, Object>> userList = platformMapper.getUserList();Map<String, Object> result = new HashMap<>();result.put("schoolList", schoolList);result.put("userList", userList);return result.toString();}}
测试结果
{"schoolList": [{"address": "北京市三里屯","name": "清华附属小学","id": 1}],"userList": [{"name": "小明","id": 1,"age": 10}]
}
可以看到实现了多数据源的查询
看完这篇,搞定SpringBoot多数据源相关推荐
- 一篇搞定 SpringBoot+Mybatis+Shiro 实现多角色权限管理
初衷:我在网上想找整合springboot+mybatis+shiro并且多角色认证的博客,发现找了好久也没有找到想到的,现在自己会了,就打算写个博客分享出去,希望能帮到你. 原创不易,请点赞支持! ...
- 分布式理论,看完这篇你定能有所获
分布式架构系统回顾 分布式系统概念 分布式系统是一个硬件或软件组件分布在不同的网络计算机上,彼此之间仅仅通过消息传递进行通信和协调的系统. 简单点说,所谓分布式系统,就是一个业务拆分成多个子业务,分布 ...
- linux shell find depth,搞定 Linux Shell 文本处理工具,看完这篇集锦就够了
原标题:搞定 Linux Shell 文本处理工具,看完这篇集锦就够了 Linux Shell是一种基本功,由于怪异的语法加之较差的可读性,通常被Python等脚本代替.既然是基本功,那就需要掌握,毕 ...
- SpringBoot的Web开发支持【超详细【一篇搞定】果断收藏系列】
SpringBoot的Web开发支持 常用的服务器配置 使用Jetty服务器替换Tomcat 排除Tomcat的启动器,引入Jetty application.yml 编写入口程序 编写Control ...
- 我就不信看完这篇你还搞不懂信息熵
我就不信看完这篇你还搞不懂信息熵 https://mp.weixin.qq.com/s/7NrB0UtmELXD3UNO3C6jGA 让我们说人话!好的数学概念都应该是通俗易懂的. 信息熵,信息熵,怎 ...
- 神了!!看完这篇文章我不仅学会了手撸vue三开关组件,还搞懂了父子组件传值
神了!!看完这篇文章我不仅学会了手撸vue三开关组件,还搞懂了父子组件传值 引子 前置知识 什么是vue组件 父子组件传值 父传子 子传父 model选项的引入 三开关组件(three-switch) ...
- 云小课|想实现资源全自动备份?看完这篇秘籍,不再蕉绿~
阅识风云是华为云信息大咖,擅长将复杂信息多元化呈现,其出品的一张图(云图说).深入浅出的博文(云小课)或短视频(云视厅)总有一款能让您快速上手华为云.更多精彩内容请单击此处. 摘要:在给备份系统设置& ...
- 运维学python用不上_作为运维你还在想要不要学Python,看完这篇文章再说!
原标题:作为运维你还在想要不要学Python,看完这篇文章再说! 本文由马哥教育Python自动化实战班5期学员推荐,转载自简书,作者为Li.Yingjie,内容略经小编改编和加工,观点跟作者无关,最 ...
- vlan配置实例详解_网工知识角|MUXVLAN技术详解,基本原理一篇搞定
学网络,就在IE-LAB 国内高端网络工程师培养基地 MUX VLAN(Multiplex VLAN )提供了一种通过VLAN进行网络资源控制的机制.通过MUX VLAN提供的二层流量隔离的机制可以实 ...
最新文章
- TMG学习(十),发布DMZ区网站
- 丁磊旗下首个IPO的AI业务!网易有道年入7个亿,清华96计算机系为主力,沈向洋任董事...
- sqlite3-查看数据库
- Spread for Windows Forms快速入门(1)---开始使用Spread
- 文件解析漏洞总结-Apache
- Spring dataSource配置
- python计算区间内偶数和_Python 计算当真因子个数为偶数个时为幸运数,计算区间内幸运数之和...
- linux指令:输出重定向与追加- 输出重定向 - 表示追加
- she's gone
- 714. 买卖股票的zui佳时机含手续费(JavaScript)
- 计算机视觉(CV)中图像的梯度
- 力扣题目——997. 找到小镇的法官
- 【智力题】拿硬币(数数字)、游戏
- windows如何设定定时关机和取消定时
- mysql 题库_题库Mysql表设计案例
- 微信内置浏览器禁止调整字体大小的方法
- 分叉币的发展史及价值|ETH、BCH、BSV 2020-03-08
- 推荐一款专家级网站流量分析软件-逆火(Nihuo)
- Java小白入门200例80之Java继承(extends)
- 科罗拉多大学计算机科学如何,科罗拉多大学波德分校计算机科学世界排名2017年最新排名第88(THE世界排名)...