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
处理方法
- application.yml和application.properties ,文件名是否拼写错误,配置信息是否书写有误;
- 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.1、Mysql 5.0、Mysql 5.1、Mysql 6.0 alpha这些版本。
Connector/J 5.0 支持MySQL 4.1、MySQL 5.0 servers、distributed transaction (XA)。
Connector/J 3.1 支持MySQL 4.1、MySQL 5.0 servers、MySQL 5.0 except distributed transaction (XA) support。
Connector/J 3.0 支持MySQL 3.x or MySQL 4.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>
- 在启动类添加禁止自动扫描数据源的注解(多数据源场景会使用,新工程中未进行数据源信息配置也会使用),根据配置的依赖去写注解或者配置信息;
@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.* 相关属性并自动配置单数据源