【问题标题】:Mapstruct list of objectMapstruct 对象列表
【发布时间】:2021-10-08 16:52:20
【问题描述】:

我想编写 DocumentMapper,或者我可以专门为部分编写秒数,或者我可以在这里如何做。我也可以编写 SectionMapper 还是可以将我想添加的所有内容合并到 DucumentMapper 中?

public class Body {
    
    @XmlElement(name = "sec")
    private List<Sec> secs;

}
public class Sec {

@XmlElement(name = "title")
private String title;

@XmlElement(name = "p")
private List<String> ps;

@XmlElement(name = "sec")
private List<Sec> subsections;

@XmlElement(name = "fig")
private List<String> figs;

}

public class Document extends JPAEntity {

@NonNull
@Column(name = "name", length=2000)
private String name;

@Column(name = "status", length=50)
private String status;

private List<Section> sections;

}

public class Section extends JPAEntity {

private String title;

private List<Paragraph> paragraphs;

private List<Figure> figures;

}

【问题讨论】:

  • 到目前为止你尝试了什么?
  • 是的,谢谢,我发布了答案。

标签: java spring spring-boot mapstruct


【解决方案1】:
@Mapper(componentModel="spring")
public interface SectionMapper {
 
SectionMapper INSTANCE = Mappers.getMapper( SectionMapper.class );
 
@Mappings({
    @Mapping(source = "title", target = "title"),
    @Mapping(source = "ps", target = "paragraphs"),
    @Mapping(source = "figs", target = "figures")
})
Section secToSection(Sec sec);

List<Section> map(List<Sec> list);

List<Section> convertSecDTOListToSectionList(List<Sec> list);

List<Sec> convertSectionListToSecDTOList(List<Section> list);


@Mappings({
    @Mapping(source = "p", target = "paragraph")
})
Paragraph pToParagraph(String p);   

    
List<Paragraph> convertPsDTOListToParagraphList(List<String> list);



@Mappings({
    @Mapping(source = "fig", target = "figure")
})
Figure figToFigure(String fig);  

List<Figure> convertFigsDTOListToFigureList(List<String> list);

}

别忘了getter/setter方法

【讨论】:

    猜你喜欢
    • 2019-12-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多