baomidou / dynamic-datasource

dynamic datasource for springboot 多数据源 动态数据源 主从分离 读写分离 分布式事务
https://www.kancloud.cn/tracy5546/dynamic-datasource/2264611
Apache License 2.0
4.67k stars 1.19k forks source link

Failed to configure a DataSource #49

Closed VoidStarSp closed 5 years ago

VoidStarSp commented 5 years ago

Enviroment

JDK Version(required): 1.8 SpringBoot Version(required): 2.1.3.RELEASE

Starter Version(required): 2.5.3

Describe

使用的依赖: 'com.baomidou:mybatis-plus-boot-starter:3.1.0', 'com.baomidou:dynamic-datasource-spring-boot-starter:2.5.3', 'com.alibaba:druid-spring-boot-starter:1.1.16', 配置多数据源切换,起初用的是com.baomidou:dynamic-datasource-spring-boot-starter:1.0.0,但是在每隔查询之后会显示Creating a new SqlSession,于是准备升级一下,yml配置大概是这个结构 spring: datasource: dynamic: datasource: master: name: msyql url: jdbc:mysql://localhost:3307 slave: sqlServer: name: sqlServer url: jdbc:sqlserver://localhost:1433 然后启动不成功,识别不了yml的url Expected Result: 成功连接 Actual Result:
Failed to configure a DataSource: 'url' attribute is not specified and no embedded datasource could be configured.

Reason: Failed to determine a suitable driver class

Steps to reproduce

huayanYu commented 5 years ago

你没觉得少配置了驱动么? driver-class-name

VoidStarSp commented 5 years ago

你没觉得少配置了驱动么? driver-class-name

我只是没有贴全

spring:
    datasource:
        dynamic:
            datasource:
                master:
                    name: msyql
                    url: jdbc:mysql://localhost:3307/xxx?serverTimezone=GMT%2B8&characterEncoding=utf8&zeroDateTimeBehavior=convertToNull&transformedBitIsBoolean=true
                    username: 
                    password: 
                    # 使用druid数据源
                    type: com.alibaba.druid.pool.DruidDataSource
                    driver-class-name: com.mysql.jdbc.Driver
                    druid:
                        max-active: 20
                        initialSize: 1
                        max-wait: 60000
                        min-idle: 1
                        max-idle: 5
                        timeBetweenEvictionRunsMillis: 60000
                        minEvictableIdleTimeMillis: 300000
                        validationQuery: select 1
                        testWhileIdle: true
                        testOnBorrow: false
                        testOnReturn: false
                        poolPreparedStatements: true
                        maxOpenPreparedStatements: 20

                slave:
                    sqlServer:
                        name: sqlServer
                        url: jdbc:sqlserver://localhost:1433;databaseName=xxx
                        username: 
                        password: 
                        # 使用druid数据源
                        type: com.alibaba.druid.pool.DruidDataSource
                        driver-class-name: com.microsoft.sqlserver.jdbc.SQLServerDriver
                        max-active: 20
                        initialSize: 1
                        max-wait: 60000
                        min-idle: 1
                        max-idle: 5
                        timeBetweenEvictionRunsMillis: 60000
                        minEvictableIdleTimeMillis: 300000
                        validationQuery: select 1
                        testWhileIdle: true
                        testOnBorrow: false
                        testOnReturn: false
                        poolPreparedStatements: true
                        maxOpenPreparedStatements: 20
lihua123569 commented 5 years ago

我也遇到这类问题,这怎么没看到解决方法,就关闭了?

lihua123569 commented 5 years ago

你这个是mybatis-plus?还是用的druid?

gatspy commented 5 years ago

这个是由于 mybatis-plusdynamic-datasource 都引用了 spring-boot-starter-jdbc. Spring Boot自动配置尝试根据添加到类路径的依赖项自动配置Bean, 所以要么老实的在application配置文件中把数据源配置一次,要么就在SpringBootApplication注解中 exclude DataSourceAutoConfiguration

或者注解排除数据源自动配置类

@SpringBootApplication(exclude = {DataSourceAutoConfiguration.class})

或者 application.yml

spring:
datasource:
type: com.zaxxer.hikari.HikariDataSource
driver-class-name: com.mysql.jdbc.Driver
username: xxxx
password: xxxx
url: jdbc:mysql://xxxxxxxxxxx