Exception
继承RuntimeException方法 调用父类的super(msg)方法,将错误 的信息传递出去
import cn. cumt. sell. enums. ResultEnum;
public class SellException extends RuntimeException {
private Integer code;
public SellException ( ResultEnum resultEnum) {
super ( resultEnum. getMsg ( ) ) ;
this . code = resultEnum. getCode ( ) ;
}
}
utils(keyUtil)
作用:生成主键。例如订单的主键 格式:时间(毫秒级别)+6为随机数 精确到毫秒级别的时间和6位随机数,基本不太会产生相同的数字。但是高并发时,仍有可能会发生。所以,我们加一个线程同步。
public class KeyUtil {
public static synchronized String genUniqueKey ( ) {
Random random = new Random ( ) ;
Integer number = random. nextInt ( 900000 ) + 100000 ;
return System. currentTimeMillis ( ) + String. valueOf ( number) ;
}
}
VO(view object)
我们只将我们需要展示给前段页面的数据传递给前段。 例子:商品的信息有:种类,价钱,库存。买家(前端页面)不需要知道库存,我们也不能给它,否则会危害到数据段安全与隐私。所以,我们另外建立类一个实体类。类中的信息,以json的格式返回给前端页面。
@Data
public class ProductVO {
@JsonProperty ( "name" )
private String categoryName;
@JsonProperty ( "type" )
private Integer categoryTYpe;
@JsonProperty ( "foods" )
private List< ProductInfoVO> productInfoVOS ;
}
@Data
public class ProductInfoVO {
@JsonProperty ( "id" )
private String productId;
@JsonProperty ( "name" )
private String productName;
@JsonProperty ( "price" )
private BigDecimal productPrice;
@JsonProperty ( "description" )
private String productDescription;
@JsonProperty ( "icon" )
private String productIcon;
}
@Data
public class ResultVO < T> {
private Integer code;
private String message;
private T data;
}
DTO (DATA TRANSFER OBJECT)
与VO相反,dto与Controller交互,他是接收controller传递过来的数据。比如,页面传递过来的订单信息。买家是谁,叫啥,手机号是啥。订单详细信息,买了啥,买类多少等。
import lombok. Data;
@Data
public class CarDTO {
private String prouctId;
private Integer productQuantity;
public CarDTO ( String prouctId, Integer productQuantity) {
this . prouctId = prouctId;
this . productQuantity = productQuantity;
}
}
@Data
public class OrderDTO {
private String orderId;
private String buyerName;
private String buyerPhone;
private String buyerAddress;
private String buyerOpenid;
private BigDecimal orderAmount;
private Integer orderStatus;
private Integer payStatus ;
private Date createTime;
private Date updateTime;
List< OrderDetail> orderDetailList;
}