java - Spring Boot项目看不到数据库连接的数据,写在application.yml中

标签 java mysql spring-boot spring-data-jpa yaml

我正在 IntelliJ IDEA Community IDE 中编写一个 Java 项目。本项目技术栈:

  • Spring 启动;
  • Spring 数据 JPA;
  • MySQL。

我在项目中有 application.yml 文件来连接数据库,但是当我运行我的应用程序时,出现错误:

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

我不知道为什么我的项目没有看到我的 application.yml 文件,该文件位于项目的 /src/main/resources/database 路径中.

有什么想法吗?

应用程序.yml:

spring:
  jpa:
    database: mysql
    show-sql: true
    hibernate:
      ddl-auto: update
  datasource:
    driver-class-name: com.mysql.cj.jdbc.Driver
    url: jdbc:mysql://localhost:3306/map
    username: root
    password: abcd

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 http://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>2.1.8.RELEASE</version>
        <relativePath/>
    </parent>

    <groupId>example.com</groupId>
    <artifactId>geo-informer</artifactId>
    <version>1.0</version>
    <name>Geo Informer</name>
    <description>
        The Spring Boot app, which makes interaction with the OpenStreetmap Nominatim API
        to work with geographic data and store it to the MySQL database
    </description>

    <properties>
        <java.version>1.8</java.version>
    </properties>

    <dependencies>
        <!-- Spring Boot -->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>

        <!-- Spring Data JPA -->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-data-jpa</artifactId>
        </dependency>

        <!-- MySQL Connector -->
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <scope>runtime</scope>
        </dependency>

        <!-- Lombok -->
        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
            <optional>true</optional>
        </dependency>
    </dependencies>

    <build>
        <plugins>
            <!-- Spring Boot Maven plugin -->
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>

</project>

这个项目位于这里:

https://github.com/OlegSandro/geo-informer/tree/dev

最佳答案

因为 application.yml 没有直接存储在 src/main/resources 中,所以它没有被加载。

在您的EntryPoint.java 文件中,您可以添加@PropertySource 注释并提供您的application.yml 的路径,或者您应该将文件直接存储在resources 文件夹下.

关于java - Spring Boot项目看不到数据库连接的数据,写在application.yml中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58183159/

相关文章:

java - 将 XML 实体映射到 Java 对象

java - Android ProgressDialog 不工作

Mysql表引用更新

postgresql - 为什么我的 Kubernetes 服务运行在不同的子网上?

spring - 如何在 spring boot 中使用 mockito 模拟 deleteById

javascript - 在 Springboot 中初始化 Graal 时出现 FileSystemNotFoundException

java - Eclipse 在项目资源管理器中未显示编译错误

java - 我无法将整数放入范围循环中

MySQL查询中带有varchar类型的MySQL order by子句

MySQL 查询优化 - 是否使用别名?