Blacol 2022-02-22 12:38 采纳率: 100%
浏览 489
已结题

SpringBoot+嵌入式h2 插入数据时报错,提示table not found

日志

### The error may exist in com/blacol/ncd/mapper/BaseInfoMapper.java (best guess)
### The error may involve com.blacol.ncd.mapper.BaseInfoMapper.insert
### The error occurred while executing an update
### SQL: INSERT INTO PUBLIC.BASEINFO  ( id, name, foreignName, gender, height, weight, country, age, nation, education, bmi, iq, eq, energy, worldView, createTime )  VALUES  ( ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ? )
### Cause: org.h2.jdbc.JdbcSQLSyntaxErrorException: Table "BASEINFO" not found; SQL statement:
INSERT INTO PUBLIC.BASEINFO  ( id,
name,
foreignName,
gender,
height,
weight,
country,
age,
nation,
education,
bmi,
iq,
eq,
energy,
worldView,
createTime )  VALUES  ( ?,
?,
?,
?,
?,
?,
?,
?,
?,
?,
?,
?,
?,
?,
?,
? ) [42102-200]

表结构

create table BASEINFO
(
    ID          CHARACTER not null,
    NAME        CHARACTER,
    FOREIGNNAME CHARACTER,
    HEIGHT      DOUBLE PRECISION,
    WEIGHT      DOUBLE PRECISION,
    COUNTRY     CHARACTER,
    AGE         INTEGER,
    NATION      CHARACTER,
    EDUCATION   CHARACTER,
    BMI         DOUBLE PRECISION,
    IQ          INTEGER,
    EQ          INTEGER,
    ENERGY      INTEGER,
    WORLDVIEW   CHARACTER,
    CREATETIME  DATE,
    GENDER      CHARACTER(1),
    constraint BASEINFO_PK
        primary key (ID)
);

Spring Boot配置文件

spring.datasource.url=jdbc:h2:~/db/ncd.mv.db;DB_CLOSE_DELAY=-1 # 有无DB_CLOSE_DELAY配置都一样
spring.web.resources.static-locations=classpath:/static/

实体类

package com.blacol.ncd.entity;

import com.baomidou.mybatisplus.annotation.TableField;
import com.baomidou.mybatisplus.annotation.TableName;

import java.sql.Date;
@TableName("PUBLIC.BASEINFO")
//改成BASEINFO、baseinfo、public.baseinfo、@TableName(schema="PUBLIC",value="BASEINFO")均无效
public class BaseInfo {
    private String id;
    private String name;
    @TableField("foreignName")
    private String foreignName;
    private String gender;
    private double height;
    private double weight;
    private String country;
    private int age;
    private String nation;
    private String education;
    private double bmi;
    private int iq;
    private int eq;
    private int energy;
    @TableField("worldView")
    private String worldView;
    @TableField("createTime")
    private Date createTime;

    public String getId() {
        return id;
    }

    public void setId(String id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getForeignName() {
        return foreignName;
    }

    public void setForeignName(String foreignName) {
        this.foreignName = foreignName;
    }

    public double getHeight() {
        return height;
    }

    public void setHeight(double height) {
        this.height = height;
    }

    public double getWeight() {
        return weight;
    }

    public void setWeight(double weight) {
        this.weight = weight;
    }

    public String getCountry() {
        return country;
    }

    public void setCountry(String country) {
        this.country = country;
    }

    public int getAge() {
        return age;
    }

    public void setAge(int age) {
        this.age = age;
    }

    public String getNation() {
        return nation;
    }

    public void setNation(String nation) {
        this.nation = nation;
    }

    public String getEducation() {
        return education;
    }

    public void setEducation(String education) {
        this.education = education;
    }

    public double getBmi() {
        return bmi;
    }

    public void setBmi(double bmi) {
        this.bmi = bmi;
    }

    public int getIq() {
        return iq;
    }

    public void setIq(int iq) {
        this.iq = iq;
    }

    public int getEq() {
        return eq;
    }

    public void setEq(int eq) {
        this.eq = eq;
    }

    public int getEnergy() {
        return energy;
    }

    public void setEnergy(int energy) {
        this.energy = energy;
    }

    public String getWorldView() {
        return worldView;
    }

    public void setWorldView(String worldView) {
        this.worldView = worldView;
    }

    public Date getCreateTime() {
        return createTime;
    }

    public void setCreateTime(Date createTime) {
        this.createTime = createTime;
    }

    public String getGender() {
        return gender;
    }

    public void setGender(String gender) {
        this.gender = gender;
    }

    @Override
    public String toString() {
        return "BaseInfo{" +
                "id='" + id + '\'' +
                ", name='" + name + '\'' +
                ", foreignName='" + foreignName + '\'' +
                ", gender='" + gender + '\'' +
                ", height=" + height +
                ", weight=" + weight +
                ", country='" + country + '\'' +
                ", age=" + age +
                ", nation='" + nation + '\'' +
                ", education='" + education + '\'' +
                ", bmi=" + bmi +
                ", iq=" + iq +
                ", eq=" + eq +
                ", energy=" + energy +
                ", worldView='" + worldView + '\'' +
                ", createTime=" + createTime +
                '}';
    }
}

插入的数据

通过Mybatis-plus插入,插入实体类BaseInfo的对象,为了更直观的展示数据使用JSON表示:

{
    "age":26,
    "bmi":23.62,
    "country":"sd",
    "createTime":1645504184362,
    "education":"bbbb",
    "energy":6,
    "eq":6,
    "foreignName":"sda",
    "gender":"m",
    "height":177,
    "id":"cc067f728381496",
    "iq":6,
    "name":"adas",
    "nation":"dd",
    "weight":74,
    "worldView":"ggg"
}
  • 写回答

2条回答 默认 最新

  • Blacol 2022-02-25 10:57
    关注

    已解决:
    主要是因为idea项目结构的问题。
    SpringBoot项目打包后会生成 target文件夹, h2的数据库文件也打包了进去。这样程序运行时是访问target里的数据库。如果要访问项目中的数据库文件可以将配置中url的~改成.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

问题事件

  • 已结题 (查看结题原因) 2月25日
  • 已采纳回答 2月25日
  • 创建了问题 2月22日

悬赏问题

  • ¥15 java 操作 elasticsearch 8.1 实现 索引的重建
  • ¥15 数据可视化Python
  • ¥15 要给毕业设计添加扫码登录的功能!!有偿
  • ¥15 kafka 分区副本增加会导致消息丢失或者不可用吗?
  • ¥15 微信公众号自制会员卡没有收款渠道啊
  • ¥15 stable diffusion
  • ¥100 Jenkins自动化部署—悬赏100元
  • ¥15 关于#python#的问题:求帮写python代码
  • ¥20 MATLAB画图图形出现上下震荡的线条
  • ¥15 关于#windows#的问题:怎么用WIN 11系统的电脑 克隆WIN NT3.51-4.0系统的硬盘