zoukankan      html  css  js  c++  java
  • Java 枚举那点事

    目录

      最近有需求,想存自定义的枚举值,比如

       HOTLINE("Hotline")
      

      我想存 Hotline 于是研究了一下Java的枚举问题

      如下数据库的Entity (贫血模型哈)

      @Entity
      @Table(name = "complaint")
      public class Complaint {
          @Id
          @GeneratedValue
          private Long id;
      
          private CaseOrigin origin;
      }
      

      枚举

      public enum CaseOrigin {
          HOTLINE("Hotline"),
          EMAIL("mail/fax/email"),
          WALK_IN("Walk-in"),
          OTHER("Others");
       
          public String value() {
              return value;
          }
          
          private final String value;
          
          private CaseOrigin(String value) {
              this.value = value;
          }
      }
      

      用的是Spring Boot 自带的 Hibernate
      Hibernate 提供了两种方便的注解

      • 1 也就是默认的注解 ,存是的 1,2,3,4,5 之类的数字
         @Enumerated(EnumType.ORDINAL)
          private CaseOrigin origin;
      
      • 2 这是存枚举的name 如 : OTHER("Others") 存的是OTHER
         @Enumerated(EnumType.STRING)
          private CaseOrigin origin;
      

      但是这两种方式,都不符合我的需求,我想存的是 Others
      于是,找到了目前为止最优雅的方式

      实现接口 AttributeConverter 重写接口的方法,很表意,就不解释了。

      
      public class CaseOriginConverter implements AttributeConverter<CaseOrigin, String> {
          
          @Override
          public String convertToDatabaseColumn(CaseOrigin attribute) {
              return attribute.value();
          }
          
          @Override
          public CaseOrigin convertToEntityAttribute(String dbData) {
            return CaseOrigin.formDbValue(dbData);
          }
      }
      
      

      枚举我们也要改造一下,当然,如果不想改造枚举类。在类CaseOriginConverter 重写convertToEntityAttribute(String dbData) 就要费点事了,比如switch,反射(最优雅的方式 )

      public enum CaseOrigin {
          HOTLINE("Hotline"),
          EMAIL("mail/fax/email"),
          WALK_IN("Walk-in"),
          OTHER("Others");
          
          
          public String value() {
              return value;
          }
          
          private final String value;
          
          public static final Map<String, CaseOrigin> dbValues = new HashMap<>();
          
          static {
              for (CaseOrigin value : values()) {
                  dbValues.put(value.value, value);
              }
          }
          
          private CaseOrigin(String value) {
              this.value = value;
          }
          
          public static CaseOrigin formDbValue(String dbValue) {
              return dbValues.get(dbValue);
          }
      }
      

      最后用的地方到了

         @Convert(converter = CaseOriginConverter.class)
          private CaseOrigin origin;
      

      这样就OK了。

    • 相关阅读:
      Windows 2008 R2 安装 Windows phone 7 开发环境
      win 7,win2008 无法给新建用户完全权限
      基于Ajax的Asp.Net 简易在线聊天室
      phpwind ecshop 用户整合
      UVALive 3942 Remember the Word(字典树+DP)
      UVA 11732 strcmp() Anyone? (压缩版字典树)
      UVA 11992 Fast Matrix Operations(线段树:区间修改)
      hdu 2222 Keywords Search(AC自动机模版题)
      动态规划基础练习笔记
      递归与分治策略基础练习笔记
    • 原文地址:https://www.cnblogs.com/qulianqing/p/10152454.html
    Copyright © 2011-2022 走看看