1. 数据库enum类型
-
数据库设计:enum
2. 枚举类特点
-
特点
-
枚举类的标配:私有化参数全参构造器+具体真实对象+私有化属性+属性的get方法
-
一定要有全参构造器
-
一定要有value的get方法
-
@EnumValue只是提示作用,没有她,只要值类型匹配也会录入
- 用它规范作用
- 最好放在实际起到作用的字段,或者整个枚举对象
-
-
核心
-
枚举类中的对象:TRUE,一般是存储到数据库中的具体值
-
如果需要枚举对象里面的私有化属性,那就要调用他的get方法
-
值的获取
IsDeployedWeb.TRUE:获取枚举类的对象名称 IsDeployedWeb.TRUE。getValue():获取枚举类对象的属性值
-
-
例子
/** * @className: com.sepy.testmysql.IsDeployedWeb * @author: sepY * @create: 2023-04-13 23:45 * @description: 枚举类 * 枚举类要求:私有化全参数构造器 ,真实对象,私有化属性+,get方法:为了获取参数可以用@getter,但是也可以用final,tostring方法 */ @AllArgsConstructor //全参构造器 public enum IsDeployedWeb { TRUE("是"), FALSE("否"); @Getter //获取值 // @EnumValue// 对应的数据库值类型为enum,不用他也可以,会自动匹配转化 private String value;//私有化不变属性 }
3. 实体类设置为String
-
实体类:enum值为实际的插入值类型
@Data @AllArgsConstructor @NoArgsConstructor @Builder @TableName("common_alert_infos") public class CommonAlertInfo { private String ip; private Integer domain_num; private String isDeployedWeb; private String portList; // @JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss") // @TableField(fill = FieldFill.INSERT_UPDATE) //// @DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss") // @DateTimeFormat(pattern = "yyyy-MM-dd ") private Date createTime; }
-
设置枚举参照类
/** * @className: com.sepy.testmysql.IsDeployedWeb * @author: sepY * @create: 2023-04-13 23:45 * @description: 枚举类 * 枚举类要求:私有化全参数构造器 ,真实对象,私有化属性+,get方法:为了获取参数可以用@getter,但是也可以用final,tostring方法 */ @AllArgsConstructor //全参构造器 public enum IsDeployedWeb { TRUE("是"), FALSE("否"); @Getter //获取值 @EnumValue// 对应的数据库值类型为enum,不用他也可以,会自动匹配转化;用它有 private String value;//私有化不变属性 }
-
test测试:传入value值,匹配对应的enum值就行
@Resource UserMapper userMapper; @Test void contextLoads() { System.out.println(new Date()); CommonAlertInfo info = CommonAlertInfo.builder().ip("101").createTime(new Date()).isDeployedWeb(IsDeployedWeb.FALSE.getValue()).domain_num(1).build(); int insert = userMapper.insert(info); }
4. 实体类设置为enum
-
实体类
@Data @AllArgsConstructor @NoArgsConstructor @Builder @TableName("common_alert_infos") public class CommonAlertInfo { private String ip; private Integer domain_num; @EnumValue private IsDeployedWeb isDeployedWeb; private String portList; // @JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss") // @TableField(fill = FieldFill.INSERT_UPDATE) //// @DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss") // @DateTimeFormat(pattern = "yyyy-MM-dd ") private Date createTime; }
-
enum
/** * @className: com.sepy.testmysql.IsDeployedWeb * @author: sepY * @create: 2023-04-13 23:45 * @description: 枚举类 * 枚举类要求:私有化全参数构造器 ,真实对象,私有化属性+,get方法:为了获取参数可以用@getter,但是也可以用final,tostring方法 */ @AllArgsConstructor //全参构造器 public enum IsDeployedWeb { TRUE("是"), FALSE("否"); @Getter //获取值 @EnumValue// 对应的数据库值类型为enum,不用他也可以,会自动匹配转化;用它有 private String value;//私有化不变属性 /** * 返回方法 * @param value * @return */ public static IsDeployedWeb getByValue(String value) { for (IsDeployedWeb it : IsDeployedWeb.values()) { if (it.getValue() == value) { return it; } } return null; } }
-
测试类
@Resource UserMapper userMapper; @Test void contextLoads() { System.out.println(new Date()); CommonAlertInfo info = CommonAlertInfo.builder().ip("1101").createTime(new Date()) .isDeployedWeb(IsDeployedWeb.getByValue(IsDeployedWeb.FALSE.getValue())).domain_num(1).build(); int insert = userMapper.insert(info); }