SpringBoot启动报错Failed to configure a DataSource: ‘url’ attribute is not specified and no embedded datasource could be configured

Java技术 潘老师 3年前 (2021-01-27) 6223 ℃ (1) 扫码查看

有些同学在使用SpringBoot搭建项目时,启动Application提示如下错误:

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

具体如图:
SpringBoot启动报错Failed to configure a DataSource: 'url' attribute is not specified and no embedded datasource could be configured
1、错误的意思大概是:未能确定合适的驱动程序类导致配置数据源失败

解决办法1:

可能的原因如下:
pom.xml中导入了类似如下mybatis依赖:

<dependency>
    <groupId>org.mybatis.spring.boot</groupId>
    <artifactId>mybatis-spring-boot-starter</artifactId>
    <version>2.1.4</version>
</dependency>

但是在核心配置文件application.propertiesapplication.yml中没有配置数据库连接相关的属性,比如url、dirver、username、password等。
解决:
application.properties中新增数据库连接配置:

#数据库连接配置
spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
spring.datasource.url=jdbc:mysql://localhost:3306/ssm?characterEncoding=utf-8&serverTimezone=UTC
spring.datasource.username=root
spring.datasource.password=123456

或在application.yml中新增数据库连接配置:

#spring配置
spring:
  #数据库连接配置
  datasource:
    driver-class-name: com.mysql.cj.jdbc.Driver
    url: jdbc:mysql://localhost:3306/ssm?characterEncoding=utf-8&serverTimezone=UTC
    username: root
    password: 123456
解决办法2:

如果你添加了有关库的依赖但是又不想配置库的连接,可以使用Application启动类上@SpringBootApplication注解中配置如下即可:

@SpringBootApplication(exclude = {DataSourceAutoConfiguration.class})
解决办法3:

如果第一种方式你尝试了但是还不能解决,还有一个可能的原因就是你的resources目录并没有设置为资源目录导致的,典型的特征就是Resources目录没有小黄标,如下:
SpringBoot启动报错Failed to configure a DataSource: 'url' attribute is not specified and no embedded datasource could be configured
我们需要打开File->Poroject Structure->Modules->Sources,展开目录找到resources目录将其置为Resources,如下:
SpringBoot启动报错Failed to configure a DataSource: 'url' attribute is not specified and no embedded datasource could be configured
然后apply->save,效果如下:
SpringBoot启动报错Failed to configure a DataSource: 'url' attribute is not specified and no embedded datasource could be configured


版权声明:本站文章,如无说明,均为本站原创,转载请注明文章来源。如有侵权,请联系博主删除。
本文链接:https://www.panziye.com/java/1873.html
喜欢 (1)
请潘老师喝杯Coffee吧!】
分享 (0)
用户头像
发表我的评论
取消评论
表情 贴图 签到 代码

Hi,您需要填写昵称和邮箱!

  • 昵称【必填】
  • 邮箱【必填】
  • 网址【可选】

(1) 个小伙伴在畅所欲言
  1. 用户头像
    还有一种就是没有编译进去,看看target有没有
    1500240116 2022-03-12 03:20 回复