【问题标题】:spring boot data jpa can't connect to mysql databasespring boot data jpa无法连接mysql数据库
【发布时间】:2016-12-02 17:21:12
【问题描述】:

我尝试使用spring-boot-starter-data-jpa 连接到mysql,并通过this example 休眠,但是得到了

...
2016-07-28 13:20:49.021 错误 7765 --- [main] os.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:错误 创建在类路径资源中定义的名称为“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: 无法确定数据库类型的嵌入式数据库驱动程序类 没有。如果您想要一个嵌入式数据库,请放置一个受支持的数据库 类路径。如果您有要从 您可能需要激活它的特定配置文件(没有配置文件是 当前活跃)

...

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

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 看看会发生什么吗?
  • 应该不需要潜水员等级,因为spring-boot会自动从spring.datasource.url检测到; 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' testCompile("junit:junit") }

标签: java hibernate spring-boot


【解决方案1】:

你没有添加驱动类

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>

同时检查这些依赖项

【讨论】:

    【解决方案2】:

    你可以在 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 文件,但异常仍然存在。见帖子。
    【解决方案3】:

    类路径上有数据库驱动程序吗? 你设置了属性spring.datasource.driver-class-name吗?

    有关详细信息,请参阅 http://docs.spring.io/spring-boot/docs/current/reference/html/boot-features-sql.html

    【讨论】:

    • 是的,我有 mysql 驱动程序,请参阅 build.gradle。我在 application.properties 文件中添加了 spring.datasource.driver-class-name=com.mysql.jdbc.Driver 行。
    猜你喜欢
    • 2016-10-11
    • 2018-01-06
    • 2020-01-11
    • 2018-10-22
    • 1970-01-01
    • 2020-09-03
    • 2018-10-09
    • 2022-01-11
    • 2019-02-26
    相关资源
    最近更新 更多