BeanUtils.copyProperties()-个人总结

实体类


package com.zdc.entity;

import java.math.BigDecimal;
import java.util.Date;

public class Employee {

	private String id;
	private String name;
	private Date birthday;
	private Integer age;
	private Boolean gender;
	private Double  myDouble;
	private Short myShort;
	
	private char mychar;
	private float myfloat;
	private Object myObj;
	
	private Date myDate;
	
	private BigDecimal myBigDecimal;
	
	
	public Employee(){
		
	}
	

	

	




	@Override
	public String toString() {
		return "Employee [id=" + id + ", name=" + name + ", birthday="
				+ birthday + ", age=" + age + ", gender=" + gender
				+ ", myDouble=" + myDouble + ", myShort=" + myShort
				+ ", mychar=" + mychar + ", myfloat=" + myfloat + ", myObj="
				+ myObj + ", myDate=" + myDate + ", myBigDecimal="
				+ myBigDecimal + "]";
	}









	public Employee(String id, String name, Date birthday, Integer age,
			Boolean gender, Double myDouble, Short myShort) {
		super();
		this.id = id;
		this.name = name;
		this.birthday = birthday;
		this.age = age;
		this.gender = gender;
		this.myDouble = myDouble;
		this.myShort = myShort;
	}
	public String getId() {
		return id;
	}
	public void setId(String id) {
		this.id = id;
	}
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	public Date getBirthday() {
		return birthday;
	}
	public void setBirthday(Date birthday) {
		this.birthday = birthday;
	}
	public Integer getAge() {
		return age;
	}
	public void setAge(Integer age) {
		this.age = age;
	}
	public Boolean getGender() {
		return gender;
	}
	public void setGender(Boolean gender) {
		this.gender = gender;
	}
	public Double getMyDouble() {
		return myDouble;
	}
	public void setMyDouble(Double myDouble) {
		this.myDouble = myDouble;
	}
	public Short getMyShort() {
		return myShort;
	}
	public void setMyShort(Short myShort) {
		this.myShort = myShort;
	}




	public char getMychar() {
		return mychar;
	}




	public void setMychar(char mychar) {
		this.mychar = mychar;
	}




	public float getMyfloat() {
		return myfloat;
	}




	public void setMyfloat(float myfloat) {
		this.myfloat = myfloat;
	}




	public Object getMyObj() {
		return myObj;
	}




	public void setMyObj(Object myObj) {
		this.myObj = myObj;
	}




	public Date getMyDate() {
		return myDate;
	}




	public void setMyDate(Date myDate) {
		this.myDate = myDate;
	}




	public BigDecimal getMyBigDecimal() {
		return myBigDecimal;
	}




	public void setMyBigDecimal(BigDecimal myBigDecimal) {
		this.myBigDecimal = myBigDecimal;
	}
	
	
	
}

Srping 测试 和总结

package com.zdc.test;

import java.lang.reflect.InvocationTargetException;
import java.util.Date;

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

import com.zdc.entity.Employee;

/**
 * 关于使用BeanUtils.copyProperties 的总结
 *  1. 引入的包不同,使用方法不同
 *        提供BeanUtils包 本人暂时知道的有两个 : Spring提供 和 apache 提供
 *        Spring提供的 BeanUtils 不需要考虑空值问题
 *        apache 提供的需要考虑空值  尤其是 java.util.Date,   java.math.BigDecimal 等类型。如果是空值得话,就会抛出异常
 *         因此需要 使用BeanUtilsBean 进行转换处理
 *  2. BeanUtils.copyProperties(这里指的是apache 提供的)    与 PropertyUtils.copyProperties() 比较
 *             PropertyUtils.copyProperties()  仅仅对属性和名称进行转换  ,如果类型匹配不成功,就会抛出异常
 *             效率方面 PropertyUtils.copyProperties()  的效率要比 BeanUtils.copyProperties() 强的多
 *  3.灵活度上,个人认为apache 提供的稍微好一点,只要继承BeanUtilsBean 就可以实现对一般需要的处理
 *        
 *         
 *         
 * @author 
 *
 */
public class copyBeanUtilsBySpring {

	public static void main(String[] args) {
		Employee sourceEmployee=new Employee("122", "zhangsan",
				new Date(), 23, true, 3.3, (short)1);
		Employee newEmployee=new Employee();
		Employee propertyUtilsEmployee=new Employee();
		BeanUtils.copyProperties(sourceEmployee, newEmployee);
		System.out.println(newEmployee.toString());
		try {
			PropertyUtils.copyProperties(propertyUtilsEmployee, sourceEmployee);
			System.out.println(propertyUtilsEmployee.toString());
		} catch (IllegalAccessException | InvocationTargetException
				| NoSuchMethodException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
}





apache提供

package com.zdc.test;

import java.lang.reflect.InvocationTargetException;
import java.math.BigDecimal;
import java.util.Date;

import org.apache.commons.beanutils.BeanUtilsBean;

import com.zdc.entity.Employee;

public class copyJavaBean {

	
	 public static void main(String[] args) {
		Employee sourceEmployee=new Employee("122", "zuodengchao",
				new Date(), 23, true, 3.3, (short)1);
		
		Employee newEmployee=new Employee();
		BeanUtilsBean beanUtils=new BeanUtilsBean();
		beanUtils.getConvertUtils().register(new org.apache.commons.beanutils
				 .converters.BigDecimalConverter(null),
				  BigDecimal.class);
		beanUtils.getConvertUtils().register(new org.apache.commons.beanutils.converters.DateConverter(null),
				            java.util.Date.class);
		
		try {
			beanUtils.copyProperties(newEmployee, sourceEmployee);
		} catch (IllegalAccessException | InvocationTargetException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		System.out.println(newEmployee.toString());
		System.out.println("执行成功");
	}
}




评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值