Spring Boot Data JPA无法连接到MySQL数据库。

3
我试着使用spring-boot-starter-data-jpa和Hibernate连接MySQL,参考了这个例子,但是出现以下错误:

...
2016-07-28 13:20:49.021 ERROR 7765 --- [main] o.s.boot.SpringApplication : 应用程序启动失败 org.springframework.beans.factory.BeanCreationException: 创建bean "org.springframework.boot.autoconfigure.orm.jpa.HibernateJpaAutoConfiguration"时出错,注入的自动装配依赖项失败。嵌套异常信息为:org.springframework.beans.factory.BeanCreationException: 注入字段javax.sql.DataSource org.springframework.boot.autoconfigure.orm.jpa.JpaBaseConfiguration.dataSource失败,嵌套异常信息为:org.springframework.beans.factory.BeanCreationException: 创建bean 'dataSource'时出错,定义在类路径资源[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。如果您想使用嵌入式数据库,请将支持的嵌入式数据库放在类路径上。如果您有要从特定配置文件加载的数据库设置,请激活该配置文件(目前没有配置文件处于激活状态)

...

应用程序属性:

    # 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中的项目结构:

在此输入图片描述

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

我刚刚在Intellij中创建了一个具有相同配置的项目,并且按预期工作。你能否手动启动 ./gradlew bootRun 并查看发生了什么? - Nonika
依赖项 { 编译("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' 测试编译("junit:junit") }不需要使用diver-class,因为spring-boot会自动从spring.datasource.url中检测到它。 - Nonika
3个回答

3

您没有添加驱动程序类

spring.datasource.driver-class-name=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 Driver 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"
}

我按照你的建议更改了build.gradle文件,但异常仍然存在。请参见帖子。 - Edgaras Karka

-1

是的,我有mysql驱动程序,请查看build.gradle文件。我在application.properties文件中添加了spring.datasource.driver-class-name=com.mysql.jdbc.Driver这一行。 - Edgaras Karka

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接