简体   繁体   English

无法从 SpringBoot 连接到 MySQL 数据库

[英]Unable to connect to MySQL database from SpringBoot

I am trying to connect to the mySQL database from my Spring Boot application.我正在尝试从 Spring Boot 应用程序连接到 mySQL 数据库。 However it is showing error when I am trying to run it.但是,当我尝试运行它时它显示错误。

How can I resolve this?我该如何解决这个问题?

The error错误

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

Adding code snippets from my files从我的文件中添加代码片段

pom.xml 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

stack trace堆栈跟踪

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 : 

I havent created the tables manually in sql as i thought spring.jpa.hibernate.ddl-auto=update should do it我还没有在 sql 中手动创建表,因为我认为 spring.jpa.hibernate.ddl-auto=update 应该这样做

It can be related to bad @annotations .它可能与糟糕的@annotations相关。 Post your entity class to re-check.发布您的实体类以重新检查。 Anyhow add the below line in your property file:无论如何,在您的属性文件中添加以下行:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM