Java物件複製之MapStruct使用

万树飞花發表於2024-03-27

gradle依賴

implementation group: 'org.mapstruct',name:'mapstruct-jdk8',version:'1.3.1.Final'
 annotationProcessor "org.mapstruct:mapstruct-processor:1.3.1.Final"

mapper(前提是原始物件與目標物件屬性一致,不一致的需要單獨處理)

@Mapper
public interface AbcMapper {

    AbcMapper INSTANCE = Mappers.getMapper(AbcMapper.class);

    List<Abc> bcdToAbc(List<Bcd> bcdList);
}

使用

List<Abc> abcList = AbcMapper.INSTANCE.bcdToAbc(bcdList);
  • 解決編譯告警
@Mapper(unmappedTargetPolicy = ReportingPolicy.IGNORE)
public interface AbcMapper {

    AbcMapper INSTANCE = Mappers.getMapper(AbcMapper.class);

    List<Abc> bcdToAbc(List<Bcd> bcdList);
}

相關文章