Springboot项目多数据源配置详细步骤

一、引入对应的依赖

复制代码
<!--    Oracel    -->
<dependency>
  <groupId>com.oracle.database.jdbc</groupId>
  <artifactId>ojdbc6</artifactId>
  <version>11.2.0.4</version>
</dependency>
<!--for SqlServer-->
<dependency>
  <groupId>com.microsoft.sqlserver</groupId>
  <artifactId>mssql-jdbc</artifactId>
  <version>8.2.0.jre8</version>
</dependency>
<!--其他类型,引入对应的数据源即可-->

二、配置文件配置

复制代码
spring:
  cloud:
    nacos:
      config:
        enabled: true
        server-addr: xx.xxx.xxx.88:8848
        file-extension: yml
      discovery:
        enabled: true
        server-addr: 10.254.253.88:8848
  #动态配置
  datasource:
    dynamic:
      primary: master
      datasource:
        #oracle
        master:
          url: jdbc:oracle:thin:@xx.xxx.xxx.xx:1521:ORCL
          driver-class-name: oracle.jdbc.driver.OracleDriver
          username: xxx
          password: xxxx@xxxxx.xxx.cn
          #SQLServer
        person:
          driver-class-name: com.microsoft.sqlserver.jdbc.SQLServerDriver
          url: jdbc:sqlserver://xx.xxx.xxx.xx:1433;DatabaseName=TransDbCpu
          username: xxxx
          password: xxxx!xxx
          #oracle
        jwPerson:
          url: jdbc:oracle:thin:@xx.xxx.xxx.xx:1521:ORCL
          driver-class-name: oracle.jdbc.driver.OracleDriver
          username: xx
          password: xxxxxxx@xxxxx.xxx.cn

三、代码中使用

举一个oracle的例子,其他类似,在实现类中增加@DS注释,注意一个实现类只可使用一种

复制代码
package com.base.info.service.impl;


import com.baomidou.dynamic.datasource.annotation.DS;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.base.info.dao.DepartDao;
import com.base.info.dao.JWPersonDao;
import com.base.info.dao.PersonDao;
import com.base.info.model.entity.SchoolJWPersonEntity;
import com.base.info.model.entity.SchoolPersonEntity;
import com.base.info.model.qo.PersonInfoQo;
import com.base.info.service.SynJWPersonService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;


/**
 * @author wcg
 * @since 2024/01/31
 */
@Slf4j
@Service
@DS("jwPerson")   //要与配置中的一致
public class SynJWPersonServiceImpl implements SynJWPersonService {

    @Resource
    private JWPersonDao jwPersonDao; //对应的dao层及实体类是对应数据库的表

    @Override
    public List<String> schoolPersonList(PersonInfoQo personInfoQo) {
        return null;
    }

}

四、工作中使用到了,特此做个记录,使用很顺畅,有问题可以留言讨论。

相关推荐
spencer_tseng3 小时前
Stream not available [SysDictDataMapper.xml]
xml·java
蒸蒸yyyyzwd8 小时前
cpp对象模型学习笔记1.1-2.8
java·笔记·学习
qq_297574678 小时前
【实战教程】SpringBoot 集成阿里云短信服务实现验证码发送
spring boot·后端·阿里云
程序员徐师兄8 小时前
Windows JDK11 下载安装教程,适合新手
java·windows·jdk11 下载安装·jdk11 下载教程
RANCE_atttackkk9 小时前
[Java]实现使用邮箱找回密码的功能
java·开发语言·前端·spring boot·intellij-idea·idea
五岳9 小时前
DTS按业务场景批量迁移阿里云MySQL表实战(下):迁移管理平台设计与实现
java·应用·dts
韩立学长10 小时前
【开题答辩实录分享】以《智能大学宿舍管理系统的设计与实现》为例进行选题答辩实录分享
数据库·spring boot·后端
zhougl99610 小时前
Java 所有关键字及规范分类
java·开发语言
Python 老手10 小时前
Python while 循环 极简核心讲解
java·python·算法
java1234_小锋10 小时前
Java高频面试题:MyISAM索引与InnoDB索引的区别?
java·开发语言