MapStruct Plus is an enhancement tool of MapStruct. Based on Mapstruct, it implements the function of automatically generating Mapper interfaces and strengthens some functions to make Java type conversion more convenient and elegant.
MapStructPlus official website
This time we ushered in a larger version update, the update log is as follows:
- Optimize complex object conversion logic and occupy less meta space! Faster performance!
This version has been optimized and partially redesigned for the automatic conversion logic of complex objects, reducing some unnecessary conversion methods and occupying less meta space.
Taking RuoYi-Vue-Plus as an example, after using the new version, the metaspace usage has been reduced by 7MB. The reduced memory usage varies depending on the complexity of the project.
And according to issue #67 , in the SpringBoot + Aop scenario, the performance will be affected. In the new version, the implementation method has been modified, and the performance ratio has increased by more than half.
- Remove dependencies such as hugool. Currently, the project only relies on MapStruct.
Since version 1.4.0, dependencies other than MapStruct have been removed, and the packaged size is smaller.
But Map
when you need to use and object conversion, you need to introduce additional hutool-core
dependency packages.
- Adaptation object loop nested scene
Class loop nesting is when two classes refer to each other, for example, the source and target object structures both contain bidirectional associations between parent and child objects. When this situation exists, direct conversion will cause a stack overflow error.
Example:
@Data
public class TreeNode {
private TreeNode parent;
private List<TreeNode> children;
}
@Data
public class TreeNodeDto {
private TreeNodeDto parent;
private List<TreeNodeDto> children;
}
parent
Properties can be of other types, possibly spanning a longer chain of properties forming a nested loop.
In order to adapt to this situation, the attribute AutoMapper
is added to the annotation of MapStructPlus , which is used to identify whether the problem of loop nesting needs to be avoided. cycleAvoiding
The default is false
, if you need to avoid loop nesting, you need to set this property to true
.
When configured as true
, in the entire object conversion process link, an CycleAvoidingMappingContext
object will be passed to temporarily save the object generated by the conversion. In the conversion link, if it is found that the object that needs to be generated already exists, the type will be returned directly, thus avoiding Stack overflow problem.
Taking the above example as an example, in AutoMapper
the annotation, the configuration cycleAvoiding
attribute is true
as follows:
@Data
@AutoMapper(target = TreeNodeDto.class, cycleAvoiding = true)
public class TreeNode {
private TreeNode parent;
private List<TreeNode> children;
}
@Data
@AutoMapper(target = TreeNode.class, cycleAvoiding = true)
public class TreeNodeDto {
private TreeNodeDto parent;
private List<TreeNodeDto> children;
}
The conversion logic generated by compilation is as follows:
public TreeNodeDto convert(TreeNode arg0, CycleAvoidingMappingContext arg1) {
TreeNodeDto target = arg1.getMappedInstance(arg0, TreeNodeDto.class);
if (target != null) {
return target;
}
if (arg0 == null) {
return null;
}
TreeNodeDto treeNodeDto = new TreeNodeDto();
arg1.storeMappedInstance(arg0, treeNodeDto);
treeNodeDto.setParent(demoConvertMapperAdapterForCycleAvoiding.iglm_TreeNodeToTreeNodeDto(arg0.getParent(), arg1));
treeNodeDto.setChildren(
demoConvertMapperAdapterForCycleAvoiding.iglm_TreeNodeToTreeNodeDto(arg0.getChildren(), arg1));
return treeNodeDto;
}
AutoMapping
,ReverseAutoMapping
supportqualifiedByName
,conditionQualifiedByName
anddependsOn
propertiesAutoMappings
Support configuration on the method