我们通常如果数据库定义的是int类型,但是需求是页面显示的是字符串,如果是多个方法,就需要重复调用,所以为此,我们提供了枚举类型来优化,
例子如下:
if(StringUtils.isNotBlank(singleCenter.getStatus())){ switch (singleCenter.getStatus()) { case "0": listStatus="已接单"; break; case "1": listStatus="未处理"; break; case "2": listStatus="已拒绝"; break; case "3": listStatus="代接单"; break; case "4": listStatus="代下单"; break; } singleCenter.setStatus(listStatus); }
优化方法:
定义一个枚举类:将你需要转换的字符串如下显示,然后调用下面的
getTypeValue()方法,再在你需要转换的代码中调用该方法就OK了
public enum BusinessTypeEnum { //这里就是定义index对应的value TYPE_1(0,"海运进口整箱"),TYPE_2(1,"海运进口拼箱"),TYPE_3(2,"海运出口整箱"),TYPE_4(3,"海运出口拼箱"),TYPE_5(4,"空运进口") ,TYPE_6(5,"空运出口"),TYPE_7(6,"集装箱出口"),TYPE_8(7,"集装箱进口"),TYPE_9(8,"多式联运"),TYPE_10(9,"集装箱") ,TYPE_11(10,"散货船"),TYPE_12(11,"干线运输"),TYPE_13(12,"零担运输"),TYPE_14(13,"仓储运输"); private int index; private String value; private BusinessTypeEnum(int index,String value){ this.index = index; this.value = value; } public int getIndex() { return index; } public void setIndex(int index) { this.index = index; } public String getValue() { return value; } public void setValue(String value) { this.value = value; } //主要通过这个方法来去value public static String getTypeValue(int index){ for(BusinessTypeEnum e : BusinessTypeEnum.values()){ if(e.getIndex() == index){ return e.getValue(); } } return ""; } }