难道这样?
//Controller的update方法
@PutMapping("/luckmoneys/{id}")
public Luckmoney update(@PathVariable("id") Integer id,
@RequestParam(value="money",required=false,defaultValue = "0")BigDecimal money,
@RequestParam(value="producer",required = false,defaultValue = "")String producer,
@RequestParam(value="consumer",required = false,defaultValue = "")String consumer){
Luckmoney luckmoney=new Luckmoney();
luckmoney.setId(id);
luckmoney.setMoney(money);
luckmoney.setProducer(producer);
luckmoney.setConsumer(consumer);
return service.update(luckmoney);
}
//Service中的update方法
public Luckmoney update(Luckmoney luckmoney){
Optional<Luckmoney> optional=repository.findById(luckmoney.getId());
if(optional.isPresent()){
Luckmoney _luckmoney=optional.get();
if(!StringUtils.isEmpty(luckmoney.getProducer())){
_luckmoney.setProducer(luckmoney.getProducer());
}
if(!StringUtils.isEmpty(luckmoney.getConsumer())){
_luckmoney.setConsumer(luckmoney.getConsumer());
}
if(!luckmoney.getMoney().equals(BigDecimal.ZERO)){
_luckmoney.setMoney(luckmoney.getMoney());
}
return repository.save(_luckmoney);//这里update的方法也是save
}else {
return null;
}
}