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

Java 如何将数据从一个表导入到另一个表-SpringBoot JPA/MySQL,java,mysql,sql,spring,spring-boot,Java,Mysql,Sql,Spring,Spring Boot,我最近创建了一个车辆管理系统 该系统源于MySQL数据库和spring的服务器端 我想创建另一个表(在运行时自动),该表只显示现有表中的两列 问题是我做错了什么? 最终目标-添加/删除/编辑车辆时,两个表将同步工作且不会发生碰撞 我很高兴能得到你的帮助 下面是“汽车”课程 CarType类只需创建另一个具有相关列(car\u id和car\u type)的mysql表 而不是在服务器端检查和管理流。只需创建复制的审计表,并在原始表上创建3个触发器,如MySQL级别的“插入后、更新后和删除后触发

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

问题是我做错了什么? 最终目标-添加/删除/编辑车辆时,两个表将同步工作且不会发生碰撞 我很高兴能得到你的帮助

下面是“汽车”课程

CarType类只需创建另一个具有相关列(car\u id和car\u type)的mysql表

  • 而不是在服务器端检查和管理流。只需创建复制的审计表,并在原始表上创建3个触发器,如MySQL级别的“插入后、更新后和删除后触发器”
了解更多有关触发访问的信息

  • 正如@scaisEdge所说,还可以从表中创建一个视图,并在spring项目中实现它
car\u detailed\u view.sql[view]

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`)))
@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
}

CarDetailedView.java[视图类]

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`)))
@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
}


似乎你只需要查看它的意思是“查看”"? 你能再单独一点吗@SCAISedgin db as mysql您可以创建一个视图,而无需复制表,只需创建一个公开所需列的查询即可。。但是‘我不在春天,我不能告诉你春天怎么做……你想做什么还不清楚。为什么在同一数据库中有两个具有相同值的表?您的CarType实体有两个Car类型的属性,一个是carId,另一个是CarType。这不是圆形的吗?如果你有两个类别A和B,并且你想在这两个类别上都进行操作,你可以简单地把它放在一个事务中。类似于-在A中插入实体A-在B中插入实体B。仅此而已。使用扩展JpaRepository的回购接口,你可以轻松地实现你的需求。
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
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`)))
@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
}