本地的SpringBoot项目平时起都没问题,想验证个功能点的时候突然起不来,报这个错误
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).
看了下不是因为数据库连接的问题,再看profile也激活了,

试了各种方式都不行,然后百思不得其解。苦思冥想中试了下不经过编译直接运行,发现竟然好使了,灵机一动看下编译出来的target文件。
经过mvn命令:

不经过mvn命令直接启动:

从上面图中可以看出问题了,经过mvn命令的缺少了配置文件,所以运行不起来,
解决办法是:
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
<resources>
<resource>
<directory>src/main/java</directory>
<includes>
<include>**/*.xml</include>
</includes>
</resource>
----------------------指定将resources中配置文件加载到target-----------------------
<resource>
<directory>src/main/resources</directory>
<includes>
<include>**/*.properties</include>
</includes>
</resource>
--------------------------指定将resources中配置文件加载到target-------------------
</resources>
</build>
希望能帮助到遇到同样问题的你(#^.^#)~。
博客作者遇到了SpringBoot项目启动时 DataSource 配置问题,报错提示未指定 url 和无法确定合适的驱动类。尽管检查了数据库连接和激活的 profile,问题依然存在。作者发现直接运行而不通过 maven 编译可以正常启动,进一步排查发现是 mvn 命令未正确打包资源文件。解决方案是在 Maven 的 build 部分添加资源配置,确保 resources 目录下的配置文件被正确包含。修复后项目成功启动。
3万+

被折叠的 条评论
为什么被折叠?



