gpt4 book ai didi

mysql - 在 Spring Boot 中动态向 MySQL 添加新列

转载 作者:行者123 更新时间:2023-11-29 16:40:41 24 4
gpt4 key购买 nike

假设我有一个保存用户信息的类

用户.java

@Data
@Entity
public class User {

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

//User Data

@Column(name = "first_name", nullable = false)
private String firstName;

@Column(name = "last_name", nullable = false)
private String lastName;
}

在前端页面中,管理员可以通过定义新字段来动态更新 Bio。假设点击 + 按钮,他可以添加名为中间名、年龄或地址等的新字段。

P.S. It is kind of admin privilege and number of updates runtime will be limited and hence no issue of creating unlimited fields.

如何使用 Spring Boot 解决 MySQL 中动态添加实体的问题?

最佳答案

您可以使用自定义 map 添加新字段,例如:

    @ElementCollection(fetch = FetchType.LAZY)
@CollectionTable(name = "custom_fields")
@MapKeyColumn(name = "field")
private Map<String, String> customFields;

关于mysql - 在 Spring Boot 中动态向 MySQL 添加新列,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53358416/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com