标签:create ret open return phone bsp stat vat null
1.写一个通用接口
public interface CodeEnum<T> { T getCode(); }
2.枚举类中实现接口
@Getter public enum OrderStatusEnum implements CodeEnum<Integer> { NEW(0, "新订单"), FINISHED(1, "完结"), CANCEL(2, "已取消"),; private Integer code; private String msg; OrderStatusEnum(Integer code, String msg) { this.code = code; this.msg = msg; } }
3.建一个工具 类
public class EnumUtil { public static <T extends CodeEnum> T getByCode(Integer code, Class<T> enumClass) { for (T each : enumClass.getEnumConstants()) { if (code.equals(each.getCode())) { return each; } } return null; } }
4.dto 类中引用
@Data //@JsonInclude(JsonInclude.Include.NON_NULL) public class OrderDTO { private String orderId; private String buyerName; private String buyerPhone = ""; private String buyerAddress; private String buyerOpenid; //订单总金额 private BigDecimal orderAmount; //订单状态 默认0新订单 private Integer orderStatus; //支付状态 默认0未支付 private Integer payStatus; // @JsonSerialize // @DateTimeFormat(pattern = "yyyy-MM-dd") @JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss") private Date createTime; private Date updateTime; List<OrderDetail> orderDetailList; @JsonIgnore //对象转json 格式可以忽略 public OrderStatusEnum getOrderStatusEnum() { return EnumUtil.getByCode(orderStatus, OrderStatusEnum.class); } }
标签:create ret open return phone bsp stat vat null
原文地址:https://www.cnblogs.com/qinls/p/10080835.html