gpt4 book ai didi

java - 使用 mapstruct 从 List 映射 List
转载 作者:塔克拉玛干 更新时间:2023-11-03 05:31:27 26 4
gpt4 key购买 nike

您好,我在使用 mapstruct 从子源类中设置 DTO 中的 List 操作时得到 null。有人可以帮我解决这个问题吗?请在这里找到我的代码

实体类:

public class Source {
int id;
String name;
List<ChildSource> childSource;
//getters and setters
}

public class ChildSource {
String code;
String action;
//getters and setters
}

目的地DTO:

public class TargetDTO{
int sNo;
String mName;
List<String> actions;
//getters and setters
}

MApper 类:

@Mapper(componentModel = "spring")    
public abstract class SampleMapper {
@Mappings({
@Mapping(target = "id", source = "sno"),
@Mapping(target = "name", source = "mNAme")
})
public abstract TargetDTO toDto(Source source);

@IterableMapping(elementTargetType = String.class)
protected abstract List<String> mapStringtoList(List<ChildSource> childSource);

protected String mapChildSourceToString(ChildSource child) {
return child.getAction();
}
}

但我的操作列表在目标 dto 中设置为 null。有人可以帮我吗?

最佳答案

你可以这样做。


@Mapper(componentModel = "spring")
public abstract class SampleMapper {
@Mappings({
@Mapping(target = "id", source = "sno"),
@Mapping(target = "name", source = "mNAme"),
@Mapping(target = "actions", source = "childSource")
})
public abstract TargetDTO toDto(Source source);

protected abstract List mapStringtoList(List childSource);

protected String mapChildSourceToString(ChildSource child) {
return child.getAction();
}
}

关于java - 使用 mapstruct 从 List<Object> 映射 List<String>,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46598488/

26 4 0