美文网首页
springboot 配置多数据源 mysql&postgres

springboot 配置多数据源 mysql&postgres

作者: angelali_90b4 | 来源:发表于2022-11-29 10:14 被阅读0次

    1.引入maven 相关驱动

            <!-- 多数据源 -->
            <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依赖 -->
            <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多数据源配置(纯粹多库)###
    #去除durid配置
    spring.autoconfigure.exclude=com.alibaba.druid.spring.boot.autoconfigure.DruidDataSourceAutoConfigure
    #指定默认数据源(必须配置)
    spring.datasource.dynamic.primary=db01
    #设置严格模式,默认false不启动. 启动后在未匹配到指定数据源时候回抛出异常,不启动会使用默认数据源.
    spring.datasource.dynamic.strict=false
    #db01库
    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库
    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
    #数据源基础配置
    spring.datasource.dynamic.druid.initial-size=5
    spring.datasource.dynamic.druid.min-idle=5
    spring.datasource.dynamic.druid.max-active=20
    # 配置获取连接等待超时的时间
    spring.datasource.dynamic.druid.max-wait=6000
    # 配置间隔多久才进行一次检测,检测需要关闭的空闲连接,单位是毫秒
    spring.datasource.dynamic.druid.time-between-eviction-runs-millis=60000
    # 配置一个连接在池中最小生存的时间,单位是毫秒
    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
    # 打开PSCache,并且指定每个连接上PSCache的大小
    spring.datasource.dynamic.druid.pool-prepared-statements=true
    spring.datasource.dynamic.druid.max-pool-prepared-statement-per-connection-size=20
    # 配置监控统计拦截的filters,去掉后监控界面sql无法统计,'wall'用于防火墙
    spring.datasource.dynamic.druid.filters=stat,wall,log4j
    # 通过connectProperties属性来打开mergeSql功能;慢SQL记录
    spring.datasource.dynamic.druid.connectionProperties=druid.stat.mergeSql=true;druid.stat.slowSqlMillis=5000
    

    3使用多数据源

    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>
     * 组织架构表 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();
    
    }
    

    相关文章

      网友评论

          本文标题:springboot 配置多数据源 mysql&postgres

          本文链接:https://www.haomeiwen.com/subject/focdfdtx.html