3.3. Mapping methods with several source parameters (mapping method with multiple source parameters)

Source: Internet
Author: User

3.3. Mapping methods with several source parameters (mapping method with multiple source parameters)

Mapstruct also supports mapping methods with multiple source parameters. This is useful, for example: in order to merge multiple entities into one data transfer object. An example is shown below:

Example 10. Mapping method with several source parameters

@Mapperpublic interface AddressMapper {    @Mappings({        @Mapping(source = "person.description", target = "description"),        @Mapping(source = "address.houseNo", target = "houseNumber")    })    DeliveryAddressDto personAndAddressToDeliveryAddressDto(Person person, Address address);}

The mapping method shown takes two source parameters and returns a combined target object. As with the single-parameter mapping method, properties are mapped by name.

If more than one source object defines a property with the same name, you must use @Mapping annotation to specify the source parameter from which you want to retrieve the property, as shown in the Description property in the example. If such ambiguity is not resolved, the code will report an error. For a property that exists only once in a given source object, you can optionally specify the name of the source parameter, because it can be determined automatically.

Note: When using @Mapping annotations, it is necessary to determine which parameter the attribute is specific to.

If all the source parameters are null, the mapping method with more than one source parameter returns NULL. Otherwise, the target object is instantiated and all properties in the supplied parameters are propagated.

Mapstruct also provides the possibility of referencing source parameters directly.

Example 11. Mapping method directly referring to a source parameter

@Mapperpublic interface AddressMapper {    @Mappings({        @Mapping(source = "person.description", target = "description"),        @Mapping(source = "hn", target = "houseNumber")    })    DeliveryAddressDto personAndAddressToDeliveryAddressDto(Person person, Integer hn);}

In this case, the source parameter is mapped directly to the target object. Parameter: HN, which is a non-bean type (Java.lang.Integer in this example) is mapped to the housenumber of Deliveryaddressdto.

3.4. Updating existing bean instances (updating an existing bean instance)

In some cases, your mapping does not need to create a new instance of the target type, but instead updates an existing instance of that type. You can implement this type of mapping by adding parameters to the target object and marking the parameter with @MappingTarget. The following example:

Example 12. Update method

@Mapperpublic interface CarMapper {    void updateCarFromDto(CarDto carDto, @MappingTarget Car car);}

After compiling the generated method Updatecarfromdto (), the target object car will be updated with the property values of the parameter cardto. In addition to void, you can also set the return type of a method to the type of the target parameter, so that the resulting implementation class will update and return the target object in the entry parameter. This approach allows for smooth invocation of the mapping method.

If the property of the target object being updated is a collection or a map type, the updated property is emptied, and the collection properties in the incoming parameter are populated to the emptied target property.

3.3. Mapping methods with several source parameters (mapping method with multiple source parameters)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.