mapstruct如何使对象中的List<Object>中的Object之间不同的字段mapping

mapstruct如何使对象中的List<Object>中的Object之间不同的字段mapping

class Fact {
List<Certification> certificationList;
}
class FactDto {
   List<CertificationDto> certificationList;
}
class Certification {
   Boolean inactive;
}
class CertificationDto {
   Boolean inActive;
}
@Mapper
interface Converter {
       FactDto toDto(Fact fact);

       Fact toEntity(FactDto factDto);
}

情况需要,不能修改原本的字段,不可以吧CertificationDto中的inActive重命名为inactive,只能考虑mapstruct的实现。

因为certificationList是List,

@Mapper
interface Converter {
// 因为certificationList是List,所以是不能这样写的,启动的时候mapstruct会报错
@Mapping(source="certificationList.inactive", target="certificationList.inActive")
FactDto toDto(Fact fact);

   Fact toEntity(FactDto factDto);
}
@Mapper
interface Converter {
       FactDto toDto(Fact fact);

       Fact toEntity(FactDto factDto);

       List<CertificationDto> toDtoList(List<Certification> certificationList);

       List<Certification> toEntityList(List<CertificationDto> certificationDtoList);

       @Mapping(source = "inactive", target = "inActive")
       CertificationDto toDto(Certification certification);

       @Mapping(source = "inActive", target = "inactive")
       Certification toEntity(CertificationDto certification);
}

这个其实看mapstruct编译出来的类,大胆猜测下实现方式和可能存在的优化手段,编写下代码看下结果就可以猜出来是这样写的。

吐槽:我用md写的,结果上传md编辑器,没保存之前是有内容的,保存之后内容不见了,懒得搞了,重新编辑了一遍纯文本,看得懂就行

posted on 2022-09-11 12:13  我欲皆真  阅读(1152)  评论(0编辑  收藏  举报

导航