springboot 2.0没有findOne,改用findById().get()
OrderMaster orderMaster = orderMasterRepository.findById(orderId).get();
if (orderMaster == null){
throw new SellException(ResultEnum.ORDER_NOT_EXIST);
}
但是,当传入参数orderId参数不存在时,会抛出异常,自己定义的SellException不会触发,需要改成findById(orderId).orElse(null)。当orderId存在时,正常返回,orderId不存在时不会抛出异常,返回括号中的值null,也可以为其他对象。
OrderMaster orderMaster = orderMasterRepository.findById(orderId).orElse(null);
if (orderMaster == null){
throw new SellException(ResultEnum.ORDER_NOT_EXIST);
}
public T orElse(T other) {
return value != null ? value : other;
}