SpringBoot多数据源yaml配置
2024-09-08 19:40:12
1.配置多数据源
- pom文件
<dependency>
<groupId>com.baomidou</groupId>
<artifactId>dynamic-datasource-spring-boot-starter</artifactId>
<version>2.5.6</version>
</dependency>
- yaml文件(一定对齐)
- 第一种
spring:
datasource:
dynamic:
primary: db1
datasource:
db1:
username: root
password: root
url: jdbc:mysql://127.0.0.1:3306/db1
driver-class-name: com.mysql.jdbc.Driver
type: com.alibaba.druid.pool.DruidDataSource
db2:
username: root2
password: root2
url: jdbc:mysql://127.0.0.1:3306/db2
driver-class-name: com.mysql.jdbc.Driver
type: com.alibaba.druid.pool.DruidDataSource - 第二种
spring:
datasource:
dynamic:
datasource:
master_db1:
username: root
password: root
url: jdbc:mysql://127.0.0.1:3306/db1
driver-class-name: com.mysql.jdbc.Driver
type: com.alibaba.druid.pool.DruidDataSource
db2:
username: root2
password: root2
url: jdbc:mysql://127.0.0.1:3306/db2
driver-class-name: com.mysql.jdbc.Driver
type: com.alibaba.druid.pool.DruidDataSource
druid:
initial-size: 20
minIdle: 50 #最小连接池数量
xxxxxxxxx
- 第一种
2.设置默认数据源
配置中的 primary: db1 , 即是指定默认使用的数据库
spring:
datasource:
dynamic:
primary: db1
3.使用注解切换数据源
- 类上
@DS("db2")
public class xxx(){} - 方法上
@DS("db2")
public void xxx(){} - mapper方法上
@Select("SELECT SFZH,USER_NAME,SEX FROM T_USER_INFO WHERE A = #{A}")
@DS("db2")
Map<String, Object> queryView(@Param("A") String A);
4.其他配置 , 启动
修改Application注解
@SpringBootApplication(exclude = DruidDataSourceAutoConfigure.class)
最新文章
- HTML学习(二)进阶篇
- [BZOJ1501][NOI2005] 智慧珠游戏
- sys,os,模块-正则表达式
- python 生成 xml文件 属性的顺序问题
- Java synchronized 详解
- PHP获取Mp3文件信息
- java web开发时的绝对路径与相对路径
- iOS学习——UI相关小结
- studio中碰到的jni问题:java.lang.UnsatisfiedLinkError
- 刚学的vue.js的单一事件管理组件通信
- 洛谷P1731 生日蛋糕
- 树莓派3 之 安装Mysql服务
- 17-(基础入门篇)GPRS(Air202)串口
- java读取写入文件
- Docker4之Stack
- Hadoop读写流程
- docker toolbox在win7下的安装
- http和WebSocket
- Informatica bulk和normal模式
- Hive命令 参数