【问题标题】:Can't see MYSQL table information in MYSQL workbench, spring Boot在 MYSQL 工作台、spring Boot 中看不到 MYSQL 表信息
【发布时间】:2020-09-22 18:43:27
【问题描述】:

我正在尝试将我的 Spring Boot 应用程序连接到 MYSQL 工作台,但我在 MYSQL 工作台中找不到我的任何列。

我的设置: 这个 application.properties 文件

spring.datasource.url=jdbc:mysql://localhost:3306/blog?useUnicode=true&useLegacyDatetimeCode=false&serverTimezone=UTC
spring.datasource.username=root
spring.datasource.password=
spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
spring.jpa.hibernate.ddl-auto=create-drop
spring.jpa.show-sql=true

当我尝试不使用“?useUnicode=true&useLegacyDatetimeCode=false&serverTimezone=UTC”时,我收到以下错误

HHH000342: Could not obtain connection to query metadata : The server time zone value 'EDT' is unrecognized or represents more than one time zone. You must configure either the server or JDBC driver (via the 'serverTimezone' configuration property) to use a more specifc time zone value if you want to utilize time zone support.

我的 Maven 依赖项

<parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.3.0.RELEASE</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>
    <groupId>com.blogportfolio</groupId>
    <artifactId>blog</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>Bala's Blog</name>
    <description>Blog portfolio backend</description>

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

    <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>
        </dependency>
        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
            <optional>true</optional>
        </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>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>

文章类

package com.blogbackend.Model;

import com.sun.istack.NotNull;
import lombok.Getter;
import lombok.Setter;

import javax.persistence.*;
import java.util.Date;


@Entity
@Getter
@Setter
@Table (name = "Articles")
public class Article {

    @Id
    @GeneratedValue(strategy = GenerationType.SEQUENCE)
    private int id;

    @NotNull
    @Column(name="articleName")
    private String artName;

    @NotNull
    @Column(name="articleBody")
    private String artBody;

    @NotNull
    @Column(name="articleCategory")
    private String artCategory;

    @NotNull
    @Column(name="articleCreated")
    private Date artCreated;

    @Column
    private Author author;

    public Article(int id) {
        this.id = id;
    }
}

当我转到 WorkBench 时,博客架构为空。

【问题讨论】:

    标签: java mysql spring spring-boot hibernate


    【解决方案1】:

    Article 实体的身份生成策略是GenerationType.SEQUENCE。但是MySQL 不直接支持Sequence

    这就是 spring.jpa.hibernate.ddl-auto=create-drop 尝试创建架构时 if 失败的原因。

    使用AUTO_INCREMENT 代替GenerationType.IDENTITY

    【讨论】:

      【解决方案2】:

      据我所知,它与您要使用的架构/表无关,而更像是服务器配置问题。根据this,它带有特定版本的使用驱动程序,必须设置此属性。

      为了完成所需表的自动创建,this 可能会有所帮助。

      如果在应用程序运行时创建已经成功,只需将ddl-auto参数从

      spring.jpa.hibernate.ddl-auto=create-drop
      

      spring.jpa.hibernate.ddl-auto=create
      

      一旦创建更改为validateupdate

      更多解释可以在here找到。

      【讨论】:

      • 嗨,Ben,我用我的 Maven 依赖项更新了我的问题。我应该更改哪个并重试?感谢您的帮助。
      • 我认为您必须返回相当多的版本。我建议您尽可能坚持使用最新版本,并使用适合您设置的所需时区添加属性。
      • 啊,我想我刚收到你的问题。尝试将属性添加到 jdbc 连接字符串,启动您的应用程序,在它运行时您可以在工作台中检查它吗?
      • 如果它不起作用,你应该检查这个:stackoverflow.com/questions/26881739/…
      • 如果您看到预期的表格:将 spring.jpa.hibernate.ddl-auto=create-drop 更改为 spring.jpa.hibernate.ddl-auto=create 并在创建后更改为 validate 或更新
      猜你喜欢
      • 2019-06-06
      • 2017-03-07
      • 2018-02-07
      • 2013-10-23
      • 2013-01-19
      • 2021-12-15
      • 1970-01-01
      • 2013-01-03
      • 1970-01-01
      相关资源
      最近更新 更多