gpt4 book ai didi

Java 和 MapStruct

转载 作者:行者123 更新时间:2023-12-01 19:52:09 25 4
gpt4 key购买 nike

我如何初始化字段中的列表并在此列表中添加一些值?

    @Mapper(componentModel = "spring")
public interface MapStructAdapter {

@Mapping(source = "some", target = "some")
@Mapping(expression = "java(new java.util.ArrayList<LegalEntity>())", target = "legalEntities")
@Mapping(expression = "java(new my.some.package.LegalEntity())", target = "getLegalEntities().add()")
@Mapping(source = "entityShortName", target = "legalEntities.legalEntity.shortName")
Representative convert(Message message);
}

最佳答案

您应该能够在映射器中添加自己的转换方法。

https://mapstruct.org/documentation/stable/reference/html/#adding-custom-methods

示例,

@Mapper(componentModel = "spring")
public interface MapStructAdapter {

@Mapping(source = "some", target = "some")
@Mapping(source = "entityShortName", target = "legalEntities")
Representative convert(Message message);

default List<LegalEntity> toLegalEntities(String entityShortName) {
LegalEntity legalEntity = new LegalEntity();
legalEntity.setShortName(entityShortName);
return Collections.singletonList(legalEntity);
}
}

关于Java 和 MapStruct,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59077100/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com