为啥在将 Spring Data JPA 与 Spring Boot 结合使用时,我的数据库自定义没有得到应用?
Posted
技术标签:
【中文标题】为啥在将 Spring Data JPA 与 Spring Boot 结合使用时,我的数据库自定义没有得到应用?【英文标题】:Why do my database customizations not get applied when using Spring Data JPA with Spring Boot?为什么在将 Spring Data JPA 与 Spring Boot 结合使用时,我的数据库自定义没有得到应用? 【发布时间】:2015-05-09 22:19:38 【问题描述】:我正在尝试在 tutorial 的 Spring Boot 项目中使用 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
数据源配置有问题吗?
【问题讨论】:
【参考方案1】: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.datasource.driverClassName
属性为空时,Spring 会抛出此错误。因此,要修复此错误,请确保 application.properties
在类路径中。
【讨论】:
【参考方案2】:我遇到了同样的错误,我的问题是 application.properties 没有打包在 jar 并且我没有在启动时提供它。
如果您使用 java -jar you-jar-name.jar 启动,请确保 application.properties 可用。根据文档:
SpringApplication 将从以下位置的 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
【讨论】:
以上是关于为啥在将 Spring Data JPA 与 Spring Boot 结合使用时,我的数据库自定义没有得到应用?的主要内容,如果未能解决你的问题,请参考以下文章
spring-data-jpa循环保存数据,为啥只保存了最后一条数据
Spring data jpa repo,为啥需要接口服务和服务实现
为啥手动定义的 Spring Data JPA 删除查询不会触发级联?