【BUG】Failed to configure a DataSource: ‘url’ attribute is not specified and no embedded datasource could be configured.
报错截图:
报错内容:
Error starting ApplicationContext. To display the conditions report re-run your application with 'debug' enabled.
2023-08-10 18:31:09.742 ERROR 2220 --- [ main] o.s.b.d.LoggingFailureAnalysisReporter :
***************************
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).
报错原因分析:数据库配置异常。
报错溯源,一般可以从如下几点着手:
(1)首先检查项目的application.yml或application.properties中是否有添加数据库相关配置。比如:
spring:
datasource:
druid:
driver-class-name: com.mysql.cj.jdbc.Driver
url: jdbc:mysql://localhost:3306/xcplus_content?serverTimezone=Asia/Shanghai&useUnicode=true&characterEncoding=utf-8&zeroDateTimeBehavior=convertToNull&useSSL=false&allowPublicKeyRetrieval=true
username: root
password: root
(2)如果项目是多模块开发,同级模块都有application.yml 或 application.properties
同名文件的话,
可以尝试修改为不同的规范文件名,比如:application-dev.yml
(3)在SpringBootApplication注解中进行数据库配置的排除,即@SpringBootApplication(exclude= {DataSourceAutoConfiguration.class})。
(4)项目是多模块开发且不同模块中的配置文件,以application-*.yml的形式命名,比如application-mapper.yml,application-service.yml等,然后在最顶层的shop-web模块配置文件中,通过spring.profiles.active进行激活配置
spring:
profiles:
# 加载不同模块的配置文件
active: mapper,service
(5)其他原因:Resources资源目录下的application.yml
或 application.properties
的默认名称有被修改,导致程序启动时主启动类的配置文件无法被加载。这时需要修改回application.yml
来才行。