Section III mapstruct translation--defining a mapper

Source: Internet
Author: User

Section III mapstruct--defining a mapper

In this section you will learn how to define a bean mapper with mapstruct and some of its necessary operational options.

3.1 Basic Mappings

Create a Java interface class that uses the necessary methods and annotations to create a simple mapper with annotations to the Org.mapstruct.Mapper package:

Example 6:

@Mapperpublic 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 annotations can cause the Mapstruct code generator to create an implementation class for the Carmapper interface at compile time.

In the resulting implementation method, all the readable attribute fields are read from the car class source, copied and bound to the target class cardto. If there is a difference in the field variable name between the equivalence attribute (class source--target Class), we can use the @mapper annotation to customize the designation.

To get a better understanding of what Mapstruct has done, see the following example:

Example 7. Mapstruct generated code (CARTOCARDTO)

GENERATED Codepublic class Carmapperimpl implements Carmapper {@Override public cardto cartocardto (car car) {I    F (car = = null) {return null;    } cardto cardto = new Cardto ();    if (car.getfeatures () = null) {cardto.setfeatures (New arraylist<string> (Car.getfeatures ()));    } cardto.setmanufacturer (Car.getmake ());    Cardto.setseatcount (Car.getnumberofseats ());    Cardto.setdriver (Persontopersondto (Car.getdriver ()));    Cardto.setprice (String.valueof (Car.getprice ()));    if (car.getcategory () = null) {cardto.setcategory (Car.getcategory (). toString ());    } cardto.setengine (Enginettoenginedto (Car.getengine ()));  return cardto;    } @Override Public persondto persontopersondto (person person) {//...}    Private enginedto Enginetoenginedto (engine engine) {if (engine = = NULL) {return null;    } enginedto enginedto = new Enginedto ();    Enginedto.sethorsepower (Engine.gethorsepower ()); EnginEdto.setfuel (Engine.getfuel ());  return enginedto; }}

Typically mapstruct's code generation strategy is to view as many of the user's custom policies as possible. In particular, this means that all of these values will be scheduled to be copied from the source class to the target class through the Getter/seter method, rather than through reflection or otherwise.

As you can see from the example, all the mappings specified by annotations are mapstruct to the code generation. If the type of the mapped property is different in the source and target entities, mapstruct either applies the auto-transform or optionally invokes/creates another mapping method. Mapstruct creates a new mapping when and only if the source and target properties are simple bean properties (that is, the variable type is not a collection or dictionary type).

The MAPSTRUCT mapping policy includes all public properties of the source and destination types, as well as attributes declared on the parent class.

3.2. Adding custom methods to Mappers (add Customization method in mapper)

Added later

Section III mapstruct translation--defining a 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.