枚举

简介:初次知道枚举  还是刚学java的时候 那时候讲四类八种,还有一种特殊的类型 叫做枚举,开始工作以后发现都是用的常量类Constant 直到现在需要处理数据库的一些东西,那么多类型 长度,如果代码中判断 要写多少if else  switch的话要弄多少处理分支。然后同事提示我可以试试枚举。一试,wo CaO,真香

mysql的字段长度枚举类:

public enum MysqlColumnTypeLengthEnum {
    
    BIGINT("bigint", 255),                        //bigint
    BINARY("binary", 255),                         //binary
    BIT("bit", 64),                                 //bit
    BLOB("blob", 0),                             //blob
    CHAR("char", 255),                             //char    
    DATE("date", 0),                             //date
    DATETIME("datetime", 6),                     //datetime
    DECIMAL("decimal", 65),                       //decimal 
    DOUBLE("double", 255),                         //double
    ENUM("enum", 64),                             //enum
    FLOAT("float", 255),                         //float
    GEOMETRY("geometry", 0),                     //geometry
    GEOMETRYCOLLECTION("geometrycollection", 0),//geometrycollection
    INT("int", 255),                            //int
    INTEGER("integer", 255),                    //integer
    JSON("json", 0),                            //json
    LINESTRING("linestring", 0),                //linestring
    LONGBLOB("longblob", 0),                    //longblob
    LONGTEXT("longtext", 0),                    //longtext
    MEDIUMBLOB("mediumblob", 0),                //mediumblob
    MEDIUMINT("mediumint", 255),                //mediumint
    MEDIUMTEXT("mediumtext", 0),                //mediumtext
    MULTILINESTRING("multilinestring", 0),        //multilinestring
    MULTIPOINT("multipoint", 0),                //multipoint
    MULTIPOLYGON("multipolygon", 0),            //multipolygon
    NUMERIC("numeric", 65),                        //numeric
    POINT("point", 0),                            //point
    POLYGON("polygon", 0),                        //polygon
    REAL("real", 255),                            //real
    SET("set", 64),                                //64
    SMALLINT("smallint", 255),                    //smallint
    TEXT("text", 0),                            //text
    TIME("time", 6),                            //time
    TIMESTAMP("timestamp", 6),                  //timestamp
    TINYBLOB("tinyblob", 0),                    //tinyblob
    TINYINT("tinyint", 255),                    //tinyint
    TINYTEXT("tinytext", 0),                    //tinytext
    VARBINARY("varbinary", 65522),                //varbinary            
    VARCHAR("varchar",21840),                     //varchar
    YEAR("year", 4),                             //year
    
    UNDEFINED_COLUMN("",null);                   //未定义
    
    public String name;
    public Integer len;
    
    MysqlColumnTypeLengthEnum(String name, Integer len){
        this.name = name;
        this.len = len;
    }
    
    public static MysqlColumnTypeLengthEnum getColumnInfo(String name){
        for(MysqlColumnTypeLengthEnum item: values()){
            if(name.equals(item.name)){
                return item;
            }
        }
        return UNDEFINED_COLUMN;
    }
}

调用的时候:

        List<Map<String, Object>> mysqlTypeList = new ArrayList<>();
        for (MysqlColumnTypeLengthEnum item : MysqlColumnTypeLengthEnum.values()) {
            Map<String, Object> m = new HashMap<>();
            m.put("name", item.name);
            m.put("len", item.len);
            mysqlTypeList.add(m);
        }

下边连接是相关文章 人家总结的非常好!!!

JAVA常量类的实现方式_枚举类_项目实践  对于常量类的封装整理的很妙

使用枚举代替常量,简化工作 尤其是最后的使用注解来代替常量类和枚举,靓仔 

posted @ 2020-06-20 12:41  ジ绯色月下ぎ  阅读(140)  评论(0编辑  收藏  举报