2016-07-28 181 views
3

我嘗試使用spring-boot-starter-data-jpa連接到MySQL和this example冬眠,但得到春天引導數據JPA無法連接到MySQL數據庫

...
2016年7月28日13:20:49.021錯誤7765 --- [主] osboot.SpringApplication:應用程序啓動失敗 org.springframework.beans.factory.BeanCreationException:錯誤 與名 'org.springframework.boot.autoconfigure.orm.jpa.HibernateJpaAutoConfiguration' 創建豆: 注入自動裝配依賴失敗;嵌套異常是 org.springframework.beans.factory.BeanCreationException:不能 autowire field:private javax.sql.DataSource org.springframework.boot.autoconfigure.orm.jpa.JpaBaseConfiguration.dataSource; 嵌套的例外是 org.springframework.beans.factory.BeanCreationException:錯誤 建立在類路徑資源 [組織/ springframework的的/ boot /自動配置/ JDBC/DataSourceAutoConfiguration $ NonEmbeddedConfiguration.class]定義的名稱 '數據源' 豆: 豆通過工廠方法實例化失敗;嵌套的異常是 org.springframework.beans.BeanInstantiationException:無法 實例化[javax.sql.DataSource中]:出廠方法 '的dataSource' 投擲 除外;嵌套的例外是 org.springframework.boot.autoconfigure.jdbc.DataSourceProperties $ DataSourceBeanCreationException: 無法確定數據庫類型 無嵌入式數據庫驅動程序類。如果你想要一個嵌入式數據庫,請在 的類路徑中加一個支持的數據庫。如果你必須從你可能需要主動它 特定配置文件加載數據庫設置(配置文件沒有 當前活動)

...

application.properties:

# DataSource settings: set here your own configurations for the database 
# connection. In this example we have "netgloo_blog" as database name and 
# "root" as username and password. 
spring.datasource.url = jdbc:mysql://localhost:3306/db 
spring.datasource.username = db 
spring.datasource.password = pass 

spring.datasource.driver-class-name=com.mysql.jdbc.Driver 

# Keep the connection alive if idle for a long time (needed in production) 
spring.datasource.testWhileIdle = true 
spring.datasource.validationQuery = SELECT 1 

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

# Hibernate ddl auto (create, create-drop, update) 
spring.jpa.hibernate.ddl-auto = update 

# Naming strategy 
spring.jpa.hibernate.naming-strategy = org.hibernate.cfg.ImprovedNamingStrategy 

# Use spring.jpa.properties.* for Hibernate native properties (the prefix is 
# stripped before adding them to the entity manager) 

# The SQL dialect makes Hibernate generate better SQL for the chosen database 
spring.jpa.properties.hibernate.dialect = org.hibernate.dialect.MySQL5Dialect 

eclipse中的項目結構:

enter image description here

的build.gradle:

buildscript { 
    repositories { 
     mavenCentral() 
    } 
    dependencies { 
     classpath("org.springframework.boot:spring-boot-gradle- plugin:1.3.6.RELEASE") 
    } 
} 

apply plugin: 'java' 
apply plugin: 'eclipse' 
apply plugin: 'idea' 
apply plugin: 'spring-boot' 

jar { 
    baseName = 'gs-accessing-data-jpa' 
    version = '0.1.0' 
} 

repositories { 
    mavenCentral() 
    maven { url "https://repository.jboss.org/nexus/content/repositories/releases" } 
} 

sourceCompatibility = 1.8 
targetCompatibility = 1.8 

dependencies { 
    compile("org.springframework.boot:spring-boot-starter-data-jpa") 
    compile group: 'org.springframework.boot', name: 'spring-boot-starter-web', version: '1.3.6.RELEASE' 
    compile group: 'mysql', name: 'mysql-connector-java', version: '6.0.3' 

    //also tried 

    runtime group: 'mysql', name: 'mysql-connector-java', version: '6.0.3' 
    runtime "org.apache.tomcat:tomcat-jdbc:7.0.47" 

    testCompile("junit:junit") 
} 

task wrapper(type: Wrapper) { 
    gradleVersion = '2.3' 
} 
+0

我剛剛在Intellij中創建了一個具有相同配置並按預期工作的項目。你可以手動啓動'./gradlew bootRun'並看看會發生什麼? – Nonika

+0

不應該需要潛水員級別,因爲spring-boot會自動檢測來自'spring.datasource.url'; 'dependencies {「org.springframework.boot:spring-boot-starter-data-jpa」) 編譯組:'org.springframework.boot',名稱:'spring-boot-starter-web',版本:'1.3.6.RELEASE' 編譯組:'mysql',名稱:'mysql-connector-java',版本:'6.0.3' testCompile(「junit:junit」) }' – Nonika

回答

1

您沒有添加的驅動程序類

spring.datasource.driver類名= com.mysql.jdbc.Driver

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

檢查這些依賴以及

0

您可以添加gradle這個運行時的依賴性,而不是在編譯MySQL驅動時間依賴性罐子?

dependencies { 
    //compile "mysql:mysql-connector-java:6.0.3" 
    runtime "mysql:mysql-connector-java:6.0.3" 
    runtime "org.apache.tomcat:tomcat-jdbc:7.0.47" 
} 
+0

我通過您的建議更改了build.gradle文件,但異常仍然存在。查看帖子。 –