mysql的enum字段,使用mybatisplus存储

1. 数据库enum类型

  1. 数据库设计:enum

cf3f912fb817cf6e8fa32661f0e4e0b.png

2. 枚举类特点

  1. 特点

    • 枚举类的标配:私有化参数全参构造器+具体真实对象+私有化属性+属性的get方法

    • 一定要有全参构造器

    • 一定要有value的get方法

    • @EnumValue只是提示作用,没有她,只要值类型匹配也会录入

      • 用它规范作用
      • 最好放在实际起到作用的字段,或者整个枚举对象
  2. 核心

    1. 枚举类中的对象:TRUE,一般是存储到数据库中的具体值

    2. 如果需要枚举对象里面的私有化属性,那就要调用他的get方法

    3. 值的获取

      IsDeployedWeb.TRUE:获取枚举类的对象名称
      IsDeployedWeb.TRUE。getValue():获取枚举类对象的属性值
      
  3. 例子

    /**
     * @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

  1. 实体类: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;
    }
    
  2. 设置枚举参照类

    /**
     * @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. 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

  1. 实体类

    @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;
    }
    ​
    
  2. 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;
        }
    }
    
  3. 测试类

    @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);
    }
    
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值