3.2Adding Custom Methods to Mappers (add custom method in Mapper)

Source: Internet
Author: User

3.2Adding Custom Methods to Mappers (add custom method in Mapper)

In some cases, we need to implement some mapping between complex types that mapstruct cannot directly generate automatically. One way is to reuse other implemented mappings. Alternatively, when using Java8 or later, you can implement a custom method directly in the Mapper interface as the default method, and if the parameter and return type match, the generated code will call the default method.

For example, join us to map person to persondto, because there are some special logic involved, mapstruct cannot directly generate the mapping code, you can define the mapper from the previous example as follows:

Example 8. Mapper which defines a custom mapping with a default method

@Mapperpublic interface CarMapper {    @Mappings({...})    CarDto carToCarDto(Car car);    default PersonDto personToPersonDto(Person person) {        //hand-written mapping logic    }}

The implementation method generated by Mapstruct cartocardto (), when the driver attribute is mapped, the code generated in Cartocardto () calls the Persontopersondto () method that is implemented manually.

The mapper can also be defined in the form of an abstract class rather than an interface, and implement a custom method directly in the Mapper class. In this case, MAPSTRUCT will generate an extension of the abstract class with all the abstract method implementations. The advantage of this method in declaring the default method is that other fields can be declared in the Mapper class.

In the previous example, the mapping from person to persondto requires some special logic, which can be defined as well:

Example 9. Mapper defined by an abstract class

@Mapperpublic abstract class CarMapper {    @Mappings(...)    public abstract CarDto carToCarDto(Car car);    public PersonDto personToPersonDto(Person person) {        //hand-written mapping logic    }}

Mapstruct will generate a subclass of Carmapper and implement the Cartocardto () method in the case of the Declaration as abstract. When the driver attribute is mapped, the code generated in Cartocardto () invokes the manually implemented Persontopersondto () method.

3.2Adding Custom Methods to Mappers (add custom method in Mapper)

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.