java MapStruct:将 2 个对象映射到第 3 个对象

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/34130599/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-11-02 22:30:53  来源:igfitidea点击:

MapStruct: Mapping 2 objects to a 3rd one

javaobject-object-mappingmapstruct

提问by anij

I have Object1 and Object2. Now, I want to map object3, with attributes from 1 & 2.

我有 Object1 和 Object2。现在,我想用 1 和 2 的属性映射 object3。

Say, I have 2 object:

说,我有 2 个对象:

1. User: {first_name, last_name, id}
2. Address: {street, locality, city, state, pin, id}

Now, with these, I want to map that in

现在,有了这些,我想将其映射到

User_View: {firstName, lastName, city, state}.

Where, first_name & last_name will be from User object and city & state from Address object.

其中,名字和姓氏将来自用户对象,城市和州来自地址对象。

Now, my question is, how to do that?

现在,我的问题是,如何做到这一点?

However, currently, I'm doing like this

但是,目前,我正在这样做

@Mapper    
public abstract class UserViewMapper {
        @Mappings({
                    @Mapping(source = "first_name", target = "firstName"),
                    @Mapping(source = "last_name", target = "lastName"),
                    @Mapping(target = "city", ignore = true),
                    @Mapping(target = "state", ignore = true)

            })
            public abstract UserView userToView(User user);

        public UserView addressToView(UserView userView, Address address) {

                if (userView == null) {
                    return null;
                }

                if (address == null) {
                    return null;
                }

                userView.setCity(address.getCity());
                userView.setState(address.getState()); 

            return userView;

            }
    }

But, here, I have to manually write the mapping in addressToView().

但是,在这里,我必须手动编写addressToView().

Therefore, is there, any way, to avoid that?

因此,有没有办法避免这种情况?

Or, what would be the preferred way, to handle such situations?

或者,处理这种情况的首选方法是什么?

回答by Gunnar

You can declare a mapping method with several source parameters and refer to the properties of all these parameters in your @Mappingannotations:

您可以使用多个源参数声明映射方法,并在@Mapping注释中引用所有这些参数的属性:

@Mapper
public abstract class UserViewMapper {

    @Mapping(source = "first_name", target = "user.firstName"),
    @Mapping(source = "last_name", target = "user.lastName"),
    public abstract UserView userAndAddressToView(User user, Address address);
}

As the "city" and "state" property names match in source and target, there is no need for mapping them.

由于“城市”和“州”属性名称在源和目标中匹配,因此无需映射它们。

Also see the chapter "Defining a mapper"in the reference documentation for more details.

有关更多详细信息,另请参阅参考文档中的“定义映射器”一章。

回答by Mike Murphy

Using MapStruct you are missing a step using the @Mapper annotation. The @Mapper will create the implementation of the mappings.

使用 MapStruct,您缺少使用 @Mapper 注释的步骤。@Mapper 将创建映射的实现。

You should review the docs at this link http://mapstruct.org/documentation/stable/reference/html/

您应该在此链接http://mapstruct.org/documentation/stable/reference/html/查看文档

Specifically

具体来说

  1. Defining a mapper

In this section you'll learn how to define a bean mapper with MapStruct and which options you have to do so. 3.1 Basic mappings

To create a mapper simply define a Java interface with the required mapping method(s) and annotate it with the org.mapstruct.Mapper annotation:

@Mapper
public interface CarMapper {

    @Mappings({
        @Mapping(source = "make", target = "manufacturer"),
        @Mapping(source = "numberOfSeats", target = "seatCount")
    })
    CarDto carToCarDto(Car car);

    @Mapping(source = "name", target = "fullName")
    PersonDto personToPersonDto(Person person);
}

The @Mapper annotation causes the MapStruct code generator to create an implementation of the CarMapper interface during build-time.

  1. 定义映射器

在本节中,您将学习如何使用 MapStruct 定义 bean 映射器以及您必须这样做的选项。3.1 基本映射

要创建映射器,只需使用所需的映射方法定义一个 Java 接口,并使用 org.mapstruct.Mapper 注释对其进行注释:

@Mapper
public interface CarMapper {

    @Mappings({
        @Mapping(source = "make", target = "manufacturer"),
        @Mapping(source = "numberOfSeats", target = "seatCount")
    })
    CarDto carToCarDto(Car car);

    @Mapping(source = "name", target = "fullName")
    PersonDto personToPersonDto(Person person);
}

@Mapper 注释使 MapStruct 代码生成器在构建时创建 CarMapper 接口的实现。