代码之家  ›  专栏  ›  技术社区  ›  Ali

MapStruct无法为@映射定义生成实现代码

  •  0
  • Ali  · 技术社区  · 2 年前

    Mapstruct在项目中运行良好,但@Mapping注释存在问题。

    Working Spring Boot版本2.2.0.RELEASE

    以下是依赖关系的详细信息:

    implementation 'org.projectlombok:lombok'
    annotationProcessor 'org.projectlombok:lombok'
    implementation 'org.mapstruct:mapstruct'
    annotationProcessor 'org.mapstruct:mapstruct-processor'
    implementation 'org.projectlombok:lombok-mapstruct-binding:0.2.0'
    annotationProcessor "org.projectlombok:lombok-mapstruct-binding:0.2.0"
    

    下面是关于这个问题的例子。

    用户实体.java

    public class UserEntity {
    
        private String username;
        private Integer userAge;
        private UserAddressEntity userAddressEntity;
    }
    

    用户地址实体.java

    public class UserAddressEntity {
        private String userAddress;
        private Integer userPhone;
    }
    

    用户地址DTO

    public class UserAddressDTO {
        private String userAddress;
        private Integer userPhone;
    }
    

    TargetDTO(我想将UserEntity转换为此DTO)

    public class TargetDTO {
    
        private String username;
        private Integer userAge;
        private UserAddressDTO userAddressDto; 
    }
    

    这是映射程序类:

    @Mapper(componentModel = "spring")
    public interface UserEntityMapper  {
    
    
        @Mapping(source = "userAddressEntity", target = "userAddressDto")
        List<TargetDTO> fromEntityToDto(List<UserEntity> userEntityList);
    }
    

    在此接口中,Mapstruct无法为@Mapping创建实现。我这样改变了实现方式:

    @Mapper(componentModel = "spring")
    public interface UserEntityMapper  {
    
    
        @Mapping(source = "userAddressEntity.userAddress", target = "userAddressDto.userAddress")
        @Mapping(source = "userAddressEntity.userPhone", target = "userAddressDto.userPhone")
        List<TargetDTO> fromEntityToDto(List<UserEntity> userEntityList);
    }
    

    它仍然不起作用。

    所有其他字段都已成功映射,只有@mapping中的定义不起作用。

    但是,当我删除@Mapping并将TargetD更改为这样时,它就起作用了:

    public class TargetDTO {
      
        private String username;
        private Integer userAge;
        private UserAddressDTO userAddressEntity; // I just updated property naming
    }
    

    但为什么它不能与@Mapping一起工作呢。 这就是问题所在。

    我还尝试了“使用”定义和单独的有问题的映射器,但它也无法将UserAddressEntity映射到UserAddressDTO:

    @Mapper(componentModel = "spring", **uses = UserAddressMapper.class**)
    public interface UserEntityMapper  {
    
    
        @Mapping(source = "userAddressEntity", target = "userAddressDto")
        List<TargetDTO> fromEntityToDto(List<UserEntity> userEntityList);
    }
    

    @Mapping的使用有什么问题? 当代码编译时,Mapstruct可以创建Implementation类并为所有其他字段生成实现代码,但不能为Implemention类中的@Mapping创建实现代码。我尝试了很多方法,但无论如何都不起作用。

    0 回复  |  直到 2 年前
        1
  •  2
  •   Toni Arpan    2 年前

    @Mapping 不直接适用于映射集合的方法。相反,您应该为各个对象定义一个映射方法。

    @Mapper(componentModel = "spring")
    public interface UserEntityMapper {
    
      @Mapping(source = "userAddressEntity", target = "userAddressDto")
      TargetDTO fromEntityToDto(UserEntity userEntityList);
    
      List<TargetDTO> fromEntitiesToDtos(List<UserEntity> userEntityList);
    }
    

    MapStruct将调用 fromEntityToDto 方法。