Failed to configure a DataSource: ‘url’ attribute is not specified and no embedded datasource could be configured.

原文链接:Failed to configure a DataSource: ‘url’ attribute is not specified and no embedded datasource could be configured. – 每天进步一点点 (longkui.site)

某日新创建一个spring boot项目,添加完依赖以后运行项目开始报错:

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).

原因1:没有配置文件

这个报错主要是因为数据库没有配置,比如一开始这个项目,添加完mysql依赖后直接启动项目导致没有读取到mysql的相关配置,这个时候可以先注释掉mysql的依赖,然后刷新maven依赖重新启动项目。

如果配置了相关的文件或者想要使用配置文件,可以继续往下看。

原因2:配置不正确

如果确实加入了mysql的相关配置,大概率是配置的格式不正确,比如下面这样:

一共两个错误,一是spring前面有空格,它的层级和上面的port一级了。

二是url后面的配置没有空格,可以看出url这个没有正常变色

(这种写法很像python!)

修改错误后,改成下面这样:

server:
port: 8082
 
# spring配置
spring:
datasource:
url: jdbc:mysql://localhost:3306/test?useUnicode=true&characterEncoding=utf-8&useSSL=true&serverTimezone=UTC
username: root
password: root
driver-class-name: com.mysql.cj.jdbc.Driver

重新启动项目后就正常了。

如果报下面的错请参考

Loading class `com.mysql.jdbc.Driver’. This is deprecated. The new driver class is `com.mysql.cj.jdbc.Driver’.

 

posted on   longkui  阅读(766)  评论(0编辑  收藏  举报

相关博文:
阅读排行:
· 阿里最新开源QwQ-32B,效果媲美deepseek-r1满血版,部署成本又又又降低了!
· SQL Server 2025 AI相关能力初探
· AI编程工具终极对决:字节Trae VS Cursor,谁才是开发者新宠?
· 开源Multi-agent AI智能体框架aevatar.ai,欢迎大家贡献代码
· Manus重磅发布:全球首款通用AI代理技术深度解析与实战指南

导航

< 2025年3月 >
23 24 25 26 27 28 1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28 29
30 31 1 2 3 4 5
点击右上角即可分享
微信分享提示