该方式是针对枚举字段的映射,相当于是代码中的数据字典,在存储的时候是对应的序列,查询出数据时替换为具体的数据
@Entity @Table(name = "employee", schema = "public") @Data @Builder @AllArgsConstructor @NoArgsConstructor public class Employee { @Id @GeneratedValue @Column(name = "id") private Long id; @Column(name = "name") private String name; @Column(name = "sex") @Enumerated(EnumType.ORDINAL)//性别字段持久化为0,1 private Sex sex; @Column(name = "type") @Enumerated(EnumType.STRING)//枚举字符串 private Type type; }
下面是sex对象的代码
public enum Sex { MAIL("男"), FMAIL("女"); private String value; private Sex(String value) { this.value = value; } }