我的控制器接受一个对象来保存它,这个对象有一个名为addTime的字段,我必须自己设置这个字段,我怎么做才能让springboot功能自动执行这个操作。
@PostMapping("/HotelVersionDistribute/apply")
@Override
public Result<Boolean> apply(@RequestBody HotelVersionDistribute entity) {
// i dont want to do it,but i have no idea
entity.setAddTime(LocalDateTime.now());
HotelClientVersion hotelClientVersion = hotelClientVersionMapper.selectById(entity.getVersionId());
if(hotelClientVersion == null){
log.warn("version_id={},not exit", entity.getVersionId());
return Result.error(Result.CODE_REASOURCE_NOT_EXIST, "版本未找到");
}
saveApply(entity, hotelClientVersion);
return Result.success(true);
}发布于 2021-06-06 11:16:31
通常,像addTime这样的列( createdBy、createdDate、updatedBy、updatedDate )称为审计列。
Spring为Spring Data / Spring Data JPA提供审计支持。由于问题没有提到相同的内容,并且假设您正在使用其中的一个,请通读并实现您的需求。
来自文档:Auditing
Spring Data提供了复杂的支持,可以透明地跟踪谁创建或更改了实体,以及更改发生的时间。要从该功能中受益,必须为实体类配备审计元数据,这些元数据可以使用注释或通过实现接口
来定义
发布于 2021-06-03 10:24:37
两种方式:
类中的
public class HotelVersionDistribute {
private LocalDateTime addTime = LocalDateTime.now();
...
}列使用MySQL的默认值机制,并将
falsepublic class HotelVersionDistribute {
@Column(insertable = false, updatable = false)
private LocalDateTime addTime;
...
}https://stackoverflow.com/questions/67814436
复制相似问题