Small knowledge, big challenge! This article is participating in the creation activity of “Essential Tips for Programmers”.

We have already seen a brief introduction to MapStruct from the official documentation. In this article, we will start with an example and walk you through its use.

Simple implementation

We note that there is a simple sample implementation on the official website, and we will analyze a wave in 2 minutes:

1. Introduce dependencies

< the dependency > < groupId > org. Mapstruct < / groupId > < artifactId > mapstruct - jdk8 < / artifactId > < version > 1.3.0. The Final < / version > </dependency> // Annotation processor, <dependency> <groupId>org.mapstruct</groupId> <artifactId>mapstruct-processor</artifactId> < version > 1.2.0. Final < / version > < / dependency >Copy the code

Java: No property named “numberOfSeats” exists in source parameter(s). Did you mean “null”? Mapstruct-processor: 1.2.0.final, Lombok: 1.16.14.

2. Prepare entity classesCar.javaAnd data transfer classesCarDto.java

@NoArgsConstructor
@AllArgsConstructor
@Data
public class Car {
    private String make;
    private int numberOfSeats;
    private CarType type;
}

@Data
@NoArgsConstructor
@AllArgsConstructor
public class CarDto {
    private String make;
    private int seatCount;
    private String type;

}
Copy the code

3. Create a mapper interface to define mapping methods

@Mapper
public interface CarMapper {
 
    CarMapper INSTANCE = Mappers.getMapper( CarMapper.class );

    @Mapping(source = "numberOfSeats", target = "seatCount")
    CarDto carToCarDto(Car car); 
   
}
Copy the code

Analysis:

  • @MapperMark the interface as a mapping interface and allowMapStructThe processor starts during compilation. Here,@MapperAnnotations are notmybatisNotes, butorg.mapstruct.Mapper;
  • Actual mapping methodcarToCarDto()Expected source objectCarAs an argument and returns the target objectCarDto, the method name can be chosen freely;
  • For the source object and the target objectDifferent namesProperty, can be used@MappingComment to configure the name;
  • For the source object and the target objectDifferent types ofProperty, can also be used@MappingAnnotation to convert, for example, a type attribute from an enumeration type to a string;
  • There can be multiple mapping methods in an interface, and for all of them,MapStructAn implementation will be generated;
  • An implementation instance of this interface can be obtained fromMappersThe interface declares oneINSTANCETo provide the client with access to the mapper implementation.

4. The implementation class

We can compile the code, and we’ll find that intargetGenerated in the fileCarMapperImpl.classFile:

As you can see from the code, MapStruct automatically generates the set/ GET code for us and does special treatment for the enumeration classes.

5. The client

@Test
public void shouldMapCarToDto(a) {

    Car car = new Car( "Morris".5, CarType.SEDAN );
    CarDto carDto = CarMapper.INSTANCE.carToCarDto( car );
    System.out.println(carDto);
    
}
Copy the code

Execution Result:

Summary: MapStruct is based on mapper interface, dynamically generates a class file of set/ GET code at compile time, and calls this class file directly at run time.

Have you mastered this simple case? Here we take a closer look at his use! If you have different opinions or better idea, welcome to contact AH Q, add AH Q can join the technical exchange group to participate in the discussion!