简体   繁体   English

创建更新 api 帮助函数

[英]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.

相关问题 如何创建功能来更新与i不同的变量? - How to create Funtion to update variables that differ i? 更新查询不适用于OrientDB中的eval()函数和参数 - Update query not working with eval() funtion and parametes in OrientDB 是否有任何 REST API 用于在 SAP HANA 登录中创建和更新用户 - Is there any REST API for create and update user in SAP HANA logon Java REST API,用于在Atlassian Confluence中创建和更新包含内容的新页面 - Java REST API to create and update new pages with contents in Atlassian Confluence‎ 蓝牙回调功能onCharacteristicRead没有让Intent调用另一个活动 - Bluetooth Callback funtion onCharacteristicRead is not making Intent call to another activity Working with <API21. Not in Marshmallow 从GCM帮助程序库迁移到GCM Api - Migrating from GCM helper library to GCM Api Android / Java创建一个帮助类来创建图形 - Android/Java creating a helper class to create graphs 何时创建辅助方法和单独的文件 - When to create helper methods and separate files 无论如何要创建创建模拟的Test Helper类? - Anyway to create a Test Helper class that creates mocks? Salesforce批量API使用Java从列表创建和更新1000条记录 - Salesforce bulk api to create and update 1000 records from a list using java
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM