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.

  1. 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.
  2. 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,MapStructLooks for matching parameter names;
  • When used to map enumeration constants, the name of the constant member is given;
  • The property cannot matchconstantexpressionUse together;
  1. The dateFormat:SimpleDateFormatimplementationStringDateDates convert to each other.
  2. NumberFormat:DecimalFormatimplementationNumberStringFormat the value of.
  3. Constant: Sets the constant string for the specified target property when the specified target property is of type:primitiveboxed(e.g.Long),MapStructCheck to see if theprimitiveAssigned to as valid textprimitiveboxedType. If possible,MapStructAssign to text; If that’s not possible,MapStructA 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!