Conditional mapping and default mapping principles in the MapStruct Core framework
Mapstruct is a Java annotation processor that is used to perform object mapping between two Java Bean.It provides a simple and type secure way to convert complex Java objects.
In the MapStruct Core frame, conditional mapping and default mapping are two important concepts.Condition mapping allows selectively mapping the attribute values according to the given condition, and the default mapping defines the default mapping behavior that should be used on the unable to mappore attribute fields.
Condition mapping can use the `@Mapping` annotation of the map object.For example, consider a Person class that contains a attribute Age and an attribute Agegroup:
public class Person {
private int age;
private String ageGroup;
// Getters and setters
}
public class PersonDto {
private int age;
private String ageGroup;
// Getters and setters
}
We hope to determine the value of the Agegroup property according to the value of the age attribute.This goal can be achieved by using@valuemapping` in the mapping method:
@Mapper
public interface PersonMapper {
@Mapping(target = "ageGroup", source = "age")
@ValueMappings({
@ValueMapping(source = "0", target = "Baby"),
@ValueMapping(source = "10", target = "Child"),
@ValueMapping(source = "18", target = "Adult")
})
PersonDto personToPersonDto(Person person);
}
In the above example, when the value of the Age attribute is 0, the Agegroup attribute will be mapped as "baby"; when the value of the Age attribute is 10, the Agegroup attribute will be mapped as "child"; when the value of the Age attribute is 18 is 18At that time, the Agegroup attribute will be mapped as "adult".
The default mapping is independent of conditional mapping.The default mapping specifies the default mapping behavior used on the attribute field that is not mapping in the target object.The default mapping can be specified using the `defaultValue` attribute of`@mapping`.For example:
@Mapper
public interface PersonMapper {
@Mapping(target = "ageGroup", source = "age", defaultValue = "Unknown")
PersonDto personToPersonDto(Person person);
}
In the above example, if the Agegroup attribute is not specified for any mapping, and when the mapping method is called, the Agegroup property will be set to "UNKNOWN".
In order to make the above -mentioned mapping work, MapStruct needs to be added as a plug -in to the construction configuration file of the project.For example, if you use Maven to build a project, you need to add the following dependencies to the pom.xml file:
<dependencies>
<dependency>
<groupId>org.mapstruct</groupId>
<artifactId>mapstruct</artifactId>
<version>1.4.2.Final</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.mapstruct</groupId>
<artifactId>mapstruct-maven-plugin</artifactId>
<version>1.4.2.Final</version>
<executions>
<execution>
<id>mapstruct-processor</id>
<phase>generate-sources</phase>
<goals>
<goal>compile</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
The above configuration will automatically generate the mapper implementation class when compiling the MapStruct annotation processor.
In summary, the conditional mapping and default mapping in the Mapstruct Core framework provides a flexible and powerful way to define the attribute mapping relationship between complex objects.By rationally utilizing these characteristics, you can simplify the transformation process and improve the readability and maintenance of the code.