You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

无法运行Spring Boot应用:数据源配置失败(驱动类未找到)

问题:Spring Boot启动失败,数据源配置异常

我从零开始开发Spring Boot项目,还未编写任何代码就尝试启动应用,弹出了报错。另外之前能正常运行的现有Spring Boot项目也出现相同报错,我是Spring Boot新手,想确认是否是环境变量被删除或修改导致的问题。

报错日志

OpenJDK 64-Bit Server VM warning: Options -Xverify:none and -noverify were deprecated in JDK 13 and will likely be removed in a future release.

  .   ____          _            __ _ _
 /\ / ___'_ __ _ _(_)_ __  __ _ \ \ \\ \
( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \\ \
 \\/  ___)| |_)| | | | | || (_| |  ) ) ) )
  '  |____| .__|_| |_|_| |_\\__, | / / / /
 =========|_|==============|___/=/_/_/_/
 :: Spring Boot ::                (v3.1.0)

2023-06-12T21:33:13.531+05:30  INFO 10624 --- [           main] com.example.food.FoodApplication         : Starting FoodApplication using Java 17.0.3 with PID 10624 (F:\YOU_SEE_ME\food\target\classes started by shaneeqaz in F:\YOU_SEE_ME\food)
2023-06-12T21:33:13.589+05:30  INFO 10624 --- [           main] com.example.food.FoodApplication         : No active profile set, falling back to 1 default profile: "default"
2023-06-12T21:33:20.704+05:30  INFO 10624 --- [           main] .s.d.r.c.RepositoryConfigurationDelegate : Bootstrapping Spring Data JPA repositories in DEFAULT mode.
2023-06-12T21:33:20.883+05:30  INFO 10624 --- [           main] .s.d.r.c.RepositoryConfigurationDelegate : Finished Spring Data repository scanning in 101 ms. Found 0 JPA repository interfaces.
2023-06-12T21:33:24.540+05:30  INFO 10624 --- [           main] o.s.b.w.embedded.tomcat.TomcatWebServer  : Tomcat initialized with port(s): 8080 (http)
2023-06-12T21:33:24.593+05:30  INFO 10624 --- [           main] o.apache.catalina.core.StandardService   : Starting service [Tomcat]
2023-06-12T21:33:24.593+05:30  INFO 10624 --- [           main] o.apache.catalina.core.StandardEngine    : Starting Servlet engine: [Apache Tomcat/10.1.8]
2023-06-12T21:33:25.110+05:30  INFO 10624 --- [           main] o.a.c.c.C.[Tomcat].[localhost].[/]       : Initializing Spring embedded WebApplicationContext
2023-06-12T21:33:25.113+05:30  INFO 10624 --- [           main] w.s.c.ServletWebServerApplicationContext : Root WebApplicationContext: initialization completed in 11045 ms
2023-06-12T21:33:25.481+05:30  WARN 10624 --- [           main] ConfigServletWebServerApplicationContext : Exception encountered during context initialization - cancelling refresh attempt: org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'dataSourceScriptDatabaseInitializer' defined in class path resource [org/springframework/boot/autoconfigure/sql/init/DataSourceInitializationConfiguration.class]: Unsatisfied dependency expressed through method 'dataSourceScriptDatabaseInitializer' parameter 0: Error creating bean with name 'dataSource' defined in class path resource [org/springframework/boot/autoconfigure/jdbc/DataSourceConfiguration$Hikari.class]: Failed to instantiate [com.zaxxer.hikari.HikariDataSource]: Factory method 'dataSource' threw exception with message: Failed to determine a suitable driver class
2023-06-12T21:33:25.486+05:30  INFO 10624 --- [           main] o.apache.catalina.core.StandardService   : Stopping service [Tomcat]
2023-06-12T21:33:25.601+05:30  INFO 10624 --- [           main] .s.b.a.l.ConditionEvaluationReportLogger : 

Error starting ApplicationContext. To display the condition evaluation report re-run your application with 'debug' enabled.
2023-06-12T21:33:25.668+05:30 ERROR 10624 --- [           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).


Process finished with exit code 1

项目pom.xml文件

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>3.1.0</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>
    <groupId>com.example</groupId>
    <artifactId>food</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>food</name>
    <description>Food Ordering</description>
    <properties>
        <java.version>17</java.version>
    </properties>
    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-data-jpa</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>

        <dependency>
            <groupId>com.mysql</groupId>
            <artifactId>mysql-connector-j</artifactId>
            <scope>runtime</scope>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>

</project>
解决方法

核心原因

项目引入了spring-boot-starter-data-jpa依赖,Spring Boot会自动触发数据源配置逻辑,但你既没提供MySQL连接信息,也没引入嵌入式数据库依赖,导致启动失败。现有项目也报错,大概率是环境变量中的数据库配置被修改/删除,或者本地MySQL服务未启动。

具体修复步骤

1. 临时禁用数据源配置(无数据库需求时)

如果暂时不需要数据库功能,可以在启动类上添加注解,禁用自动数据源配置:

@SpringBootApplication(exclude = {DataSourceAutoConfiguration.class, HibernateJpaAutoConfiguration.class})
public class FoodApplication {
    public static void main(String[] args) {
        SpringApplication.run(FoodApplication.class, args);
    }
}

2. 配置MySQL数据库(正式开发方案)

在src/main/resources/application.properties中添加MySQL连接信息:

# 替换为你的数据库名称、用户名、密码
spring.datasource.url=jdbc:mysql://localhost:3306/your_db_name?useSSL=false&serverTimezone=UTC&allowPublicKeyRetrieval=true
spring.datasource.username=your_mysql_user
spring.datasource.password=your_mysql_pwd
spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
# JPA配置可选
spring.jpa.hibernate.ddl-auto=update
spring.jpa.show-sql=true
  • 确保本地MySQL服务处于启动状态,端口3306正常可用
  • 现有项目需检查配置文件是否被修改,或环境变量中SPRING_DATASOURCE_URL这类配置是否失效

3. 使用嵌入式数据库(快速测试)

如果只是想快速启动项目验证功能,可以引入H2嵌入式数据库依赖到pom.xml:

<dependency>
    <groupId>com.h2database</groupId>
    <artifactId>h2</artifactId>
    <scope>runtime</scope>
</dependency>

添加后Spring Boot会自动配置H2内存数据库,启动后可通过http://localhost:8080/h2-console访问控制台(默认JDBC URL为jdbc:h2:mem:testdb)。

4. 环境变量检查

  • 检查系统环境变量中是否存在数据库相关配置(如SPRING_DATASOURCE_URL、SPRING_DATASOURCE_USERNAME),确认变量值是否正确
  • 验证JAVA_HOME环境变量指向的JDK版本是否为17+(Spring Boot 3.1.0要求JDK17及以上,你的日志显示使用17.0.3,版本符合要求)

内容的提问来源于stack exchange,提问作者shaneeqa

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.18 18:14:58