如何将数据从一张表导入到另一张表 - SpringBoot JPA /MySQL

Posted

技术标签:

【中文标题】如何将数据从一张表导入到另一张表 - SpringBoot JPA /MySQL【英文标题】:How to import data from one table to another - SpringBoot JPA /MySQL 【发布时间】:2021-05-23 20:26:34 【问题描述】:

我最近创建了一个车辆管理系统 系统来源于spring mysql数据库和服务器端 我想创建另一个表(在运行时自动),它只显示现有表的 2 列。

问题是我做错了什么? 最终目标 - 添加/删除/编辑车辆时,两个表将同步工作且不会发生碰撞 我很乐意为您提供帮助

下面是“汽车”类

import javax.persistence.*;
import java.time.LocalDate;

@Entity
@Table(name = "car")
public class Car 
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private long carId;

    private String licensePlate;

    private int carType;

    private boolean suv;

    private int engineCapacity;

    private int year;

    private String note;

    private int status;

    private LocalDate careDate;

    private LocalDate editDate;



    public Car() 
    

    public Car(long carId) 
        this.carId = carId;
    

    public long getCarId() 
        return carId;
    

    public void setCarId(long carId) 
        this.carId = carId;
    

    public String getLicensePlate() 
        return licensePlate;
    

    public void setLicensePlate(String licensePlate) 
        this.licensePlate = licensePlate;
    

    public int getCarType() 
        return carType;
    

    public void setCarType(int carType) 
        this.carType = carType;
    

    public boolean isSuv() 
        return suv;
    

    public void setSuv(boolean SUV) 
        this.suv = SUV;
    

    public int getEngineCapacity() 
        return engineCapacity;
    

    public void setEngineCapacity(int engineCapacity) 
        this.engineCapacity = engineCapacity;
    

    public int getYear() 
        return year;
    

    public void setYear(int year) 
        this.year = year;
    

    public String getNote() 
        return note;
    

    public void setNote(String note) 
        this.note = note;
    

    public int getStatus() 
        return status;
    

    public void setStatus(int status) 
        this.status = status;
    

    public LocalDate getCareDate() 
        return careDate;
    

    public void setCareDate(LocalDate careDate) 
        this.careDate = careDate;
    

    public LocalDate getEditDate() 
        return editDate;
    


    public void setEditDate(LocalDate editDate) 
        this.editDate = editDate;
    

 

而 CarType 类只需要创建另一个具有相关列(car_id 和 car_type)的 MySQL 表

package com.example.CarSystemMatanElbaz.model;

import javax.persistence.*;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

@Entity
public class CarType 

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private long id;

    @OneToOne(cascade = CascadeType.ALL,fetch = FetchType.LAZY,orphanRemoval = true)
    @JoinColumn(name= "car_id")
    private Car carId;

    @OneToOne(cascade = CascadeType.ALL,fetch = FetchType.LAZY,orphanRemoval = true)
    @JoinColumn(name= "car_type")
    private Car carType;

    public CarType() 

    

    public CarType(long id, Car carId, Car carType) 
        this.id = id;
        this.carId = carId;
        this.carType = carType;
    

    public long getId() 
        return id;
    

    public void setId(long id) 
        this.id = id;
    

    public Car getCarId() 
        return carId;
    

    public void setCarId(Car carId) 
        this.carId = carId;
    

    public Car getCarType() 
        return carType;
    

    public void setCarType(Car carType) 
        this.carType = carType;
    

【问题讨论】:

看来你只需要查看 “视图”是什么意思?你能更具体吗? @scaisEdge 在 db as mysql 中,您可以创建一个不重复表的视图,而只是创建一个查询来公开您需要的列.. 但是'我不在春天,我不能告诉你如何在春天做这个.. 你想做什么还不清楚。为什么在同一个数据库中有两个具有相同值的表?您的 CarType 实体有两个 Car 类型的属性,一个是 carId,另一个是 carType。这不是圆形的吗?如果你有两个类 A 和 B 并且你想对两者都进行操作,你可以简单地将它放在一个事务中。比如 - 在 A 中插入实体 a - 在 B 中插入实体 b。就是这样。 【参考方案1】: 而不是在服务器端检查和管理流。只需创建复制审计表并在原始表上创建 3 个触发器,例如“在 MySQL 级别的插入后、更新后和删除后触发器。
CREATE  TRIGGER `db`.`car_AFTER_INSERT` AFTER INSERT ON `trn_student_misc_fees_req_status` FOR EACH ROW
BEGIN
  # INSERT Query of Another table using 'NEW' Keyword with car table fields. 
END
CREATE  TRIGGER `db`.`car_AFTER_UPDATE` AFTER UPDATE ON `trn_student_misc_fees_req_status` FOR EACH ROW
BEGIN
  # UPDATE Query of Another table using 'NEW' Keyword with car table fields. 
END
CREATE  TRIGGER `db`.`car_AFTER_DELETE` AFTER DELETE ON `trn_student_misc_fees_req_status` FOR EACH ROW
BEGIN
  # DELETE Query of Another table using 'OLD' Keyword with car table fields. 
END

阅读更多关于触发器的信息访问https://www.mysqltutorial.org/mysql-triggers.aspx

正如@scaisEdge 所说,还可以从表中创建一个视图并在您的 spring 项目中实现它。

car_detailed_view.sql [查看]

CREATE 
    ALGORITHM = UNDEFINED  
    SQL SECURITY DEFINER
VIEW `car_detailed_view` AS
    SELECT 
    car.carId,car.licensePlate,car.carType,car.suv,car.engineCapacity,car.year,car.note,car.status,car.careDate,car.editDate;
    FROM
        (`car`
        INNER JOIN `CarType`  ON ((`car`.`car_id` = `CarType`.`car_id`)))

CarDetailedView.java [视图类]

@Immutable
@Entity
@Table(name = "car_detailed_view")
public class CarDetailedView

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private long carId;

    private String licensePlate;

    private int carType;

    private boolean suv;

    private int engineCapacity;

    private int year;

    private String note;

    private int status;

    private LocalDate careDate;

    private LocalDate editDate;

   //getter,setter and constructor


【讨论】:

使用扩展 JpaRepository 的 repo 接口,您可以轻松满足您的需求。

以上是关于如何将数据从一张表导入到另一张表 - SpringBoot JPA /MySQL的主要内容,如果未能解决你的问题,请参考以下文章

Vlookup 公式将一些数据从一张表提取到另一张表(谷歌电子表格)

Excel VBA 将值从一张表移动到另一张表

如何在 OpenOffice 中将一列单元格从一张表插入到另一张表的单个单元格中?

sql语句 怎么从一张表中查询数据插入到另一张表中

sql语句 怎么从一张表中查询数据插入到另一张表中

SQL TRIGGER 更新时从一张表到另一张表