dynamic-datasource-spring-boot-starter 多数据源配置

1、pom文件



4.0.0

org.springframework.boot
waterloggingdata
1.0-SNAPSHOT
jar
waterloggingdata


    org.springframework.boot
    spring-boot-starter-parent
    2.0.2.RELEASE
    



    UTF-8
    UTF-8
    1.8



    
    
        org.springframework.boot
        spring-boot-starter-web
    
    
        org.springframework.boot
        spring-boot-starter-test
        test
    
    

    
    
        com.baomidou
        dynamic-datasource-spring-boot-starter
        2.5.6
    
    
        mybatis-plus-boot-starter
        com.baomidou
        3.0.5
    
    
        
        
        
    
    
        org.springframework.boot
        spring-boot-starter-jdbc
    
    

    
    
        org.apache.velocity
        velocity
        1.7
    
    
        org.freemarker
        freemarker
        2.3.28
    
    
        org.springframework.boot
        spring-boot-starter-thymeleaf
    
    

    
    
        org.springframework.boot
        spring-boot-devtools
    
    

    
    
        org.projectlombok
        lombok
        provided
    
    

    
    
        mysql
        mysql-connector-java
        6.0.6
    

    
        com.alibaba
        druid-spring-boot-starter
        1.1.10
        true
    
    

    
    
        com.oracle
        ojdbc6
        11.2.0
    
    




    
        
            src/main/java
            false
            
                **/*.xml
            
        
        
        
            src/main/resources
        
    

    
        
            org.springframework.boot
            spring-boot-maven-plugin
        
    


2、配置文件
如果选择druid链接池要去除druid配置;还要配置一个默认数据源(spring.datasource.dynamic.primary)

#Service Config
server.port=8912
server.tomcat.uri-encoding=UTF-8

#DRUID POOL Congif
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=60000
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 1 FROM DUAL 
spring.datasource.dynamic.druid.test-while-idle=true
spring.datasource.dynamic.druid.test-on-borrow=false
spring.datasource.dynamic.druid.test-on-return=false
spring.datasource.dynamic.druid.pool-prepared-statements=true
spring.datasource.dynamic.druid.max-pool-prepared-statement-per-connection-size=20


#去除druid配置
spring.autoconfigure.exclude=com.alibaba.druid.spring.boot.autoconfigure.DruidDataSourceAutoConfigure



#默认数据源
spring.datasource.dynamic.primary=mysql
#mysql数据源配置
spring.datasource.dynamic.datasource.mysql.driver-class-name=com.mysql.cj.jdbc.Driver
spring.datasource.dynamic.datasource.mysql.url=jdbc\:mysql\://xxxxx?useOldAliasMetadataBehavior\=true&useUnicode\=true&characterEncoding\=UTF-8&autoReconnect\=true&autoReconnectForPools\=true&failOverReadOnly\=false&connectTimeout\=0&socketTimeout\=0&allowMultiQueries\=true&useSSL\=false&serverTimezone\=Hongkong&rewriteBatchedStatements\=true
spring.datasource.dynamic.datasource.mysql.username=xxxx
spring.datasource.dynamic.datasource.mysql.password=xxxx

#oracle数据源配置
spring.datasource.dynamic.datasource.oracle.driver-class-name=oracle.jdbc.driver.OracleDriver
spring.datasource.dynamic.datasource.oracle.url=jdbc:oracle:thin:@(DESCRIPTION=(ADDRESS_LIST= (ADDRESS=(PROTOCOL=TCP)(HOST=10.48.36.150)(PORT=1521)))(CONNECT_DATA=(SERVICE_NAME=racdb)))
spring.datasource.dynamic.datasource.oracle.username=xxxx
spring.datasource.dynamic.datasource.oracle.password=xxxx

#mybatis-plus
mybatis-plus.mapper-locations=classpath\:sqlMapperXml/*Mapper.xml
mybatis-plus.typeAliasesPackage=com.dream.team.model
mybatis-plus.global-config.id-type=2
mybatis-plus.global-config.field-strategy=2
mybatis-plus.global-config.auto-set-db-type=true
mybatis-plus.global-config.db-column-underline=true
mybatis-plus.global-config.refresh-mapper=true
mybatis-plus.configuration.map-underscore-to-camel-case=true
mybatis-plus.configuration.cache-enabled=false
mybatis-plus.configuration.jdbc-type-for-null=null
mybatis-plus.configuration.call-setters-on-nulls=true

# ELASTICSEARCH (ElasticsearchProperties)
# Elasticsearch cluster name.
spring.data.elasticsearch.cluster-name=elasticsearch
# Comma-separated list of cluster node addresses.
spring.data.elasticsearch.cluster-nodes=127.0.0.1:9300
# Whether to enable Elasticsearch repositories.
spring.data.elasticsearch.repositories.enabled=true


logging.level.root=info
logging.level.com.caody.muyi=debug
logging.path=logs/

3、数据源切换

@Service
@DS("oracle")
public class QxdmsSurfGuojiaDataHourServiceImpl extends ServiceImpl implements IQxdmsSurfGuojiaDataHourService {

}

你可能感兴趣的:(dynamic-datasource-spring-boot-starter 多数据源配置)