Failed to configure a DataSource: 'url' attribute is not specified and no embedded的解决办法

本文解决了一个常见的Spring Boot项目启动时报错的问题,即未能正确配置DataSource。主要原因是URL未指定或内嵌数据库配置失败。通过添加必要的Maven依赖并正确配置数据源属性,成功解决了该问题。

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

报错如下:

***************************
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).

这个错误的大概意思是:配置DataSource失败,连接数据库的url属性没有指定和没有配置内嵌的数据源

问题原因: Mybatis没有找到合适的加载类,其实是大部分spring - datasource - url没有加载成功,分析原因如下所示.

  1. DataSourceAutoConfiguration会自动加载.

  2. 没有配置spring - datasource - url 属性.

  3. spring - datasource - url 配置的地址格式有问题.

  4. 配置 spring - datasource - url的文件没有加载.

关于这个问题,我的解决办法是赋值以前对的数据源和修改相关依赖:

为pom.xml文件中添加的依赖:

<!-- jdbc -->
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-jdbc</artifactId>
</dependency>
<!-- 数据库驱动 -->
<dependency>
    <groupId>mysql</groupId>
    <artifactId>mysql-connector-java</artifactId>
</dependency>

修改配置文件中的属性信息:

server.port=8081
spring.datasource.driver-class-name=com.mysql.jdbc.Driver
spring.datasource.url=jdbc:mysql://localhost:3306/javaee?useUnicode=true&characterEncoding=utf-8&useSSL=false&serverTimezone = GMT
spring.datasource.username=root
spring.datasource.password=root
mybatis.type-aliases-package=com.simple.test.pojo

在配置这两个文件后,启动项目就没有问题了。


文件参考:

第一个springboot项目启动报错Failed to configure a DataSource: 'url' attribute is not specified and no embedde

SpringBoot 2.0 报错: Failed to configure a DataSource: 'url' attribute is not specified and no embe...(这个文件有详细说明原因)

Failed to configure a DataSource: 'url' attribute is not specified and no embedded 的三种解决办法

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值