Springboot 项目失败 运行。 SQL 日志中的 BeanCreationException 错误

Springboot project failing to run. SQL BeanCreationException error in logs

我正在尝试使用 springboot 制作 REST api。通常我会使用 PHPMyAdmin 作为数据库,但第一次尝试 H2。

我没有在我的旧项目中看到这些问题,因为我使用 PHPMyAdmin 在项目外创建了 SQL 数据库。

任何帮助将不胜感激。

这是 Maven 输出 Maven 输出

org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'dataSourceScriptDatabaseInitializer' defined in class path resource [org/springframework/boot/autoconfigure/sql/init/DataSourceInitializationConfiguration.class]: Invocation of init method failed; nested exception is org.springframework.jdbc.datasource.init.ScriptStatementFailedException: Failed to execute SQL script statement #2 of URL [file:/C:/Users/Brendan%20Ahern/Desktop/takehomeproject/api_java_project/api_java_project/target/classes/schema.sql]: CREATE TABLE accounts ( accountnumber int(9) NOT NULL, pin int(4) NOT NULL, balance int(11) NOT NULL, overdraft int(11) NOT NULL ) ENGINE=InnoDB DEFAULT CHARSET=latin1; nested exception is org.h2.jdbc.JdbcSQLSyntaxErrorException: Syntax error in SQL statement "CREATE TABLE ACCOUNTS ( ACCOUNTNUMBER INT(9) NOT NULL, PIN INT(4) NOT NULL, BALANCE INT(11) NOT NULL, OVERDRAFT INT(11) NOT NULL ) ENGINE=[*]INNODB DEFAULT CHARSET=LATIN1"; expected "identifier"; SQL statement:
CREATE TABLE accounts ( accountnumber int(9) NOT NULL, pin int(4) NOT NULL, balance int(11) NOT NULL, overdraft int(11) NOT NULL ) ENGINE=InnoDB DEFAULT CHARSET=latin1 [42001-200]
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1804) ~[spring-beans-5.3.18.jar:5.3.18]
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:620) ~[spring-beans-5.3.18.jar:5.3.18]
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:542) ~[spring-beans-5.3.18.jar:5.3.18]
    at org.springframework.beans.factory.support.AbstractBeanFactory.lambda$doGetBean[=12=](AbstractBeanFactory.java:335) ~[spring-beans-5.3.18.jar:5.3.18]
    at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:234) ~[spring-beans-5.3.18.jar:5.3.18]
    at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:333) ~[spring-beans-5.3.18.jar:5.3.18]
    at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:208) ~[spring-beans-5.3.18.jar:5.3.18]
    at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:322) ~[spring-beans-5.3.18.jar:5.3.18]
    at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:208) ~[spring-beans-5.3.18.jar:5.3.18]
    at org.springframework.context.support.AbstractApplicationContext.getBean(AbstractApplicationContext.java:1154) ~[spring-context-5.3.18.jar:5.3.18]
    at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:908) ~[spring-context-5.3.18.jar:5.3.18]
    at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:583) ~[spring-context-5.3.18.jar:5.3.18]
    at org.springframework.boot.web.servlet.context.ServletWebServerApplicationContext.refresh(ServletWebServerApplicationContext.java:145) ~[spring-boot-2.6.6.jar:2.6.6]
    at org.springframework.boot.SpringApplication.refresh(SpringApplication.java:740) ~[spring-boot-2.6.6.jar:2.6.6]
    at org.springframework.boot.SpringApplication.refreshContext(SpringApplication.java:415) ~[spring-boot-2.6.6.jar:2.6.6]
    at org.springframework.boot.SpringApplication.run(SpringApplication.java:303) ~[spring-boot-2.6.6.jar:2.6.6]
    at org.springframework.boot.SpringApplication.run(SpringApplication.java:1312) ~[spring-boot-2.6.6.jar:2.6.6]
    at org.springframework.boot.SpringApplication.run(SpringApplication.java:1301) ~[spring-boot-2.6.6.jar:2.6.6]
    at com.example.api_java_project.ApiJavaProjectApplication.main(ApiJavaProjectApplication.java:15) ~[classes/:na]

这是我的schema.sql

DROP TABLE IF EXISTS accounts;

CREATE TABLE accounts (
  accountnumber int(9) NOT NULL,
  pin int(4) NOT NULL,
  balance int(11) NOT NULL,
  overdraft int(11) NOT NULL
) ENGINE=InnoDB DEFAULT CHARSET=latin1;

DROP TABLE IF EXISTS atm;

CREATE TABLE atm (
  note_value String NOT NULL,
  note_amount int(11) NOT NULL


) ENGINE=InnoDB DEFAULT CHARSET=latin1;

这是我的帐户对象class

@Entity
public class Account {

    @Id
    private int accountNumber;
    private int pin;
    private double balance;
    private double overdraft;

    public Account() {

    }

    public Account(int accountNumber, int pin, double balance, double overdraft) {
        super();
        this.accountNumber = accountNumber;
        this.pin = pin;
        this.balance = balance;
        this.overdraft = overdraft;
    }

    public Integer getAccountNumber() {
        return accountNumber;
    }
    public void setAccountNumber(Integer accountNumber) {
        this.accountNumber = accountNumber;
    }
    public int getPin() {
        return pin;
    }
    public void setPin(int pin) {
        this.pin = pin;
    }
    public Double getBalance() {
        return balance;
    }
    public void setBalance(Double balance) {
        this.balance = balance;
    }
    public Double getOverdraft() {
        return overdraft;
    }
    public void setOverdraft(Double overdraft) {
        this.overdraft = overdraft;
    }
}

错误消息实际上告诉您错误的原因。

expected "identifier";

您必须为要创建的表指定一个 PRIMARY KEY 值。

此外,如果您不想手动分配 PRIMARY KEY 值,请不要忘记添加自动分配的 AUTO_INCREMENT 定义。

[*] exceptions from H2 中的标记表示解析器错误的位置:

… ) ENGINE=[*]INNODB …

你这里有两个问题。

  1. 你不能在 H2 中使用 ENGINE=InnoDB 等,除非 MySQL 或启用了 MariaDB 兼容模式,你需要附加相关标志(;MODE=MySQL 并且可能其他)到 JDBC URL: https://h2database.com/html/features.html#compatibility 您还需要将 H2 升级到更新的版本,因为旧的不受支持的 H2 1.4.200 在这里限制更多,无法将 latin1 解析为字符集名称(或者您可以将其替换为 UTF8)。

  2. H2中没有STRING这样的数据类型,在真实的MySQL中也是如此,你需要使用一些有效的数据类型,例如VARCHAR(100).