首页 > 解决方案 > 在 Spring Boot 应用程序中获取 MySQL 语法错误

问题描述

我正在制作一个简单的应用程序,将 Spring Boot 与 Gradle 一起使用,并尝试将其连接到我计算机上的 MySQL。当我运行应用程序时,我收到以下错误:

java.sql.SQLSyntaxErrorException:您的 SQL 语法有错误;检查与您的 MySQL 服务器版本相对应的手册,以在第 1 行的 'load (id integer not null, customer varchar(255), date datetime not null,destin' 附近使用正确的语法

现在我只有一个类,Load,这就是它的样子:

@Entity
public class Load {
    public Load(){}
    @Id
    @GeneratedValue
    private int id;

    @NotBlank(message = "Customer field must not be blank")
    @NotNull
    @Size(max = 100, message = "Customer name too long!")
    private String customer;

    @NotBlank(message = "Destination field must not be blank")
    @NotNull
    @Size(max = 100, message = "Destination name too long!")
    private String destination;

    @NotBlank(message = "Driver field must not be blank")
    @NotNull
    @Size(max = 50, message = "Driver name too long!")
    private String driver;


    @NotNull
    private Date date;

    public Load(String customer, String destination, String driver, Date date) {
        this.customer = customer;
        this.destination = destination;
        this.driver = driver;
        this.date = date;
    }

我不太确定我在这堂课中创建的问题出在哪里

标签: javamysqlspring-bootgradlepersistence

解决方案


您正在尝试使用 MySQL 保留关键字Load作为表名,这就是问题所在。

但是您也可以在使用反引号字符时使用保留关键字。

@Entity
@javax.persistence.Table(name = "`load`")
public class Load{
   ...
}

推荐阅读