四种方案解决报错:Failed to configure a DataSource: 'url' attribute is not specified and no embedded datasour

本文提供四种解决方案,针对Spring Boot应用中出现的DataSource配置错误,包括排除自动配置、检查依赖、修改pom.xml以及正确配置数据库参数。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

四种方案解决报错:Failed to configure a DataSource: 'url' attribute is not specified and no embedded datasource could be configured.

方案一

在@SpringBootApplication后添加exclude = { DataSourceAutoConfiguration.class }来排除自动配置 :

@SpringBootApplication(exclude = { DataSourceAutoConfiguration.class })

方案二

在application.yml文件中添加排除自动配置

spring:
  autoconfigure:
    exclude: org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration

方案三

检查pom依赖有没有直接或者间接的导入了下面的依赖
在这里插入图片描述
比如A模块依赖了leyou-item-interface模块:
在这里插入图片描述
leyou-item-interface模块中依赖了
在这里插入图片描述
mapper-spring-boot-starter中依赖了

<dependency>
   <groupId>org.springframework.boot</groupId>
   <artifactId>spring-boot-starter-jdbc</artifactId>
</dependency>

所以会加载自动配置类,如果项目不想连接数据库的话可以选择导入更小的依赖,因为我的leyou-item-interface模块用到了persistence所以直接将
在这里插入图片描述
改为
在这里插入图片描述
这样就不会导入spring-boot-starter-jdbc的模块了,也就不会自动配置了,完美解决。

方案四

前面三种方案都是项目不用数据库的情况下,如果用数据库还报这个错基本就是四大参数没配置:

spring:
  datasource:
    url: jdbc:mysql://localhost:3306/xxxx
    username: root
    password: root
    driver-class-name: com.mysql.jdbc.Driver

结语

产生这种错误的原因是:
导入了spring-boot-starter-jdbc模块但是没有配置四大参数
解决起来也不难,要么配置,要么不用。

第一次写博客,请各位大佬多多指教~~~

Spring Boot是一个用于快速开发Java应用程序的框架,它提供了很多便捷的功能和配置选项。在使用Spring Boot时,有时会遇到一些报错信息,其中一个常见的报错是"Failed to configure a DataSource: 'url' attribute is not specified and no embedded"。 这个报错通常是由于没有正确配置数据源引起的。在Spring Boot中,数据源是用于连接数据库的重要组件。当你在应用程序中使用数据库时,需要配置数据源的相关信息,如数据库的URL、用户名、密码等。 出现这个报错的原因是因为在配置文件中没有正确指定数据源的URL属性,并且也没有使用嵌入式数据库。解决这个问题的方法有两种: 1. 配置数据源的URL属性:在你的应用程序的配置文件(如application.properties或application.yml)中,添加以下配置项: ``` spring.datasource.url=jdbc:mysql://localhost:3306/mydatabase ``` 其中,`jdbc:mysql://localhost:3306/mydatabase`是你数据库的URL,根据实际情况进行修改。 2. 使用嵌入式数据库:如果你不想手动配置数据源,可以考虑使用Spring Boot提供的嵌入式数据库(如H2、HSQLDB等)。在这种情况下,你只需要在配置文件中添加以下配置项: ``` spring.datasource.url=jdbc:h2:mem:testdb spring.datasource.driver-class-name=org.h2.Driver spring.datasource.username=sa spring.datasource.password= ``` 这样就可以使用嵌入式数据库进行开发和测试了。
评论 17
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值