For Modelmapper, the main implementation is the assignment of objects and objects, in the micro-service data transmission objects used more, dto This object is abstracted from the business model, to meet a certain kind of business, it is not related to the data persistence model, and if we take the data table model out, When a DTO is returned to the end of use, an assignment mechanism is needed, which is what modelmapper needs to do.
In the current development process, because of the use of Lombok plug-ins, so that we have reduced a lot of duplicated code, it is in the form of annotations in the code, that is, @getter represents the foreign company reading methods, @setter the external public writing method, the latter in the design of the general public, The assignment of an object is generally done through @builder, but this is a bad thing for modelmapper, it must require you to publicly @setter annotations before you can assign a value, which is conceivable, modelmapper mechanism should mean to take all the fields, It is then assigned a value using its own setter method.
Note Address :
- @setter annotations must be disclosed
- A Dto object cannot inherit the original model, and a simple entity should be created because annotations are the ones that can be integrated from the parent class.
true ) @Getter @setter@allargsconstructor@noargsconstructorpublicclass userdto { Private String name;}
Here is the data model
true ) @Getter @noargsconstructor@allargsconstructorpublicclass UserInfo { Private String name; Private String email; = "Amount cannot be less than 0." = ten, message = "Amount cannot be greater than 10." Private Money price ;}
Here's how to use it, and the method of using the Beanutils class can be implemented as well.
Modelmapper method
New Modelmapper (); = Modelmapper.map (UserInfo, userdto. Class
Beanutils method
UserInfo userinfo= Userinfo.builder (). Name ("Zzl"). Build (); Userdto userdto=new userdto (); Beanutils.copyproperties (userinfo,userdto);
Thanks for reading!
Some points needing attention in Java~modelmapper