[英]spring boot data jpa can't connect to mysql database
我嘗試使用spring-boot-starter-data-jpa
和hibernate通過這個例子連接到mysql,但得到
...
2016-07-28 13:20:49.021 ERROR 7765 --- [main] osboot.SpringApplication:應用程序啟動失敗org.springframework.beans.factory.BeanCreationException:創建名為'org.springframework.boot.autoconfigure.orm的bean時出錯.jpa.HibernateJpaAutoConfiguration':自動連接依賴項的注入失敗; 嵌套異常是org.springframework.beans.factory.BeanCreationException:無法自動裝配字段:private javax.sql.DataSource org.springframework.boot.autoconfigure.orm.jpa.JpaBaseConfiguration.dataSource; 嵌套異常是org.springframework.beans.factory.BeanCreationException:在類路徑資源中定義名為'dataSource'的bean時出錯[org / springframework / boot / autoconfigure / jdbc / DataSourceAutoConfiguration $ NonEmbeddedConfiguration.class]:通過工廠方法進行Bean實例化失敗; 嵌套異常是org.springframework.beans.BeanInstantiationException:無法實例化[javax.sql.DataSource]:工廠方法'dataSource'拋出異常; 嵌套異常是org.springframework.boot.autoconfigure.jdbc.DataSourceProperties $ DataSourceBeanCreationException:無法確定數據庫類型為NONE的嵌入式數據庫驅動程序類。 如果你想要一個嵌入式數據庫,請在類路徑上放置一個受支持的數據庫。 如果您要從特定配置文件加載數據庫設置,則可能需要激活它(當前沒有配置文件處於活動狀態)...
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
日食中的項目結構:
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'
}
您沒有添加驅動程序類
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>
檢查這些依賴項
你可以在gradle中添加運行時依賴,而不是在MYSQL驅動程序jar上編譯時依賴嗎?
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"
}
你在類路徑上有數據庫驅動程序嗎? 你有設置屬性spring.datasource.driver-class-name ?
有關更多詳細信息,請參閱http://docs.spring.io/spring-boot/docs/current/reference/html/boot-features-sql.html
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.