MapStruct - Java Mapper

MapStruct is a Java Bean Mapper.

It contains functions that automatically maps between two Java functions. We only need to create the interface, and the library will automatically create a concrete implementation.



pom.xml modifications for maven


We also need to configure the compiler-plugin to generate the sources on built time.

 <!-- Sets the compiler version -->
	<!-- config for MapStruct -->


We have a Person.class

public class Person {
    private Long id;
    private Integer age;
    private String name;

And a PersonDto.class without id

public class PersonDto {
    private Integer age;
    private String name;

In order for MapStruct to work, we need to create a mapper interface, but we don’t need to implement it. The implementation will be created at compile time.

public interface PersonMapper {
    Person personDtoToPerson(PersonDto personDto);
    PersonDto personToPersonDto(Person person);

If we do a mvn clean install it will generate a class called PersonMapperImpl.class that contains the generated code.

(lo dejo en 4.4. A Test Case)