Spring Boot无法读取yml属性文件

时间:2019-10-17 12:23:27

标签: spring-boot

我有一个Spring Boot应用程序的工作属性文件,但是每当将其转换为Yaml时,都会出现错误,提示未指定数据库URL。

我正在使用以下依赖项:

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>

,属性文件的配置如下(工作正常):

spring.datasource.url=jdbc:mysql://localhost:3306/db_name?useUnicode=true&useJDBCCompliantTimezoneShift=true&useLegacyDatetimeCode=false&serverTimezone=UTC
spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
spring.datasource.username=username
spring.datasource.password=password
spring.jpa.hibernate.ddl-auto=update
spring.jpa.generate-ddl=true
spring.jpa.show-sql=true

在Yaml中,我有:

 spring:
        datasource:
            url: "jdbc:mysql://localhost:3306/db_name?useUnicode=true&useJDBCCompliantTimezoneShift=true&useLegacyDatetimeCode=false&serverTimezone=UTC"
            driver-class-name: com.mysql.cj.jdbc.Driver
            username: "username"
            password: "password"
        jpa:
            hibernate:
                ddl-auto: update
            generate-ddl: true
            show-sql: true

我尝试使用带引号和不带引号的方法,但尝试使用driverClassName而不是driver-class-name,但是每次服务器尝试启动时,我收到以下消息:

***************************
APPLICATION FAILED TO START
***************************

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

2 个答案:

答案 0 :(得分:1)

我认为您必须从

中删除"
url: "jdbc:mysql://localhost:3306/db_name?useUnicode=true&useJDBCCompliantTimezoneShift=true&useLegacyDatetimeCode=false&serverTimezone=UTC"

并使其像

 url: jdbc:mysql://localhost:3306/db_name?useUnicode=true&useJDBCCompliantTimezoneShift=true&useLegacyDatetimeCode=false&serverTimezone=UTC

并跟随link可能会帮助

答案 1 :(得分:0)

您的pom.xml文件中是否声明了MySql驱动器?如果这样做,Spring Boot应该会自动识别您正在使用MySql并找出驱动程序类。

相关问题