当前位置: 首页 > 知识库问答 >
问题:

Mapstruct目标创建

宋洲
2023-03-14

mapstruct如何在以下方法之间做出决定:

>

  • 使用source的getter获取所有参数,然后调用目标的构造函数来创建映射。

    使用所有空参数调用目标的构造函数,然后使用setter设置每个参数。

    Target target(Source source) {
      if ( source == null ) {
          return null;
      }
    
      String a = null;
      String b = null;
      String c = null;
    
      a = source.getA();
      b = source.getB();
      c = source.getC();
    
      Target target = new Target( a, b, c );
    
      return target;
    }
    

    第二种方法可能是

    Target target(Source source) {
        if ( source == null ) {
            return null;
        }
    
        String a = null;
        String b = null;
        String c = null;
    
        Target target = new Target( a, b, c );
    
        target.setA(source.getA());
        target.setA(source.getB());
        target.setA(source.getC());
    
        return target;
    }
    

    这是目标对象类的样子。

    import lombok.AccessLevel;
    import lombok.AllArgsConstructor;
    import lombok.Builder;
    import lombok.Getter;
    import lombok.NoArgsConstructor;
    import lombok.NonNull;
    import lombok.Setter;
    
    @Getters
    @Setters
    @AllArgsConstructor
    @NoArgsConstructor(access = AccessLevel.PRIVATE)
    public class Target {
    
    @NonNull
    private String a;
    
    @NonNull
    private String b;
    
    @NonNull
    private String c;
    
    }
    
  • 共有1个答案

    邵和硕
    2023-03-14

    MapStruct将使用哪个构造函数的决定在文档的“使用构造函数”部分中定义。规则如下:

    >

    如果存在单个公共构造函数,则将使用它来构造对象,而忽略其他非公共构造函数。

    如果存在无参数构造函数,则它将用于构造对象,而其他构造函数将被忽略。

    如果有多个合格的构造函数,那么由于构造函数不明确,将出现编译错误。为了消除歧义,可以使用名为Default的注释(来自任何包)。

    从您提供的示例中,有一个公共构造函数(来自AllArgsConstructor的),这意味着MapStruct将使用第一种方法。

    Target target(Source source) {
      if ( source == null ) {
          return null;
      }
    
      String a = null;
      String b = null;
      String c = null;
    
      a = source.getA();
      b = source.getB();
      c = source.getC();
    
      Target target = new Target( a, b, c );
    
      return target;
    }
    

    问题中的第二种方法是没有意义的,MapSTRt永远无法生成这样的代码。如果您看到这样的东西,那么这就是一个错误。

     类似资料:
    • 我正在从ModelMapper到MapStruct的转换过程中,到目前为止一切都很顺利,除了在列表上排序和筛选的情况,如果我有一个从域对象到实体的映射,例如: 从一个api中,我想在contactAddressLine1上进行过滤或排序,然后我想查找源代码(或反向映射时的目标代码),以便将其添加到动态jpq或sql查询中。 有没有办法用MapSTRt做到这一点? 使用ModelMapper,我可以

    • 我有以下DTO和域对象。我正在使用Mapstruct将域对象复制到DTO对象。 使用下面的映射器将域映射到DTO。我不想将电话属性从域映射到DTO。怎么做?我尝试在mapping ignore中提供嵌套目标属性,但它给出了错误:

    • 我从以下来源下载了应用程序https://github.com/springframeworkguru/spring5-mvc-rest/tree/vendor-api,但Mapstruct遇到了问题。 我认为在Intellij注释中@mapper不为mapper创建bean。我没有改变John GitHub的代码。你知道吗?我试图将生成的路径源更改为目标,但这没有帮助,谢谢你的帮助。

    • 但是它包含了很多样板代码(在@Mapping注释中),我怎么能对mapper说我想要将source.user映射到这个目标而不指定target的字段呢?

    • 问题内容: 每当我在XCode 6 beta 4中进行构建时,都会出现此错误。这似乎使我的应用程序异常缓慢。 警告:* 使用空的LLDB目标在路径’/ ///***.app’处创建LLDB目标时出错,这可能会导致从远程设备读取内存的速度变慢。 这到底是什么意思,我该如何解决? 提前致谢! 问题答案: 您是否在64位设备上使用和运行您的应用程序?(iPhone 5S或iPhone 5S模拟器) 苹果

    • } 但是在映射器实现中,我看到清除了Target的列表,我不需要它: } 我是读错了还是做错了?