写法一
// ----- first
/**
* Use Context annotation and expression set id
*
* @param sourceDO
* @param id
* @return
*/
@Mapping( target = "id", expression = "java(id)")
TargetDTO convert(SourceDO sourceDO, @Context Long id);
/**
* convert source list to target list
*
* @param sourceDO
* @param id
* @return
*/
List<TargetDTO> convert(List<SourceDO> sourceDO, @Context Long id);
// ----- first end
写法二
// second
/**
* Use Context AfterMapping annotations set id
* first ignore id true
*
* @param sourceDO
* @param context
* @return
*/
@Mapping( target = "id", ignore = true)
TargetDTO convert(SourceDO sourceDO, @Context IdentityContext context);
/**
* convert source list to target list
*
* @param sourceDO
* @param context
* @return
*/
List<TargetDTO> convert(List<SourceDO> sourceDO, @Context IdentityContext context);
// second end
源码可以参考: mapstruct-examples/mapstruct-mapping-list at master · win32fk/mapstruct-examples · GitHub