红茶的个人站点

  • 首页
  • 专栏
  • 开发工具
  • 其它
  • 隐私政策
Awalon
Talk is cheap,show me the code.
  1. 首页
  2. 开发工具
  3. 正文

DEBUG:无法连接数据库

2025年6月5日 7点热度 0人点赞 0条评论

问题描述

使用 Idea 启动 Spring Boot 应用时有如下错误:

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

似乎是因为配置文件中缺少数据库连接相关信息导致的数据库连接不上的问题。

但查看配置文件是没有问题的:

image-20250605095359168

问题排查

Idea 启动应用前需要先编译代码为字节码(.class),再将字节码和资源文件(/resources 目录下的静态文件)打包到指定目录(/target/classes)下,然后运行相应的字节码。

所以源码目录下的配置文件没有问题并不能确保编译打包后的字节码环境没有问题。

查看打包目录:

image-20250605095839910

只将资源目录中的 xml 文件打包进去了,proerties.yml配置文件并不存在,因此产生了缺少 url 和驱动类的错误信息。

Idea 使用 mvn 打包,查看 POM 中的 Maven 配置:

<build>
    <resources>
        <resource>
            <directory>src/main/resources</directory>
            <!-- 开启过滤 -->
            <filtering>true</filtering>
            <!-- 包含 XML 文件 -->
            <includes>
                <include>**/*.xml</include>
            </includes>
        </resource>
    </resources>
</build>

这里开启了资源过滤(filtering),因此只包含 xml 文件,没有打包配置文件。

问题处理

修改配置,打包时候包含配置文件:

<resources>
    <resource>
        <directory>src/main/resources</directory>
        <!-- 开启过滤 -->
        <filtering>true</filtering>
        <!-- 包含 XML 文件 -->
        <includes>
            <include>**/*.yml</include>
            <include>**/*.yaml</include>
            <include>**/*.xml</include>
        </includes>
    </resource>
</resources>

当然也可以直接删除 Resources 配置,如果你不需要过滤功能。或者也可以改为排除(exclude)部分你不想打包的文件。

本作品采用 知识共享署名 4.0 国际许可协议 进行许可
标签: debug IDEA maven spring
最后更新:2025年6月5日

魔芋红茶

加一点PHP,加一点Go,加一点Python......

点赞
< 上一篇

文章评论

razz evil exclaim smile redface biggrin eek confused idea lol mad twisted rolleyes wink cool arrow neutral cry mrgreen drooling persevering
取消回复

*

code

COPYRIGHT © 2021 icexmoon.cn. ALL RIGHTS RESERVED.
本网站由提供CDN加速/云存储服务

Theme Kratos Made By Seaton Jiang

宁ICP备2021001508号

宁公网安备64040202000141号