【问题标题】:Spring Boot Mapstruct not woking mappingSpring Boot Mapstruct 不工作映射
【发布时间】:2021-11-19 00:26:13
【问题描述】:

我的 Dto 模型

public class CoverLetterDto{
  
   private Long user_id;

}

我的主要模特

public class CoverLetter {
      
       private User user;
    
    }

我的 mapstruct 类

@Mapping(source="user_id", target = "user")
CoverLetter CoverLetterDtotoEntityCoverLetter(CoverLetterDto dto, User user);

大家好,我的项目使用 mapstruct 但我的 dto 类 user_id 没有映射用户,请帮助我谢谢

【问题讨论】:

    标签: java spring-boot mapstruct


    【解决方案1】:

    这应该可以解决问题:

    @Mapper
    public interface CoverLetterMapper {
    
        CoverLetterMapper INSTANCE = Mappers.getMapper(CoverLetterMapper.class);
    
        @Mapping(source="user_id", target = "user.user_id")
        CoverLetter mapCoverLetterDtoToCoverLetter(CoverLetterDto dto);
    }
    

    因为您想将user_idCoverLetterDto 映射到CoverLetter 中的User 类,所以您必须使用点符号 来访问user_id 中的字段user 对象。

    如果您还有其他问题,请告诉我。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2016-12-14
      • 1970-01-01
      • 2015-11-01
      • 2021-06-24
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多