【问题标题】:Dynamically add new column to MySQL in Spring Boot在 Spring Boot 中动态向 MySQL 添加新列
【发布时间】:2018-11-18 06:21:50
【问题描述】:

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

用户.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;
}

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

附:这是一种管理员权限,更新运行时的数量将受到限制,因此不会出现创建无限字段的问题。

如何使用 Spring Boot 在 MySQL 中动态添加实体?

【问题讨论】:

    标签: mysql spring spring-boot jpa


    【解决方案1】:

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

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

    【讨论】:

      猜你喜欢
      • 2021-07-10
      • 1970-01-01
      • 2014-12-12
      • 2021-11-01
      • 2016-06-27
      • 2022-01-18
      • 1970-01-01
      • 1970-01-01
      • 2021-03-21
      相关资源
      最近更新 更多