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

如何使用mapstruct将对象映射为字段?

刘焱
2023-03-14

我需要将多个dto映射到实体。几乎所有的dto都有一个指向另一个dto的引用,我不知道在映射dto时如何传递引用。

以下是该地址的一个业务实现:

addressRepository.saveAll(dtos.stream()
                .map(addressMapper::asEntity)
                .collect(Collectors.toList())).stream()
                .map(addressMapper::asDto)
                .collect(Collectors.toList());

这是地图绘制者

    @Mappings({
            @Mapping(target="streetName", source="streetName"),
            @Mapping(target="streetNumber", source="streetNumber"),
            @Mapping(target="block", source="block"),
            @Mapping(target="floor", source="floor")

    })
    CsvBusinessData asDto(AddressEntity address);

    @InheritInverseConfiguration
    AddressEntity asAddressEntity(CsvBusinessData address);

所有实现者都从同一个dto读取,然后将它们映射到dto和entites。

以下是针对客户实施的建议:

customerRepository.saveAll(dtos.stream()
            .map( csvMapper::asCustomerEntity)

            .collect(Collectors.toList())).stream()
            .map(customerMapper::asDto)
            .collect(Collectors.toList()); 

以及我希望引用映射地址实体的映射(我的客户实体有一个地址类型字段)。

 @Mappings({
            @Mapping(target = "nom", source = "lastName"),
            @Mapping(target = "nd", source = "accountNumber"),
            @Mapping(target = "logo", source = "logo"),
            @Mapping(target = "address", source = "address"
            )

    })
    CsvBusinessData asDto(CustomerEntity customer);

    @InheritInverseConfiguration
    CustomerEntity asCustomerEntity(CsvBusinessData customer);

非常感谢你。

共有1个答案

梁韬
2023-03-14

首先,您不必为同名字段创建映射,MapStruct会为您创建映射。例如block block。1) 您的对象是相关的:您可以对相关对象使用“使用”功能。

@Mapper(uses = {AddressMapper.class}, unmappedTargetPolicy = ReportingPolicy.IGNORE)
public interface CustomerMapper {
    @Mapping(target = "nom", source = "lastName")
    @Mapping(target = "nd", source = "accountNumber")
    @Mapping(target = "logo", source = "logo") // not needed as same name, delete it
    @Mapping(target = "address", source = "address") // not needed as same name, delete it
    CsvBusinessData asDto(CustomerEntity customer);

    @InheritInverseConfiguration
    CustomerEntity asCustomerEntity(CsvBusinessData customer);
}

@Mapper( unmappedTargetPolicy = ReportingPolicy.IGNORE)
public interface CustomerMapper {
    @Mapping(target="streetName", source="streetName") // not needed as same name, delete it
    @Mapping(target="streetNumber", source="streetNumber") // not needed as same name, delete it
    @Mapping(target="block", source="block") // not needed as same name, delete it
    @Mapping(target="floor", source="floor") // not needed as same name, delete it
    CsvBusinessData asDto(AddressEntity address);
}

2)您的对象不相关:您可以通过传递少量对象作为参数来解决您的问题。但是对于相反的方向,您必须有自己的方法。如果您在AddressEntity和CustomerEntity中有相同的字段名称,那么只需将其指定address.street名称或customer.last名称

@Mapping(target="streetName", source="streetName")// possibly not needed as same name
@Mapping(target="streetNumber", source="streetNumber")// possibly not needed as same name
@Mapping(target="block", source="block")// possibly not needed as same name
@Mapping(target="floor", source="floor")// possibly not needed as same name
@Mapping(target = "nom", source = "lastName")
@Mapping(target = "nd", source = "accountNumber")
@Mapping(target = "logo", source = "logo")// possibly not needed as same name
@Mapping(target = "address", source = "customer.address") // specified as you have parameter address
CsvBusinessData asDto(AddressEntity address, CustomerEntity customer);

@Mapping(target="streetName", source="streetName")// possibly not needed as same name
@Mapping(target="streetNumber", source="streetNumber")// possibly not needed as same name
@Mapping(target="block", source="block")// possibly not needed as same name
@Mapping(target="floor", source="floor")// possibly not needed as same name
AddressEntity asAddressEntity(CsvBusinessData businessData);

@Mapping(source = "nom", target = "lastName")
@Mapping(source = "nd", target = "accountNumber")
@Mapping(source = "logo", target = "logo")// possibly not needed as same name
@Mapping(source = "address", target = "address") // specified as you have parameter address
CustomerEntity asCustomerEntity(CsvBusinessData businessData);
 类似资料:
  • 下面是我的DTO。 源DTO 目标DTO

  • 我创建映射如下所示。如何将平面dto对象属性(街道、城市等)映射到域对象中的嵌套地址。当我试着去做的时候,我发现了一个错误: [错误]诊断:返回类型中的属性“Address.PostalCode”未知。@Mapping(来源=“City”,目标=“Address.City”), 还有类...

  • 我有Object1和Object2。现在,我想映射对象3,属性来自1 比方说,我有两个目标: 现在,有了这些,我想把它映射进去 哪里,first_name 现在,我的问题是,怎么做? 然而,目前,我正在这样做 但是,在这里,我必须在addressToView()中手动编写映射。 因此,有没有办法避免这种情况? 或者,处理此类情况的首选方式是什么?

  • 问题内容: 如果名称和名称相同,默认情况下MapStruct将映射源和目标的所有属性。尽管我们可以在@Mapping中使用ignore属性来省略任何字段映射。但这不是我想要的。我想控制映射策略。我必须指定类似 现在此映射仅用于将ID和名称从源映射到目标。除非在映射注释中指定,否则不应映射其他字段。 问题答案: 您正在寻找的是#1392中 的功能请求。有一个待定的PR,因此可以在下一版本(1.3.0

  • 我试图使用MapStruct在dto和实体对象之间映射convert,但是生成的映射器实现只返回空的映射对象。 BeermapperImpl 任何人都可以提供我的代码可能出错的地方?谢谢!

  • 我想映射2个模型,其中每个模型都有几乎相同的枚举。让我展示: 第一个模型有枚举: 第二个模型具有枚举: 我有这样的自定义映射方法: 然后我用: 但是你可以得到: 我还创建了枚举映射器,如: 但我不需要单独设置,只希望枚举字段映射到内部映射中。简单地说,当我做枚举时,也应该映射。 谢谢 p、 对不起我的英语,希望我的问题有意义:)