【问题标题】:Unable to connect to MySQL database from SpringBoot无法从 Spring Boot 连接到 MySQL 数据库
【发布时间】:2020-02-10 08:30:48
【问题描述】:

我正在尝试从我的 Spring Boot 应用程序连接到 mySQL 数据库。但是,当我尝试运行它时它显示错误。

我该如何解决这个问题?

错误

org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'entityManagerFactory' defined in class path resource [org/springframework/boot/autoconfigure/orm/jpa/HibernateJpaAutoConfiguration.class]: Invocation of init method failed; nested exception is javax.persistence.PersistenceException: [PersistenceUnit: default] Unable to build Hibernate SessionFactory

从我的文件中添加代码 sn-ps

pom.xml

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
  <modelVersion>4.0.0</modelVersion>
  <groupId>io.nkamanoo.springboot</groupId>
  <artifactId>course-api</artifactId>
  <version>0.0.1-SNAPSHOT</version>
  <name>Java Brains Course API</name>



<parent>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-parent</artifactId>
    <version>1.5.9.RELEASE</version>
</parent>

<dependencies>

    <dependency>
       <groupId> org.springframework.boot </groupId>
       <artifactId>spring-boot-starter-data-jpa</artifactId>
    </dependency>


    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-web</artifactId>
    </dependency>

        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <scope>runtime</scope>
            <version>8.0.19</version>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
            <exclusions>
                <exclusion>
                    <groupId>org.junit.vintage</groupId>
                    <artifactId>junit-vintage-engine</artifactId>
                </exclusion>
            </exclusions>
        </dependency>
</dependencies>


 <properties>
    <java.version>1.8</java.version>
 </properties>

 </project>

application.properties

spring.jpa.hibernate.ddl-auto=update
spring.datasource.url=jdbc:mysql://localhost:3306/db_example
spring.datasource.username=root
spring.datasource.password=root

spring.jpa.hibernate.use-new-id-generator-mappings=false
spring.jpa.properties.hibernate.dialect = org.hibernate.dialect.MySQL5Dialect

堆栈跟踪

2020-02-10 13:44:34.073  INFO 63618 --- [           main] org.hibernate.Version                    : HHH000412: Hibernate Core {5.0.12.Final}
2020-02-10 13:44:34.075  INFO 63618 --- [           main] org.hibernate.cfg.Environment            : HHH000206: hibernate.properties not found
2020-02-10 13:44:34.078  INFO 63618 --- [           main] org.hibernate.cfg.Environment            : HHH000021: Bytecode provider name : javassist
2020-02-10 13:44:34.139  INFO 63618 --- [           main] o.hibernate.annotations.common.Version   : HCANN000001: Hibernate Commons Annotations {5.0.1.Final}
2020-02-10 13:44:34.295  INFO 63618 --- [           main] org.hibernate.dialect.Dialect            : HHH000400: Using dialect: org.hibernate.dialect.MySQL5Dialect
2020-02-10 13:44:34.823  INFO 63618 --- [           main] org.hibernate.tool.hbm2ddl.SchemaUpdate  : HHH000228: Running hbm2ddl schema update
2020-02-10 13:44:34.864  INFO 63618 --- [           main] rmationExtractorJdbcDatabaseMetaDataImpl : HHH000262: Table not found: topic
2020-02-10 13:44:34.867  INFO 63618 --- [           main] rmationExtractorJdbcDatabaseMetaDataImpl : HHH000262: Table not found: topic
2020-02-10 13:44:34.886  WARN 63618 --- [           main] ationConfigEmbeddedWebApplicationContext : Exception encountered during context initialization - cancelling refresh attempt: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'entityManagerFactory' defined in class path resource [org/springframework/boot/autoconfigure/orm/jpa/HibernateJpaAutoConfiguration.class]: Invocation of init method failed; nested exception is javax.persistence.PersistenceException: [PersistenceUnit: default] Unable to build Hibernate SessionFactory
2020-02-10 13:44:34.892  INFO 63618 --- [           main] o.apache.catalina.core.StandardService   : Stopping service [Tomcat]
2020-02-10 13:44:34.910  INFO 63618 --- [           main] utoConfigurationReportLoggingInitializer : 

我没有在 sql 中手动创建表,因为我认为 spring.jpa.hibernate.ddl-auto=update 应该这样做

【问题讨论】:

  • 我认为你应该向我们展示整个堆栈跟踪,因为我认为根本原因在于堆栈跟踪的更深处。
  • 添加了结束堆栈跟踪
  • 你在哪里添加了堆栈跟踪?
  • 你可以尝试设置 spring.jpa.hibernate.ddl-auto 来创建,但我怀疑这是问题所在。当配置中有问题时,通常会出现此错误。尝试将日志级别设置为 DEBUG,也许这会提示问题所在。
  • 你可以看看this post。接受的答案给出了一些提示

标签: java mysql hibernate spring-boot jpa


【解决方案1】:

可能与坏@annotations有关。发布您的实体类以重新检查。 无论如何在您的属性文件中添加以下行:

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

【讨论】:

    猜你喜欢
    • 2020-09-03
    • 2018-10-09
    • 2020-01-11
    • 2019-10-19
    • 2019-02-26
    • 2016-12-02
    • 2020-07-07
    • 2019-04-12
    • 2018-01-06
    相关资源
    最近更新 更多