[英]To create an update api helper funtion
i am unable to complete this updateAction method as all the methods i try is either i have to set all the fields i am giving as input one by one and if i just replace the old action with the current actionDTO then all the other fields which i am not giving as input are also changing to null whereas i want only the fields i am giving as input to change all the other fields to remain same .我无法完成此 updateAction 方法,因为我尝试的所有方法要么是我必须将我提供的所有字段一一设置为输入,如果我只是用当前 actionDTO 替换旧操作,那么我的所有其他字段我不作为输入也将更改为 null,而我只希望我作为输入提供的字段将所有其他字段更改为保持不变。 please help i have been trying to solve this since 2 days trying different different solution looking for answers couldn't find any.
请帮助我自从 2 天以来一直在尝试解决这个问题,尝试不同的不同解决方案来寻找答案,但找不到任何答案。
public Actions updateAction(ActionDTO actionDTO,String actionId){
log.info("Entering the updateAction method of ActionsManager class");
log.info("Exiting the updateAction method of ActionsManager class");
return actionsRepository.findById(actionIdObject).get();
}
or just share how to use the fields to loop through all the fields in actionDTO and change it或者只是分享如何使用字段循环遍历actionDTO中的所有字段并更改它
You have to create an interface @Mapper ActionMapper and try it with @BeanMapping and then use it as a partialUpdate from Service.您必须创建一个接口@Mapper ActionMapper 并使用@BeanMapping 进行尝试,然后将其用作Service 的partialUpdate。
@Mapper(componentModel = "spring")
public interface ActionsMapper {
@BeanMapping(nullValuePropertyMappingStrategy = NullValuePropertyMappingStrategy.IGNORE)
void updateActionFromDto(ActiosnDto dto, @MappingTarget Actions actions);
}
//This should be implemented in ServiceImpl and also initialize ActionsMapper in constructor //这个要在ServiceImpl中实现,还要在构造函数中初始化ActionsMapper
private ActionMapper actionsMapper;
@Autowired
public ActionsServiceImpl(ActionsRepository actionsRepository, ActionMapper actionsMapper) {
this.actionsRepository = actionsRepository;
this.actionsMapper = actionsMapper;
}
public Actions partialUpdate(ActionsDto actionsDto, String id) {
Optional<Actions> optionalActions = actionsRepository.findById(id);
if (optionalActions.isPresent()) {
Actions myActions = optionalActions.get();
actionsMapper.updateActionFromDto(actionsDto, myActions);
return actionsRepository.save(myActions);
} else {
throw new ResponseStatusException(HttpStatus.NOT_FOUND);
}
}
public interface ActionService {
Actions partialUpdate(ActionsDto actions, String id);
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.