CopyProperties BeanUtils difference method and the PropertyUtils

first copyProperties

        org.apache.commons.beanutils package includes two tools, and BeanUtils PropertyUtils, these two tools have a respective copyProperties (Object dest, Object orig) method, two methods may be the same among the two objects value of the attribute assigned to the target object by the source object. Use this category scene is generally passed from the front of a background wrapper class, the value of the copy to an entity wrapper class object, the object is written to the database entity table, multiple lines may be omitted get and set methods.

 

2. BeanUtils difference and copyProperties method of PropertyUtils

       The method of method name copyProperties tools among these two parameters are the same, is used similarly to the scene, the difference between them is that the source object int, when the value is null. Use the following code illustrates, there is a User class, and a class that inherits from the class UserVO, to do this, is among the User property UserVO to copy them.

 

3. Test Code

3.1 entity class

        Here are two entity classes, which an entity class inherits from another entity classes, test code to do is to assign a value to the parent class attributes to subclasses of them (lack of space, eliminating the need for get, set and toString method) .

import java.util.Date;

public class User {
	private Long id;
	
	private String name;
	
	private Integer sex;
	
	private Double age;
	
	private Date birthDay;
}
public class UserVO extends User{
	private String address;

	public String getAddress() {
		return address;
	}

	public void setAddress(String address) {
		this.address = address;
	}
}

3.2 Test Methods

        Property for non-null value, the result of the process of the two tools get the same, then a null value to directly test, the test method is as follows:

import java.lang.reflect.InvocationTargetException;

import org.apache.commons.beanutils.BeanUtils;
import org.apache.commons.beanutils.PropertyUtils;

public class Test01 {
	public static void main(String[] args) {
		// 原对象
		User sourceUser = new User();
		sourceUser.setId(null);
		sourceUser.setName(null);
		sourceUser.setAge(null);
		sourceUser.setSex(null);
		sourceUser.setBirthDay(null);
		// 目标对象
		UserVO destUser = new UserVO();
		// 测试BeanUtil
		testBeanUtil(destUser, sourceUser);
		// 测试PropertyUtil
		testPropertyUtil(destUser, sourceUser);
		
	}
	
	private static void testBeanUtil(User destUser, User sourceUser) {
		try {
			BeanUtils.copyProperties(destUser, sourceUser);
		} catch (IllegalAccessException e) {
			e.printStackTrace();
		} catch (InvocationTargetException e) {
			e.printStackTrace();
		}
		System.out.println("BeanUtils result:" + destUser);
	}
	
	private static void testPropertyUtil(User destUser, User sourceUser) {
		try {
			PropertyUtils.copyProperties(destUser, sourceUser);
		} catch (IllegalAccessException e) {
			e.printStackTrace();
		} catch (InvocationTargetException e) {
			e.printStackTrace();
		} catch (NoSuchMethodException e) {
			e.printStackTrace();
		}
		System.out.println("PropertyUtils result:" + destUser);
	}
}

Output 3.3

BeanUtils result:User [id=0, name=null, sex=0, age=0.0, birthDay=null, address=null]
PropertyUtils result:User [id=null, name=null, sex=null, age=null, birthDay=null, address=null]

        Can be seen from the results of the printing, the value of a Long, Integer type, Double type if it is empty, the method calls copyProperties BeanUtils ground to these types of results will assign a value of 0 or 0.0, and a method using a source PropertyUtils the copyProperties value of the object and the target object is the same.

 

4 Conclusion

        Org.apache.commons.beanutils.BeanUtils method using the obtained result will copyProperties to null Integer, Long, Double types of attributes assigned to 0 or 0.0, but String, Date and so there will not be such a class results ( null is still null), and the results obtained using the method copyProperties org.apache.commons.beanutils.BeanUtils the null value is not assigned to the initial value (still null to null). So come across a similar scene in the actual business development requires the correct selection of the appropriate tools.

Published 48 original articles · won praise 52 · views 20000 +

Guess you like

Origin blog.csdn.net/y506798278/article/details/99204061