Assignment between entity objects -- use of BeanUtils

Posted by 1042 on Sat, 29 Jan 2022 20:31:54 +0100

Entity objects transfer values to each other. For example, assigning the value of VO object to entity object is a common function in the code. It is troublesome to assign values to each other through get and set. The operation can be easily completed with the help of tool class BeanUtils.

BeanUtils dependent package import

BeanUtils is a member of the Apache commons component, which is mainly used to simplify the operation of JavaBean s encapsulating data. To use BeanUtils, you must import the corresponding jar package. The maven coordinate of BeanUtils is

<dependency>
    <groupId>commons-beanutils</groupId>
    <artifactId>commons-beanutils</artifactId>
    <version>1.9.4</version>
</dependency>

Examples

Assign the student ranking information (StudentVo object) from the front end to the student object (StudentEntity) and ranking object (RankingEntity) respectively. The codes of these three classes are as follows:

@Data
public class StudentVo {
    private String sno;
    private String sname;
    private Integer ranking;
    private String schoolTerm;

    public String toString(){
        return "studentVo The value of the object  sno:"+getSno()+" sname:"+getSname()+" ranking:"+getRanking().toString()+" schoolTerm:"+getSchoolTerm();
    }
}

@Data
public class StudentEntity {
    private String sno;
    private String sname;
    private Integer sage;

    public String toString(){
        return "studentEntity The value of the object sno:"+getSno()+" sname:"+getSname()+" sage:"+getSage();
    }
}

@Data
public class RankingEntity {
    private String sno;
    private Integer ranking;
    private String schoolTerm;

    public String toString(){
        return "rankingEntity Value student number of object:"+getSno()+" Ranking:"+getRanking().toString()+" semester:"+getSchoolTerm();
    }
}

Assign the value of VO object to entity object through BeanUtils Copyproperties (target, source): assign the data of the source entity object to the target object, and only assign the data with the same property name. If the property in the target does not exist in the source, give null value. The test code is as follows:

public class App {
    public static void main( String[] args ) throws InvocationTargetException, IllegalAccessException {
        StudentVo studentVo = new StudentVo();
        studentVo.setSno("1");
        studentVo.setRanking(20);
        studentVo.setSname("Hu Cheng");
        studentVo.setSchoolTerm("The third semester");

        System.out.println(studentVo.toString());
        StudentEntity studentEntity = new StudentEntity();

        BeanUtils.copyProperties(studentEntity,studentVo);
        System.out.println(studentEntity.toString());

        RankingEntity rankingEntity = new RankingEntity();
        BeanUtils.copyProperties(rankingEntity,studentVo);
        System.out.println(rankingEntity.toString());
    }
}

Operation results:

Ending

Tip: due to the limitation of the length of the article, there are 20 questions about MySQL below. I will redo and sort them into a pdf document. I will show you the catalogue of the remaining questions later, Click here to unlock all contents!

If you think it's helpful, you might as well [forward + like + follow] to support me, and more technical articles and learning articles will be brought to you in the future! (ALI asked a lot about the underlying MySQL implementation and index Implementation)

External chain picture transferring... (img-Q2V9tCJv-1623618711931)]

[external chain picture transferring... (img-tQL4TxEP-1623618711931)]

After reading this pdf, you can also talk about MySQL with the interviewer. In fact, the demand of Alibaba p7 post is not so difficult (but not simple). Solid Java foundation + no short board knowledge + in-depth learning of some open source technologies + reading the source code + algorithm problem brushing. This set of p7 post is almost no problem. I still hope everyone can get a high salary offer.

Topics: Java Interview Programmer