鳄鱼笔记(二) --> SpringBoot<--[SpringBoot 實現多yml切換]

盖玉石
2023-12-01

一、實現多yml 檔

1. 添加主啟動配置類

添加application.yml

spring:
  profiles:
    active: demo

2. 配置類 application-XXX.yml

添加application-demo.yml

server:
  port: 8103
  tomcat:
    uri-encoding: utf-8

3. 配置類 application-XXX.yml

添加application-demo2.yml

server:
  port: 8104
  tomcat:
    uri-encoding: utf-8

添加以上三個yml 檔 在 resource目錄下,最後會以端口8103 啟動,啟動后控制台窗口會打印如下代碼

The following profiles are active: demo

實現多數據源

1、依賴

		<dependency>
            <groupId>com.baomidou</groupId>
            <artifactId>mybatis-plus-boot-starter</artifactId>
            <version>3.2.0</version>
        </dependency>
        <dependency>
            <groupId>com.baomidou</groupId>
            <artifactId>dynamic-datasource-spring-boot-starter</artifactId>
            <version>2.5.6</version>
        </dependency>

2、yml檔案

spring:
  autoconfigure:
    exclude: com.alibaba.druid.spring.boot.autoconfigure.DruidDataSourceAutoConfigure
  datasource:
    dynamic:
      druid:
        initial-size: 1
        max-active: 5

        min-idle: 1
        max-wait: 60000
      primary: db1 #主數據庫
      datasource:
        db1:
          driver-class-name: oracle.jdbc.driver.OracleDriver
          url: jdbc:oracle:thin:@xx.xx.xx.xx:1521:xxxx
          username: xxx
          password: xxx
        db2:
          driver-class-name: oracle.jdbc.driver.OracleDriver
          url: jdbc:oracle:thin:@xx.xx.xx.xx:1521:xxxx
          username: xxx
          password: xxx
mybatis:
  mapper-locations: classpath:mapper/*.xml
  type-aliases-package: com.dpm.maintain.dpm_maintain_api.model
  configuration:
      log-impl: org.apache.ibatis.logging.stdout.StdOutImpl
      #log-impl: org.apache.ibatis.logging.slf4j.Slf4jImpl
      call-setters-on-nulls: true # 返回MAP集合的時候 如有有NULL值 不會剔除改鍵值對

3、使用

package com.wistron.mes.dao;

import com.baomidou.dynamic.datasource.annotation.DS;
import com.tp.strive.model.Record;
import org.apache.ibatis.annotations.Mapper;

import java.util.List;
import java.util.Map;
@Mapper
@DS("db1")
public interface XxxxDao {
    int xxx(Record record);

END、pom檔

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.4.4</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>
    <groupId>com.mes.moveOut</groupId>
    <artifactId>moveOut</artifactId>
    <version>2021-04-06</version>
    <packaging>war</packaging>
    <name>mesapi</name>
    <description>moveOut for mes</description>

    <properties>
        <java.version>1.8</java.version>
        <timestamp>${maven.build.timestamp}</timestamp>
        <maven.build.timestamp.format>yyyy-MM-dd HH:mm:ss</maven.build.timestamp.format>
    </properties>

    <dependencies>
        <!--springboot 父類<必要>-->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
            <!--去除 springboot 父類 中的tomcat集成 改為本地運行 provide-->
            <exclusions>
                <exclusion>
                    <groupId>org.springframework.boot</groupId>
                    <artifactId>spring-boot-starter-tomcat</artifactId>
                </exclusion>
            </exclusions>
        </dependency>
        <!--tomcat 插件改為運行時執行, provide 代表打包剔除-->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-tomcat</artifactId>
            <scope>provided</scope>
        </dependency>
        <!--springboot 數據庫連接 mybatis<必要>-->
        <!--<dependency>
            <groupId>org.mybatis.spring.boot</groupId>
            <artifactId>mybatis-spring-boot-starter</artifactId>
            <version>2.1.3</version>
        </dependency>-->

        <dependency>
            <groupId>com.baomidou</groupId>
            <artifactId>mybatis-plus-boot-starter</artifactId>
            <version>3.2.0</version>
        </dependency>
        <dependency>
            <groupId>com.baomidou</groupId>
            <artifactId>dynamic-datasource-spring-boot-starter</artifactId>
            <version>2.5.6</version>
        </dependency>
        <!--阿里巴巴連接池-->
        <dependency>
            <groupId>com.alibaba</groupId>
            <artifactId>druid-spring-boot-starter</artifactId>
            <version>1.1.20</version>
        </dependency>
        <!--springboot 單元測試包<非必要,但一般都有>-->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
        <!--數據庫鏈接驅動  <連接ORACLE 必要>-->
        <dependency>
            <groupId>com.oracle.database.jdbc</groupId>
            <artifactId>ojdbc8</artifactId>
            <scope>runtime</scope>
        </dependency>
        <!--swagger 插件,用於查看API info  <非必要,輔助類插件>-->
        <dependency>
            <groupId>io.springfox</groupId>
            <artifactId>springfox-swagger2</artifactId>
            <version>2.9.2</version>
        </dependency>
        <dependency>
            <groupId>cn.easyproject</groupId>
            <artifactId>orai18n</artifactId>
            <version>12.1.0.2.0</version>
        </dependency>
        <dependency>
            <groupId>io.springfox</groupId>
            <artifactId>springfox-swagger-ui</artifactId>
            <version>2.9.2</version>
        </dependency>
        <!--swagger 插件  end-->
        <!--lombok 插件,需要安裝 
        用於實體類 @Date 註解應用,可以不用get set 方法  <非必要>-->
        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
            <scope>provided</scope>
        </dependency>
        <!--JSON轉化   讀取外部JSON 配置檔-->
        <dependency>
            <groupId>com.alibaba</groupId>
            <artifactId>fastjson</artifactId>
            <version>1.2.75</version>
        </dependency>
        <dependency>
            <groupId>commons-io</groupId>
            <artifactId>commons-io</artifactId>
            <version>2.5</version>
        </dependency>
        <!--切換記錄操作日誌 aop-->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-aop</artifactId>
        </dependency>
        <!--poi excel生成-->
        <dependency>
            <groupId>org.apache.poi</groupId>
            <artifactId>poi</artifactId>
            <version>5.0.0</version>
        </dependency>
        <dependency>
            <groupId>org.apache.poi</groupId>
            <artifactId>poi-ooxml</artifactId>
            <version>5.0.0</version>
        </dependency>
    </dependencies>


    <!--項目構建配置-->
    <build>
        <!--項目打包名稱-->
        <finalName>demo</finalName>
        <!--排除指定文件,json/*  和 yml 文件-->
        <resources>
            <!--<resource>
                <directory>src/main/resources</directory>
                <filtering>true</filtering>
                <excludes>
                    <exclude>**/*.json</exclude>
                    <exclude>**/*.yml</exclude>
                </excludes>
            </resource>-->
        </resources>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
            <!--<plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-resources-plugin</artifactId>
                <version>2.7</version>
                <configuration>
                    <delimiters>
                        <delimiter>@</delimiter>
                    </delimiters>
                    <useDefaultDelimiters>false</useDefaultDelimiters>
                </configuration>
            </plugin>-->
        </plugins>
    </build>

</project>

 类似资料: