springboot 配置多數(shù)據(jù)源 mysql&postgresql

1.引入maven 相關(guān)驅(qū)動(dòng)

        <!-- 多數(shù)據(jù)源 -->
        <dependency>
            <groupId>com.baomidou</groupId>
            <artifactId>dynamic-datasource-spring-boot-starter</artifactId>
            <version>3.5.1</version>
        </dependency>
        <dependency>
            <groupId>com.alibaba</groupId>
            <artifactId>druid-spring-boot-starter</artifactId>
            <version>1.2.8</version>
        </dependency>
        <!-- postgresql依賴(lài) -->
        <dependency>
            <groupId>org.postgresql</groupId>
            <artifactId>postgresql</artifactId>
        </dependency>
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <version>8.0.31</version>
        </dependency>

2.application.properties 文件配置

###MySQL druid多數(shù)據(jù)源配置(純粹多庫(kù))###
#去除durid配置
spring.autoconfigure.exclude=com.alibaba.druid.spring.boot.autoconfigure.DruidDataSourceAutoConfigure
#指定默認(rèn)數(shù)據(jù)源(必須配置)
spring.datasource.dynamic.primary=db01
#設(shè)置嚴(yán)格模式,默認(rèn)false不啟動(dòng). 啟動(dòng)后在未匹配到指定數(shù)據(jù)源時(shí)候回拋出異常,不啟動(dòng)會(huì)使用默認(rèn)數(shù)據(jù)源.
spring.datasource.dynamic.strict=false
#db01庫(kù)
spring.datasource.dynamic.datasource.db01.driver-class-name=com.mysql.cj.jdbc.Driver
spring.datasource.dynamic.datasource.db01.url=jdbc:mysql://wecommysqldev.mysql.database.chinacloudapi.cn:3306/bonussimulation
spring.datasource.dynamic.datasource.db01.username=wecom
spring.datasource.dynamic.datasource.db01.password=QweAsd123#@!
#db02庫(kù)
spring.datasource.dynamic.datasource.wecom.driver-class-name=org.postgresql.Driver
spring.datasource.dynamic.datasource.wecom.url=jdbc:postgresql://wecompostgresdev-p.postgres.database.chinacloudapi.cn:5432/postgres?currentSchema=user&useSSL=false
spring.datasource.dynamic.datasource.wecom.username=wecom_readonly@wecompostgresdev-p
spring.datasource.dynamic.datasource.wecom.password=Wecom#eDc@voc
#數(shù)據(jù)源基礎(chǔ)配置
spring.datasource.dynamic.druid.initial-size=5
spring.datasource.dynamic.druid.min-idle=5
spring.datasource.dynamic.druid.max-active=20
# 配置獲取連接等待超時(shí)的時(shí)間
spring.datasource.dynamic.druid.max-wait=6000
# 配置間隔多久才進(jìn)行一次檢測(cè),檢測(cè)需要關(guān)閉的空閑連接,單位是毫秒
spring.datasource.dynamic.druid.time-between-eviction-runs-millis=60000
# 配置一個(gè)連接在池中最小生存的時(shí)間,單位是毫秒
spring.datasource.dynamic.druid.min-evictable-idle-time-millis=300000
spring.datasource.dynamic.druid.validation-query=select  'Hello Jack'  as  txt
spring.datasource.dynamic.druid.test-while-idle=true
spring.datasource.dynamic.druid.test-on-borrow=false
spring.datasource.dynamic.druid.test-on-return=false
# 打開(kāi)PSCache,并且指定每個(gè)連接上PSCache的大小
spring.datasource.dynamic.druid.pool-prepared-statements=true
spring.datasource.dynamic.druid.max-pool-prepared-statement-per-connection-size=20
# 配置監(jiān)控統(tǒng)計(jì)攔截的filters,去掉后監(jiān)控界面sql無(wú)法統(tǒng)計(jì),'wall'用于防火墻
spring.datasource.dynamic.druid.filters=stat,wall,log4j
# 通過(guò)connectProperties屬性來(lái)打開(kāi)mergeSql功能;慢SQL記錄
spring.datasource.dynamic.druid.connectionProperties=druid.stat.mergeSql=true;druid.stat.slowSqlMillis=5000

3使用多數(shù)據(jù)源

package com.springboot.model.mapper;

import com.baomidou.dynamic.datasource.annotation.DS;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.springboot.model.entity.OrganizeStructure;
import com.springboot.model.entity.vo.OrgVO;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.springframework.stereotype.Repository;

import java.util.List;

/**
 * <p>
 * 組織架構(gòu)表 Mapper 接口
 * </p>
 *
 * @author xingguohao
 * @since 2022-10-31
 */
@Mapper
@Repository
public interface OrganizeStructureMapper extends BaseMapper<OrganizeStructure> {

    @DS("wecom")
    List<OrgVO> getWecomOrgList();

    @DS("wecom")
    OrgVO getParentOrg(@Param("parentId") Long parentId);

    @DS("wecom")
    List<OrganizeStructure> fetchOrganizeStructureList();

}
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀(guān)點(diǎn),簡(jiǎn)書(shū)系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容