2015-03-08 121 views
3

我想在這個tutorial的Spring Boot項目中使用Spring Data JPA。這些都是我pom.xml依賴關係:爲什麼在Spring Boot中使用Spring Data JPA時,我的數據庫定製無法應用?

<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.datasource.driverClassName屬性爲空

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."); 
} 

春天拋出這個錯誤。因此,要解決此錯誤,請確保application.properties位於類路徑中。

0

我遇到了同樣的錯誤,我的問題是,application.properties沒有打包在 jar中,我沒有在啓動時提供它。

如果您正在使用java -jar you-jar-name.jar啓動,請確保application.properties可用。每文檔:

SpringApplication將負載從application.properties文件的屬性在以下位置,並將它們添加到春節環境:

一個當前目錄的/ config中子目錄。 當前目錄 甲類路徑/配置包 類路徑根

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

相關問題