Failed to configure a DataSource: ‘url’ attribute is not specified and no embedded datasource could be configured.
某日新创建一个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
重新启动项目后就正常了。
如果报下面的错请参考
分类:
JAVA
标签:
springBoot
« 上一篇: Loading class `com.mysql.jdbc.Driver’. This is deprecated. The new driver class is `com.mysql.cj.jdbc.Driver’.
» 下一篇: org.thymeleaf.exceptions.TemplateInputException: Error resolving template [students/getList], template might not exist or might not be accessible by any of the configured Template Resolvers
» 下一篇: org.thymeleaf.exceptions.TemplateInputException: Error resolving template [students/getList], template might not exist or might not be accessible by any of the configured Template Resolvers
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 阿里最新开源QwQ-32B,效果媲美deepseek-r1满血版,部署成本又又又降低了!
· SQL Server 2025 AI相关能力初探
· AI编程工具终极对决:字节Trae VS Cursor,谁才是开发者新宠?
· 开源Multi-agent AI智能体框架aevatar.ai,欢迎大家贡献代码
· Manus重磅发布:全球首款通用AI代理技术深度解析与实战指南