Three methods between objects of the same property of rapid replication

1: BeanUtils class package commons-beanutils

Usage: introducing dependence

<dependency>
    <groupId>commons-beanutils</groupId>
    <artifactId>commons-beanutils</artifactId>
    <version>1.7.0</version>
</dependency>
import org.apache.commons.beanutils.BeanUtils;

/*BeanUtils.copyProperties(target,source);
与上边正好相反,将source的属性复制到target(属性字段相同才会进行复制,否则不复制)*/
BeanUtils.copyProperties(studentVo, student);

Note: This method needs to throw an exception

2: Use BeanUtils class spring-beans5.0.8 package

Usage: introducing dependence

<dependency>
 	<groupId>org.springframework</groupId>
    <artifactId>spring-beans</artifactId>
    <version>5.2.4.RELEASE</version>
</dependency>
import org.springframework.beans.BeanUtils;

//BeanUtils.copyProperties(source, target);
//将source的属性复制到target(属性字段相同才会进行复制,否则不复制)
BeanUtils.copyProperties(student, studentVo);

3: copy provided by way cglib

Usage: introducing dependence

<dependency>
   <groupId>cglib</groupId>
   <artifactId>cglib</artifactId>
   <version>3.1</version>
</dependency>
import net.sf.cglib.beans.BeanCopier;

//将student的属性复制到studentVo(属性字段相同才会进行复制,否则不复制)
BeanCopier copier = BeanCopier.create(Student.class,StudentVo.class,false);
copier.copy(student,studentVo,null);

Note: The performance of the three were better than the 3 2, 2 better than 1

There are a variety of replication, it will not make list

Published 27 original articles · won praise 1 · views 836

Guess you like

Origin blog.csdn.net/weixin_44971379/article/details/105235179