今天在启动项目时,遇到如下问题:
***************************
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).
Disconnected from the target VM, address: '127.0.0.1:64983', transport: 'socket'
Process finished with exit code 0
从而,导致项目无法启动:
即Failed to determine a suitable driver class.
我们将这句错误Failed to determine a suitable driver class.
翻译为无法确定合适的驱动程序类
。
但我已经配置了连接mysql
的驱动类,如下代码所示:
<properties>
<java.version>1.8java.version>
<mybatis-version>2.2.2mybatis-version>
properties>
<dependency>
<groupId>mysqlgroupId>
<artifactId>mysql-connector-javaartifactId>
<scope>runtimescope>
dependency>
<dependency>
<groupId>org.mybatis.spring.bootgroupId>
<artifactId>mybatis-spring-boot-starterartifactId>
<version>${mybatis-version}version>
dependency>
根据网上资料,说需要配置如下依赖:
<dependency>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-starter-jdbcartifactId>
dependency>
但是,即便配置了该依赖,项目还是无法启动。
于是,再次去看application.yml
:
spring:
# 指定哪个文件,比如dev.yml local.yml
config:
activate:
on-profile:
- @spring.active@
# 应用名称
application:
name: lowCode
datasource:
driver-class-name: com.mysql.cj.jdbc.Driver
password: 123456
username: root
url: jdbc:mysql://localhost:3306/test?useUnicode=true&characterEncoding=utf8&useSSL=false
# 连接池配置
druid:
# 初始化大小,最小,最大
initial-size: 5
min-idle: 5
max-active: 20
# 配置获取连接等待超时的时间
max-wait: 60000
# 配置间隔多久才进行一次检测,检测需要关闭的空闲连接,单位毫秒
time-between-eviction-runs-millis: 60000
# 配置一个连接在池中最小生存时间
min-evictable-idle-time-millis: 300000
validation-query: SELECT 1 FROM sys_user
test-while-idle: true
test-on-borrow: false
test-on-return: false
我配置了druid
连接池,但没有引入druid
依赖。
正因为没有引入druid
依赖,才报出了上述错误,进而引入如下依赖:
<properties>
<java.version>1.8java.version>
<alibabaDruidStarter.version>1.2.11alibabaDruidStarter.version>
properties>
<dependency>
<groupId>com.alibabagroupId>
<artifactId>druid-spring-boot-starterartifactId>
<version>${alibabaDruidStarter.version}version>
dependency>
但是,项目还是无法启动,报出同样的错误。
经过反复调试,原来我的上述applicaltion.yml
文件配置出现错误,不应该使用如下代码配置active
文件:
spring:
# 指定哪个文件,比如dev.yml local.yml
config:
activate:
on-profile:
- @spring.active@
而是配置成如下代码:
spring:
# 指定哪个文件,比如dev.yml local.yml
profiles:
active: @spring.active@
这两种不同配置的区别,可以参考我的这篇文章: https://blog.csdn.net/lvoelife/article/details/126350747
如是修改后,便可成功启动了项目:
【备注】:对spring.active
不了解的,可以网上自行查找资料。
出现上述错误,一般有如下解决方案:
查看是否缺少了驱动类。
application.yml
的文件是否配置有误:
datasource
的相关属性是否配置有误,例如:地址值啊,数据库驱动啊,用户名啊,密码啊
指定文件(@spring.active@
)是否有误