作者:Eddy  历史版本:1  最后编辑:龚清  更新时间:2024-09-20 15:13

使用xml配置数据源

配置文件

数据元类型配置文件

在对应服务下的config/application-app.yml

##---------数据库配置---------
#数据库类型 oracle,mysql,postgres,mssql,dm,kingbase,oscar
db:
  encrypt: ${DB_ENCRYPT:false}
  dbType: ${DB_TYPE:mysql}

数据源配置文件

在对应服务下的conf中有多个数据库配置文件,对应服务启动的数据库类型和环境。

|   |   |   |-- dataSource-cloud.xml
|   |   |   |-- dataSource-dev.xml
|   |   |   |-- dataSource-prod.xml
|   |   |   |-- dataSource-test.xml
|   |   |   |-- ...

单数据源配置

默认数据库配置都是只有一个数据源,修改<dataSource>,参考如下。

<?xml version="1.0" encoding="UTF-8"?>
<dataSources>
  <dataSource>
    <name>def</name>
    <alias>dataSource_default</alias>

    <!-- dev下使用该链接 -->
     <dbType>mysql</dbType>
      <driver>com.mysql.jdbc.Driver</driver>
    <driverUrl>jdbc:mysql://192.168.3.210:3306/ibps_platform_dev_v3.3.7?useUnicode=true&amp;characterEncoding=utf-8&amp;autoReconnect=true&amp;useSSL=false&amp;serverTimezone=UTC</driverUrl>
    <user>root</user>
    <password>root</password>

    <status>actived</status>
    <isDefault>true</isDefault>
    <variables>
      <validationQuery>SELECT 1 FROM IBPS_RIGHTS_CONF</validationQuery>
      <testWhileIdle>true</testWhileIdle>
      <testOnBorrow>false</testOnBorrow>
      <testOnReturn>false</testOnReturn>
      <maxPoolPreparedStatementPerConnectionSize>20</maxPoolPreparedStatementPerConnectionSize>
      <removeAbandonedTimeout>1800</removeAbandonedTimeout>
      <logAbandoned>true</logAbandoned>
      <poolPreparedStatements>true</poolPreparedStatements>
      <maxActive>64</maxActive>
      <minIdle>10</minIdle>
      <initialSize>5</initialSize>
      <removeAbandoned>true</removeAbandoned>
      <!-- 连接池超时应该小于服务端设置的超时,MySQL默认8h=8*60*60=28800s=28800000ms -->
      <!-- 连接池超时就应该小于28800000ms -->
      <maxWait>300000</maxWait><!-- 300s -->
      <timeBetweenEvictionRunsMillis>300000</timeBetweenEvictionRunsMillis><!-- 300s -->
      <minEvictableIdleTimeMillis>180000</minEvictableIdleTimeMillis><!-- 180s -->
      <filters>stat</filters>
    </variables>
  </dataSource>
</dataSources>

多数据源配置

默认数据库配置都是只有一个数据源,需要多数据源可自行添加,即添加多个<dataSource>,参考如下。

<?xml version="1.0" encoding="UTF-8"?>
<dataSources>
  <dataSource>
    <name>def</name>
    <alias>dataSource_default</alias>

    <!-- dev下使用该链接 -->
     <dbType>mysql</dbType>
      <driver>com.mysql.jdbc.Driver</driver>
    <driverUrl>jdbc:mysql://192.168.3.210:3306/ibps_platform_dev_v3.3.7?useUnicode=true&amp;characterEncoding=utf-8&amp;autoReconnect=true&amp;useSSL=false&amp;serverTimezone=UTC</driverUrl>
    <user>root</user>
    <password>root</password>

    <status>actived</status>
    <isDefault>true</isDefault>
    <variables>
      <validationQuery>SELECT 1 FROM IBPS_RIGHTS_CONF</validationQuery>
      <testWhileIdle>true</testWhileIdle>
      <testOnBorrow>false</testOnBorrow>
      <testOnReturn>false</testOnReturn>
      <maxPoolPreparedStatementPerConnectionSize>20</maxPoolPreparedStatementPerConnectionSize>
      <removeAbandonedTimeout>1800</removeAbandonedTimeout>
      <logAbandoned>true</logAbandoned>
      <poolPreparedStatements>true</poolPreparedStatements>
      <maxActive>64</maxActive>
      <minIdle>10</minIdle>
      <initialSize>5</initialSize>
      <removeAbandoned>true</removeAbandoned>
      <!-- 连接池超时应该小于服务端设置的超时,MySQL默认8h=8*60*60=28800s=28800000ms -->
      <!-- 连接池超时就应该小于28800000ms -->
      <maxWait>300000</maxWait><!-- 300s -->
      <timeBetweenEvictionRunsMillis>300000</timeBetweenEvictionRunsMillis><!-- 300s -->
      <minEvictableIdleTimeMillis>180000</minEvictableIdleTimeMillis><!-- 180s -->
      <filters>stat</filters>
    </variables>
  </dataSource>

  <dataSource>
    <name>test</name>
    <alias>dataSource_test</alias>

    <!-- test下使用该链接 -->
     <dbType>mysql</dbType>
      <driver>com.mysql.jdbc.Driver</driver>
    <driverUrl>jdbc:mysql://192.168.3.210:3306/ibps_platform_test_v3.3.7?useUnicode=true&amp;characterEncoding=utf-8&amp;autoReconnect=true&amp;useSSL=false&amp;serverTimezone=UTC</driverUrl>
    <user>root</user>
    <password>root</password>

    <status>actived</status>
    <isDefault>false</isDefault>
    <variables>
      <validationQuery>SELECT 1 FROM IBPS_RIGHTS_CONF</validationQuery>
      <testWhileIdle>true</testWhileIdle>
      <testOnBorrow>false</testOnBorrow>
      <testOnReturn>false</testOnReturn>
      <maxPoolPreparedStatementPerConnectionSize>20</maxPoolPreparedStatementPerConnectionSize>
      <removeAbandonedTimeout>1800</removeAbandonedTimeout>
      <logAbandoned>true</logAbandoned>
      <poolPreparedStatements>true</poolPreparedStatements>
      <maxActive>64</maxActive>
      <minIdle>10</minIdle>
      <initialSize>5</initialSize>
      <removeAbandoned>true</removeAbandoned>
      <!-- 连接池超时应该小于服务端设置的超时,MySQL默认8h=8*60*60=28800s=28800000ms -->
      <!-- 连接池超时就应该小于28800000ms -->
      <maxWait>300000</maxWait><!-- 300s -->
      <timeBetweenEvictionRunsMillis>300000</timeBetweenEvictionRunsMillis><!-- 300s -->
      <minEvictableIdleTimeMillis>180000</minEvictableIdleTimeMillis><!-- 180s -->
      <filters>stat</filters>
    </variables>
  </dataSource>
</dataSources>

代码中使用

try {
    DbContextHolder.setDataSource("dataSource_test", "mysql");
}
catch (Exception e) {
    throw e;
}
finally {
    DbContextHolder.clearDataSource();
}