【无标题】

Failed to determine a suitable driver class

Springboot 使用mysql驱动启动异常

玩项目遇到了两次的问题,记录一下。。。。

***************************
APPLICATION FAILED TO START
***************************

Description:

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


Action:

Consider the following:
    If you want an embedded database (H2, HSQL or Derby), please put it on the classpath.
    If you have database settings to be loaded from a particular profile you may need to activate it (no profiles are currently active).

配置

spring:
  datasource:
    # JDBC >= 6.x com mysql.jdbc.Driver 需要指定时区serverTimezone:
    # JDBC <= 5.x com mysql.jdbc.Driver
      driver-class-name: com.mysql.cj.jdbc.Driver
      url: jdbc:mysql://XXXXXXXX/db?useSSL=false&useUnicode=true&characterEncoding=utf-8&zeroDateTimeBehavior=convertToNull&transformedBitIsBoolean=true&tinyInt1isBit=false&allowMultiQueries=true&serverTimezone=GMT%2B8&allowPublicKeyRetrieval=true
      username: user
      password: 123123

处理方法

  1. application.ymlapplication.properties ,文件名是否拼写错误,配置信息是否书写有误;
  2. jdbc驱动类是否导入成功,成功还报错查询mysql的版本和驱动版本是否匹配;
    (自己mysql 5.6.39 使用驱动8.0.26,是没问题的)
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
        </dependency>

网上找的对应版本:
Connector/J 5.1 支持Mysql 4.1Mysql 5.0Mysql 5.1Mysql 6.0 alpha这些版本。
Connector/J 5.0 支持MySQL 4.1MySQL 5.0 servers、distributed transaction (XA)Connector/J 3.1 支持MySQL 4.1MySQL 5.0 servers、MySQL 5.0 except distributed transaction (XA) support。
Connector/J 3.0 支持MySQL 3.x or MySQL 4.1
  1. 配置文件没有被扫描到,在pom中build添加(自己的问题在此解决= =):
<resources>
    <resource>
        <directory>src/main/java</directory>
        <includes>
            <include>**/*.yml</include>
            <include>**/*.properties</include>
            <include>**/*.xml</include>
        </includes>
        <filtering>false</filtering>
    </resource>
    <resource>
        <directory>src/main/resources</directory>
        <includes>
            <include>**/*.yml</include>
            <include>**/*.properties</include>
            <include>**/*.xml</include>
        </includes>
        <filtering>false</filtering>
    </resource>
</resources>
  1. 在启动类添加禁止自动扫描数据源的注解(多数据源场景会使用,新工程中未进行数据源信息配置也会使用),根据配置的依赖去写注解或者配置信息
@SpringBootApplication(exclude = {DataSourceAutoConfiguration.class, DruidDataSourceAutoConfigure.class, MybatisAutoConfiguration.class})@EnableAutoConfiguration(exclude = {DataSourceAutoConfiguration.class, DruidDataSourceAutoConfigure.class, MybatisAutoConfiguration.class} )

yml中排除也可

#springboot排除自动配置
spring:
  autoconfigure:
    exclude:
      - org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration
      - com.alibaba.druid.spring.boot.autoconfigure.DruidDataSourceAutoConfigure
      - org.mybatis.spring.boot.autoconfigure.MybatisAutoConfiguration
spring.autoconfigure.exclude[0]=org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration
spring.autoconfigure.exclude[1]=com.alibaba.druid.spring.boot.autoconfigure.DruidDataSourceAutoConfigure
spring.autoconfigure.exclude[2]=org.mybatis.spring.boot.autoconfigure.MybatisAutoConfiguration

如果该类不在类路径中,则可以使用excludeName注释的属性,并指定完全限定的名称(全类名字符串)。定义排除项,即可以使用哪个注释级别也可以使用属性来定义。

@SpringBootApplication(excludeName = {"org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration", "org.springframework.boot.autoconfigure.mail.MailSenderAutoConfiguration"})

DataSourceAutoConfiguration.class 会自动查找 application.yml 或者 properties 文件里的 spring.datasource.* 相关属性并自动配置单数据源

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值