Java 在两个方向上映射/展平嵌套属性

Java 在两个方向上映射/展平嵌套属性,java,mapstruct,Java,Mapstruct,我有一个实体类,如: class SomeEntity { int id; string name; date created; } class SomeDto { class SomeDtoAttributes { string name; date created; } string id; SomeDtoAttributes attributes; } 我有一个dto类,比如: class SomeEntity { in

我有一个实体类,如:

class SomeEntity {
  int id;
  string name;
  date created;
}
class SomeDto {
   class SomeDtoAttributes {
      string name;
      date created;
   }

   string id;
   SomeDtoAttributes attributes;
}
我有一个dto类,比如:

class SomeEntity {
  int id;
  string name;
  date created;
}
class SomeDto {
   class SomeDtoAttributes {
      string name;
      date created;
   }

   string id;
   SomeDtoAttributes attributes;
}
基本上,除了id之外,实体上的所有属性都放在dto的attributes属性中

我希望此映射器可用于遵循相同模式但具有不同属性的其他实体/DTO

我已经阅读了文档,但我不清楚如何实现这一点。目前我有一些类似于:

abstract class EntityMapper<DTO, DTOA E> {

    abstract E toEntity(DTO dto, DTOA attributes);

    abstract DTO toDto(E entity);

    abstract DTOA toDtoAttributes(E entity);

    abstract List<E> toEntityList(List<DTO> dtos);

    abstract List<DTO> toDtoList(List<E> entities);

    @AfterMapping
    protected void afterMapping(@MappingTarget DTO dto, E entity) {
        DTOA dtoAttributes = this.toDtoAttributes(entity);
        dto.setAttributes(dtoAttributes);
    }
}
抽象类EntityMapper{
抽象实体(DTO DTO,DTOA属性);
抽象DTO TODO(实体);
抽象DTOA TodToAttribute(E实体);
摘要列表到实体列表(列表DTO);
抽象列表TodList(列表实体);
@后映射
受保护的void afterMapping(@MappingTarget-DTO-DTO,E-entity){
DTOA dtoAttributes=此.toDtoAttributes(实体);
dto.setAttributes(dtoAttributes);
}
}

但是我不喜欢我必须在
toEntity
方法中同时传递
dto
dtoAttributes
,并且
toEntityList
方法不会填充返回实体上的属性属性。

如果所有实体和dto都是这样。在一些接口和泛型的帮助下,您可以实现类似的功能

class SomeDto implements Dto<SomeDtoAttribute> {
   class SomeDtoAttributes implements DtoAttributes {
      String name;
      date created;
   }

   String id;
   SomeDtoAttributes attributes;
}
类SomeDto来实现Dto{
类SomeDtoAttributes实现DtoAttributes{
字符串名;
创建日期;
}
字符串id;
某些属性;
}
然后在您的映射器中,您可以执行以下操作

abstract class EntityMapper<DTO extends Dto<DTOA>, DTOA, E> {

    E toEntity(DTO dto) {
        E entity = toEntity(dto.getAttributes());
        if (entity != null) {
            entity.setId(dto.getId());
        }
        return entity;
    }

    @Mapping(target = "id", ignore = true)    
    abstract E toEntity(DTOA dtoAttributes);

    abstract DTO toDto(E entity);

    abstract DTOA toDtoAttributes(E entity);

    abstract List<E> toEntityList(List<DTO> dtos);

    abstract List<DTO> toDtoList(List<E> entities);

    @AfterMapping
    protected void afterMapping(@MappingTarget DTO dto, E entity) {
        DTOA dtoAttributes = this.toDtoAttributes(entity);
        dto.setAttributes(dtoAttributes);
    }
}
抽象类EntityMapper{
电子实体(DTO到DTO){
E实体=实体(dto.getAttributes());
如果(实体!=null){
entity.setId(dto.getId());
}
返回实体;
}
@映射(target=“id”,ignore=true)
抽象实体(DTOA DTOAT属性);
抽象DTO TODO(实体);
抽象DTOA TodToAttribute(E实体);
摘要列表到实体列表(列表DTO);
抽象列表TodList(列表实体);
@后映射
受保护的void afterMapping(@MappingTarget-DTO-DTO,E-entity){
DTOA dtoAttributes=此.toDtoAttributes(实体);
dto.setAttributes(dtoAttributes);
}
}

这是什么语言?每个DTO类是否都将包含反映匹配实体的内部DTOAttribute类?如果是这样,为什么不让DTO构造函数接受实体并将非id字段映射到新的DTOAAttribute类实例?如果没有,您可以使用java反射吗?