Spring Boot项目看不到用application.yml编写的数据库连接数据

时间:2019-10-01 10:44:26

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

我正在IntelliJ IDEA社区IDE中编写一个Java项目。该项目的技术栈:

  • Spring Boot;
  • Spring Data 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路径中。

有什么想法吗?

application.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

2 个答案:

答案 0 :(得分:2)

由于application.yml未直接存储在src/main/resources中,因此不会加载。

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

答案 1 :(得分:1)

在构建项目时,application.yml或application.properties文件应位于您的类路径中。 Spring Boot设计了它的结构,使得当执行maven构建时,来自src / main / resources的属性文件与其他源文件一起放置在classes目录中。

因此,您需要直接将这些文件放置在src / main / resources中

如果您仍然希望将文件放在其他位置,则可以使用@PropertySource

指向该位置。