当前位置: 首页 > news >正文

SpringBoot + Mybatis多数据源

一、配置文件

spring:
#  datasource:
#    username: root
#    password: 123456
#    url: jdbc:mysql://127.0.0.1:3306/jun01?characterEncoding=utf-8&serverTimezone=UTC
#    driver-class-name: com.mysql.cj.jdbc.Driverdatasource:# 数据源1onedata:jdbc-url: jdbc:mysql://127.0.0.1:3306/jun01?useUnicode=true&characterEncoding=utf-8username: rootpassword: 123456driver-class-name: com.mysql.cj.jdbc.Driver# 数据源2twodata:jdbc-url: jdbc:mysql://127.0.0.1:3306/jun02?useUnicode=true&characterEncoding=utf-8username: rootpassword: 123456driver-class-name: com.mysql.cj.jdbc.Driver

二、数据源配置类

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;@Configuration
public class DataSourceConfiguration {// Primary 注解是在没有指明使用哪个数据源的时候指定默认使用的主数据源@Primary@Bean("oneDataSource")@ConfigurationProperties(prefix = "spring.datasource.onedata")public DataSource primaryDataSource() {return DataSourceBuilder.create().build();}@Bean("twoDataSource")@ConfigurationProperties(prefix = "spring.datasource.twodata")public DataSource secondaryDataSource() {return DataSourceBuilder.create().build();}
}

三、数据源与 Mybatis 配置

1、数据源一

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.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Primary;import javax.annotation.Resource;
import javax.sql.DataSource;@Configuration
// 指定该数据源扫描指定包下面的Mapper接口 与 *.xml文件
@MapperScan(basePackages = "com.south.mapper1",sqlSessionFactoryRef = "sqlSessionFactoryOne",sqlSessionTemplateRef = "sqlSessionTemplateOne")
public class DataSourceOneConfig {// 注入数据源1@Resourceprivate DataSource oneDataSource;@Bean@Primarypublic SqlSessionFactory sqlSessionFactoryOne() throws Exception {SqlSessionFactoryBean sqlSessionFactoryBean = new SqlSessionFactoryBean();sqlSessionFactoryBean.setDataSource(oneDataSource);return sqlSessionFactoryBean.getObject();}@Bean@Primarypublic SqlSessionTemplate sqlSessionTemplateOne() throws Exception {return new SqlSessionTemplate(sqlSessionFactoryOne());}
}

2、数据源二(两种不同的配置方式,都能实现多数据源的效果)

import org.apache.ibatis.session.SqlSessionFactory;
import org.mybatis.spring.SqlSessionFactoryBean;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.io.Resource;
import org.springframework.core.io.support.PathMatchingResourcePatternResolver;
import org.springframework.jdbc.datasource.DataSourceTransactionManager;import javax.sql.DataSource;@Configuration
@MapperScan(basePackages = "com.example.mapper2",sqlSessionFactoryRef = "sqlSessionFactoryTwo")
public class DataSourceTwoConfig {// mapper 扫描 xml 文件的路径private static final String MAPPER_LOCATION = "classpath:mapper2/*.xml";private DataSource twoDataSource;// 通过构造方法进行注入public DataSourceTwoConfig(DataSource dataSource) {this.twoDataSource = dataSource;}@Beanpublic SqlSessionFactory sqlSessionFactoryTwo() throws Exception {SqlSessionFactoryBean sqlSessionFactoryBean = new SqlSessionFactoryBean();// 指定数据源sqlSessionFactoryBean.setDataSource(twoDataSource);/** 获取xml文件资源对象* 当Mapper接口所对应的.xml文件与Mapper接口文件分离,存储在 resources* 文件夹下的时候,需要手动指定.xml文件所在的路径*/Resource[] resources = new PathMatchingResourcePatternResolver().getResources(MAPPER_LOCATION);sqlSessionFactoryBean.setMapperLocations(resources);return sqlSessionFactoryBean.getObject();}@Beanpublic DataSourceTransactionManager SecondaryDataSourceManager() {return new DataSourceTransactionManager(twoDataSource);}
}

四、使用案例

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;import javax.annotation.Resource;
import java.util.List;@Controller
@RequestMapping("/testController")
public class TestController {// 数据源1Mapper注入@Resourceprivate OneMapper oneMapper;// 数据源2Mapper注入@Resourceprivate TwoMapper twoMapper;@GetMapping("/selectManyDataSouroneMapperceData")@ResponseBodypublic void selectManyDataSourceDatwoMapperta() {// 查询数据源1中的数据List<String> message = oneMapper.getMessage();System.out.println(message);// 查询数据源2中的数据List<String> tag = twoMapper.getTag();System.out.println(tag);}
}

 

http://www.lryc.cn/news/125658.html

相关文章:

  • ad+硬件每日学习十个知识点(35)23.8.15 (接口电路:RS232、RS485、RS422,单线协议UART->TTL)
  • sql类型-用户定义表类型
  • 小程序 vant 项目记录总结 使用 scss 分享 订阅消息 wxs 分包 echarts图表 canvas getCurrentPages页面栈
  • 关于Power Query中一些忽略的细节
  • QML与C++交互
  • Microsoft ISA服务器配置及日志分析
  • Openlayers 实战 - 地图视野(View)- 图层 -(layer)- 资源(source)显示等级设置。
  • Linux:shell脚本 正则表达式与AWK
  • Android UI自动化测试框架—SoloPi简介
  • Android Studio Giraffe 正式版下载地址
  • 【C语言】调试技巧
  • MySQL SUBSTRING_INDEX() 函数的详细介绍
  • 开源数据库Mysql_DBA运维实战 (DML/DQL语句)
  • 【LangChain】Memory
  • Java并发编程(六)线程池[Executor体系]
  • macOS CLion 使用 bits/stdc++.h
  • PS出现的问题——为什么PS另存的格式少了很多
  • 【Linux】进程通信篇Ⅱ:共享内存、消息队列、信号量
  • 8.14 校招 内推 面经
  • 阿里云服务器安装部署Docker使用教程
  • WebRTC | ICE详解
  • 网络设备(防火墙、路由器、交换机)日志分析监控
  • 2023年国赛数学建模思路 - 复盘:人力资源安排的最优化模型
  • Compute shader SV 理解图
  • 生信豆芽菜-多种算法计算免疫浸润
  • 逆向破解学习-单机斗地主
  • matplotlib绘制位置-时序甘特图
  • 数据库概述、部署MySQL服务、必备命令、密码管理、安装图形软件、SELECT语法 、筛选条件
  • 概率论与数理统计:第四章:随机变量的数字特征
  • 解决饿了么ui的对话框缩放和移动