Some points needing attention in Java~modelmapper

Source: Internet
Author: User

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 :

    1. @setter annotations must be disclosed
    2. 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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.