In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-19 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/03 Report--
This article mainly explains "how to use Mapstruct". The content in the article is simple and clear, and it is easy to learn and understand. Please follow the editor's train of thought to study and learn how to use Mapstruct.
In Java, there are three ways to handle these attribute copies:
Hard-code directly and hard-code the code.
Use a variety of BeanUtils to complete the assignment through reflection
Use a tool like MapStruct to do it directly at compile time
1. How to use it?
As usual, you need to add dependency packages to pom, and we are using the 1.4.1.Final version here.
Org.mapstruct mapstruct ${org.mapstruct.version}
This is not over, but you need to add a plug-in to the build section of pom. It is so complicated because its principle is the same as that of lombok, which is also implemented in the compiler through APT.
This means that the code is completed at compile time. No reflection is needed, so the efficiency is the same as writing get and set directly.
Org.apache.maven.plugins maven-compiler-plugin 3.8.1 1.8 1.8 org.mapstruct mapstruct-processor ${org.mapstruct.version} org.projectlombok lombok 1.18.16 org.projectlombok lombok-mapstruct-binding 0.2.0
At this point, we can use the annotations it provides to easily copy the attributes.
@ Mapper (nullValueCheckStrategy = NullValueCheckStrategy.ALWAYS) public interface Transform {Transform T = Mappers.getMapper (Transform.class); Member fromMemberEntity (MemberEntity entity); MemberEntity fromMember (Member member);}
The above is a sample code. The Mapper annotation indicates that this is a type conversion tool (object mapper) that provides a number of strategies for us to choose from. Directly write the interface file, and do not need to do some extra action, mapstruct will know what you want to do!
In traditional programming, if Member has a lot of properties, we need to do this process manually, and the code will be very much.
After using Mapperstruct, this part of the repetitive work, tools have been done for us.
Look at the picture below!
The above figure shows that the code is generated in the generated-source directory under target, which is the work of the plug-in we added above; the content of the code is actually some non-empty judgment and get, set, and so on. Properties of the same field name and type will be copied indifferently.
If you have a lot of bean attributes, this tool will change your code from hundreds of lines to several lines!
two。 Compare with other ways
Does mapstruct have any advantages? Why not just use BeanUtils? They have the same effect, and the latter are provided by various class libraries.
The main reason is the problem of efficiency.
BeanUtils is implemented through reflection and must be inefficient, while mapstuct is implemented based on APT with no performance loss.
The attribute copy of BeanUtils has a lot of obstacles when judging null values and different types of attributes, while mapstruct has very flexible strategies and transformation methods and is more customized (discussed later).
3. Complex scene
Then let's take a look at a complicated scene.
If you have only a few common attributes in your bean, then using mapstruct is silky and smooth. However, there are always some exceptions that require more advanced handling.
Suppose I want to convert from Unit to ProductUnitEntity, but there is a field called measureType that is of a different type, so we can use the Mappings annotation to do this conversion.
@ Mappings ({@ Mapping (source = "measureType.value", target = "measureType")}) ProductUnitEntity fromUnit (Unit v)
The compiled code is as follows. With source and target, you can achieve more awesome behavior than BeanUtils. You can even do some date conversion through dateFormat.
In fact, the measureType above is an enumerated type. How do you convert a normal type to an enumerated type? We only need to provide a default method to ok. Mapstruct determines the parameter type and return value, so the name of the method can be any legal value.
Default Unit.MeasureType measureTypeIntegerToDomain (Integer value) {for (Unit.MeasureType s: Unit.MeasureType.values ()) {if (s.getValue () = = value) {return s;}} return null;}
Can mapstruct achieve the conversion between List? It's okay, too. The following two lines of code can automatically supplement the for loop to make your code more concise.
List fromSkuEntityList (List v); List fromSkuList (List v)
End, so here's the problem.
Since such a good thing, then why many projects now, do not use mapstruct, or even BeanUtils, directly manual where the get, set?
One reason is that these tools significantly reduce the amount of code. Mapstruct+hibernate-validate, one for conversion and the other for verification, is a nightmare for companies around the world based on the number of lines of code. Performance will go down!
Another reason is that the use of these tools is not conducive to project refactoring. If you change the a field to the b field in DTO, mapstruct lovingly ignores these changes for you. Your project code will not indicate errors, and the risk will go directly to the runtime.
With get and set, the only risk is that the developer forgets to assign a value to a new field, except that the code changes a lot.
In this case, the work done by machines is not necessarily more reliable than that of humans. So there is a big premise for using mapstruct: your team can agree not to name variables and refactoring. Only in this way can its value be brought into full play.
Thank you for your reading, the above is the content of "how to use Mapstruct", after the study of this article, I believe you have a deeper understanding of how to use Mapstruct, and the specific use needs to be verified in practice. Here is, the editor will push for you more related knowledge points of the article, welcome to follow!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.