java - 无法从 SpringBoot 连接到 MySQL 数据库
问题描述
我正在尝试从我的 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
从我的文件中添加代码片段
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>
应用程序属性
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 应该这样做
解决方案
它可能与坏的有关@annotations
。发布您的实体类以重新检查。无论如何在您的属性文件中添加以下行:
spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
推荐阅读
- javascript - 在 for 循环中创建多个元素
- php - 如何在页面 url php 中获取包含空格和字符的所有变量
- python - 'NoneType' 对象不可迭代用于搭配功能
- python-3.x - 将整数转换为字符串
- kubernetes - timeoutSeconds 在 kubernetes liveness/readiness 探测中的作用是什么?
- javascript - axios获取请求时得到415(不支持的媒体类型)
- android - 根据从 Android Sharesheet 中选择的应用程序将特定文本共享到另一个应用程序
- javascript - 刷新时如何在url中添加相同的参数
- gitlab - 合并到特定分支时触发管道
- python-3.x - 代码中 %i 的含义是什么 **frames=[PhotoImage(file="chatgif.gif",format="gif -index %i" %(i)) for i in range(20)]**