如果源不包含此列表,如何在目标(用于映射)中获取列表元素?



我正在尝试映射下一个结构(在推土机中成功映射,mapstruct有一些问题(列表在源中不存在,如何在映射器中获取它的第一个元素?目标类别:

@Data
public class FirstLvl {
protected List<SecondLvl> list;

@Data
public static class SecondLvl {
protected FirstLvl.SecondLvl.ThirdLvl thirdLvl;
@Data
public static class ThirdLvl {
protected FourthLvl fourthLvl;
}
}
}
@Data
public class FourthLvl {
protected FourthLvl.FithLvl fithLvl;
@Data
public static class FithLvl {
protected String str1;
protected String str2;
}
}

来源:

@Data
public class FirstLvlDTO {
protected FirstLvlDTO.SecondLvlDTO secondLvlDTO ;
@Data
public static class SecondLvlDTO  {
protected FirstLvlDTO.SecondLvlDTO.ThirdLvlDTO thirdLvlDTO ;

@Data
public static class ThirdLvlDTO {
protected FourthLvlDTO fourthLvlDTO;
}
}
}
@Data
public class FourthLvlDTO  {
protected String str1;
protected String str2;
}

映射器和对象工厂:

@Mapper(uses = { ObjectFactory.class })
public interface FirstLvlDTOtoFirstLvl  {

@映射(目标="list[0]。thirdLvl.quathLVl.fithLvl.str2",源="secondLvlDTO.thirdLVDTO.quathLVDTO.str2"(

FirstLvl toFirstLvl (FirstLvlDTO firstLvlDTO );

}
public class ObjectFactory {
public FirstLvl createFirstLvl() {
return new FirstLvl();
}

public FirstLvl.SecondLvl createFSLvl() {
return new FirstLvl.SecondLvl();
}
}

问题是我不知道如何获得List的第一个元素(在mapstruct中没有使用[]表示法(。我可以将方法getlist0/setlist0添加到类FirstLvl中,只需返回List的第一个元素(return List.get(0((,并在list0.thirdLvl.fourthLvl.fithLvl.str2这样的映射器中使用它,但我不知道这是个好主意吗,还有可能做得更好吗List在FirstLvlDTO(源代码(中不存在,所以我真的不明白如何将其映射并将这个嵌套结构设置为List的第一个元素

我不确定我是否完全理解您的代码。我的方法会是这样的。但是,您在列表中的类型似乎不匹配。

public interface TestMapper {
@Mapping(target = "list0", ignore = true)
@Mapping(target = "list", ignore = true)
A toA(A1 a1);
@AfterMapping
default void mapperCode(@MappingTarget A a, A1 a1) {
List<A.B> list = a.getList(); // target list requiures Type A.B 
A1.B1 b1 = a1.getB1(); // source does have A1.B1 and not A.B as type
// if your Types match set the list here manually like 
// a.setList(List.of(a1.getB()));
}
}

您的代码只能使用A、B、A1、B1…读取。我建议你用真名。

编辑:

一个可行但非常丑陋的解决方案是手动操作。我不确定这对你是否有帮助。

public interface TestMapper {
@Mapping(target = "list", ignore = true)
FirstLvl map(FirstLvlDTO firstLvlDTO);
@AfterMapping
default void mapperCode(@MappingTarget FirstLvl firstLvl, FirstLvlDTO firstLvlDTO) {
String str1 = firstLvlDTO.getSecondLvlDTO().getThirdLvlDTO().getFourthLvlDTO().getStr1();
String str2 = firstLvlDTO.getSecondLvlDTO().getThirdLvlDTO().getFourthLvlDTO().getStr2();
FirstLvl.SecondLvl secondLvl = new FirstLvl.SecondLvl();
FirstLvl.SecondLvl.ThirdLvl thirdLvl = new FirstLvl.SecondLvl.ThirdLvl();
secondLvl.setThirdLvl(thirdLvl);
FourthLvl fourthLvl = new FourthLvl();
thirdLvl.setFourthLvl(fourthLvl);
FourthLvl.FithLvl fithLvl = new FourthLvl.FithLvl(str1, str2);
fourthLvl.setFithLvl(fithLvl);
firstLvl.setList(Arrays.asList(secondLvl));
}
}
@Data
public class FourthLvl {
protected FourthLvl.FithLvl fithLvl;
@Data
public static class FithLvl {
public FithLvl(String str1, String str2){
this.str1 = str1;
this.str2 = str2;
}
protected String str1;
protected String str2;
}

}

Mapstruct不支持多对一或一对多映射,这些映射需要手动实现。

在这里,您可以找到一个适用于您的类的示例:

@Mapper
public abstract class LevelMapper {
@Mapping( target = "secondLvlDTO", source = "list" )
public abstract FirstLvlDTO map(FirstLvl lvl);
// used by the manual implementation to continue mapping.
@Mapping( target = "thirdLvlDTO.fourthLvlDTO", source = "thirdLvl.fourthLvl.fithLvl" )
protected abstract SecondLvlDTO map(SecondLvl lvl);
// manually implementation for getting the first object from the list and converting it.
protected SecondLvlDTO getFirst(List<SecondLvl> list) {
return map( list.get( 0 ) );
}
// the reverse

@Mapping( target = "list", source = "secondLvlDTO" )
public abstract FirstLvl map(FirstLvlDTO lvl);
// used by the manual implementation to continue mapping.
@Mapping( target = "thirdLvl.fourthLvl.fithLvl", source = "thirdLvlDTO.fourthLvlDTO" )
protected abstract SecondLvl map(SecondLvlDTO lvl);
// manually implementation for creating the the list and filling it with the converted dto.
protected List<SecondLvl> asList(SecondLvlDTO dto) {
return Arrays.asList( map( dto ) ); // or any other way to create a list.
}
}

最新更新