为什么在将 Spring Data JPA 与 Spring Boot 一起使用时,我的数据库定制没有得到应用?

Why do my database customizations not get applied when using Spring Data JPA with Spring Boot?

我正在尝试在此 tutorial 的 Spring 引导项目中使用 Spring Data JPA。这些是我的 pom.xml 依赖项:

<dependencies>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-test</artifactId>
        <scope>test</scope>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-web</artifactId>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-data-jpa</artifactId>
    </dependency>
    <dependency>
        <groupId>mysql</groupId>
        <artifactId>mysql-connector-java</artifactId>
        <scope>runtime</scope>
    </dependency>
</dependencies>

application.properties

# DataSource settings: set here configurations for the database connection
spring.datasource.url = jdbc:mysql://localhost/dao
spring.datasource.username = root
spring.datasource.driverClassName = com.mysql.jdbc.Driver
spring.datasource.password = 

# Specify the DBMS
spring.jpa.database = MYSQL

# Show or not log for each sql query
spring.jpa.show-sql = true

# Hibernate settings are prefixed with spring.jpa.hibernate.*
spring.jpa.hibernate.ddl-auto = update
spring.jpa.hibernate.dialect = org.hibernate.dialect.MySQL5Dialect
spring.jpa.hibernate.naming_strategy = org.hibernate.cfg.ImprovedNamingStrategy   

我得到这个错误:

 Caused by: org.springframework.beans.factory.BeanCreationException: Cannot         determine embedded database driver class for database type NONE. If you want an    embedded database please put a supported one on the classpath.
at org.springframework.boot.autoconfigure.jdbc.DataSourceProperties.getDriverClassName(DataSourceProperties.java:137)
at org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration$NonEmbeddedConfiguration.dataSource(DataSourceAutoConfiguration.java:117)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:606)
at org.springframework.beans.factory.support.SimpleInstantiationStrategy.instantiat   e(SimpleInstantiationStrategy.java:162)
... 40 more

数据源配置有问题吗?

DataSourceProperties.getDriverClassName() 方法中抛出错误。在下面找到来自 spring 发行版的相同源代码:

if (!StringUtils.hasText(driverClassName)) {
    throw new BeanCreationException(
        "Cannot determine embedded database driver class for database type "
            + this.embeddedDatabaseConnection
            + ". If you want an embedded "
            + "database please put a supported one on the classpath.");
}

Spring 在 spring.datasource.driverClassName 属性 为空时抛出此错误。因此,要修复此错误,请确保 application.properties 在类路径中。

我 运行 遇到同样的错误,我的问题是 application.properties 没有打包在 jar 并且我没有在启动时提供它。

如果您使用 java -jar you-jar-name.jar 启动,请确保 application.properties 可用。根据文档:

Spring应用程序将从位于以下位置的 application.properties 文件加载属性并将它们添加到 Spring 环境:

当前目录的/config 子目录。 当前目录 类路径 /config 包 类路径根

http://docs.spring.io/spring-boot/docs/current/reference/html/boot-features-external-config.html#boot-features-external-config-application-property-files