Small knowledge, big challenge! This article is participating in the creation activity of “Essential Tips for Programmers”.
In the previous article we looked at the use of @Mapper and used a small example to demonstrate the use of custom transformation classes. In this article we will demonstrate the @Mapping annotation and the Expression attribute.
@Mapping
@Mapping can be used to configure the Mapping of a bean property or enumeration constant. The default is to map a property with the same name. You can also specify this manually using the source, expression, or constant property.
- Target: The target name of the property. The same target property cannot be mapped more than once. If used to map enumeration constants, the name of the constant member is given, in which case multiple values in the source enumeration can be mapped to the same value in the target enumeration.
- Source: The source name of the property,
- If the annotated method has more than one source parameter, the property name must be qualified with the parameter name, for example
"AddressParam. City"
; - When no matching attribute is found,
MapStruct
Looks for matching parameter names; - When used to map enumeration constants, the name of the constant member is given;
- The property cannot match
constant
或expression
Use together;
- The dateFormat:
SimpleDateFormat
implementationString
到Date
Dates convert to each other. - NumberFormat:
DecimalFormat
implementationNumber
与String
Format the value of. - Constant: Sets the constant string for the specified target property when the specified target property is of type:
primitive
或boxed
(e.g.Long
),MapStruct
Check to see if theprimitive
Assigned to as valid textprimitive
或boxed
Type. If possible,MapStruct
Assign to text; If that’s not possible,MapStruct
A user-defined mapping method will be attempted.
In addition, MapStruct treats constants as strings and converts the value by applying matching methods, type conversion methods, or built-in conversions. This property cannot be used with source, defaultValue, defaultExpression, or expression. 6. Expression: Is an expression that sets the specified target property. Its attributes cannot be used with source, defaultValue, defaultExpression, or constant. 7. Ignore: Ignore this field.
We use the expression property to implement the above uses example:
1. Define methods in mapper
@Mapping(target = "type", expression = "java(new com.ittest.controller.BooleanStrFormat().toStr(carVo.isType()))")
CarDto carVoToDtoWithExpression(CarVo carVo);
Copy the code
2. Generated implementation classes
@Override
public CarDto carVoToDtoWithExpression(CarVo carVo) {
if ( carVo == null ) {
return null;
}
CarDto carDto = new CarDto();
carDto.setMake( carVo.getMake() );
carDto.setSeatCount( carVo.getSeatCount() );
carDto.setType( new com.ittest.controller.BooleanStrFormat().toStr(carVo.isType()) );
return carDto;
}
Copy the code
3. The client
@Test
public void mapCarVoToDtoWithExpression(a) {
CarVo carVo = new CarVo( "Morris".5.false );
CarDto carDto = CarMapper.INSTANCE.carVoToDtoWithExpression( carVo );
System.out.println(carDto);
}
Copy the code
Running results:
Important: Enumeration mapping has been deprecated and replaced by ValueMapping. It will be removed in a later release.
There are other uses that you can explore. 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!