首页 > 解决方案 > 在 Spring starter 项目中是否必须配置 DataSource?

问题描述

我想创建一个基本的 Spring 启动应用程序并发出获取请求。我已经添加了 Spring JPA、Spring Web 依赖项并开始在端口 8080 中运行,但我的应用程序要启动。它说“说明:

无法配置数据源:未指定“url”属性,并且无法配置嵌入式数据源。原因:无法确定合适的驱动程序类“。

我没有添加任何数据库依赖项,因为我不需要它,在我的项目中是否必须具有数据库依赖项?这是我的 pom.xml

<?xml version="1.0" encoding="UTF-8"?>

4.0.0 org.springframework.boot spring-boot-starter-parent 2.3.1.RELEASE com.example java-sample-project 0.0.1-SNAPSHOT java-sample-project Spring Boot的演示项目

<properties>
    <java.version>1.8</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>org.springframework.boot</groupId>
        <artifactId>spring-boot-devtools</artifactId>
        <scope>runtime</scope>
        <optional>true</optional>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-test</artifactId>
        <scope>test</scope>
        <exclusions>
            <exclusion>
                <groupId>org.junit.vintage</groupId>
                <artifactId>junit-vintage-engine</artifactId>
            </exclusion>
        </exclusions>
    </dependency>
</dependencies>

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

标签: javamysqlspringspring-bootweb

解决方案


是的,这是强制性的。但是,如果您不想运行真正的数据库,请使用内存数据库 H2。只需将 com.h2database:h2 添加到依赖项即可。


推荐阅读