日期:2014-05-16  浏览次数:20886 次

利用Apache BeanUtilsBean的copyProperties进行对象属性值copy

在开发中进行遇到值对象和实体间的数据复制.

其中最常用的为Apache BeanUtilsBean的copyProperties.

本次测试发现该工具可兼容大部分数据格式,但在Java.sql.Date和java.util.Date复制时,原数据如为空将发送报错.

通过注册BeanUtilsBean的转换器,简单封装copyProperties方法.

完成效果: 调用封装后的工具类方法复制对象属性时, 遇到Date数据一律给予新对象对应属性赋值为null.

?

测试代码如下

?

?1.1 实体类Student

public class Student {
	private int intValue;
	private double doubleValue;
	private long longValue0;
	private boolean booleanValue0;
	private Boolean booleanValue1;
	private String strintValue;
	private java.util.Date utilDateValue;
	private java.sql.Date sqlDateValue;
	private Integer IntegerValue;
	private Long LongValue1;
	private Teacher teacher;
	
	//getter/setter
	public int getIntValue() {
		return intValue;
	}
	public void setIntValue(int intValue) {
		this.intValue = intValue;
	}
	public double getDoubleValue() {
		return doubleValue;
	}
	public void setDoubleValue(double doubleValue) {
		this.doubleValue = doubleValue;
	}
	public String getStrintValue() {
		return strintValue;
	}
	public void setStrintValue(String strintValue) {
		this.strintValue = strintValue;
	}
	public java.util.Date getUtilDateValue() {
		return utilDateValue;
	}
	public void setUtilDateValue(java.util.Date utilDateValue) {
		this.utilDateValue = utilDateValue;
	}
	public java.sql.Date getSqlDateValue() {
		return sqlDateValue;
	}
	public void setSqlDateValue(java.sql.Date sqlDateValue) {
		this.sqlDateValue = sqlDateValue;
	}
	public Integer getIntegerValue() {
		return IntegerValue;
	}
	public void setIntegerValue(Integer integerValue) {
		IntegerValue = integerValue;
	}
	public long getLongValue0() {
		return longValue0;
	}
	public void setLongValue0(long longValue0) {
		this.longValue0 = longValue0;
	}
	public Long getLongValue1() {
		return LongValue1;
	}
	public void setLongValue1(Long longValue1) {
		LongValue1 = longValue1;
	}
	public boolean isBooleanValue0() {
		return booleanValue0;
	}
	public void setBooleanValue0(boolean booleanValue0) {
		this.booleanValue0 = booleanValue0;
	}
	public Boolean getBooleanValue1() {
		return booleanValue1;
	}
	public void setBooleanValue1(Boolean booleanValue1) {
		this.booleanValue1 = booleanValue1;
	}
	public Teacher getTeacher() {
		return teacher;
	}
	public void setTeacher(Teacher teacher) {
		this.teacher = teacher;
	}
	
}

?

1.2 实体类teacher

public class Teacher {
	private int id;
	private String name;
	
	//getter/setter
	public int getId() {
		return id;
	}
	public void setId(int id) {
		this.id = id;
	}
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}

	
}

?

2 简单封装的工具类BeanUtilPlus1

package com.test.util;

import java.lang.reflect.InvocationTargetException;
import java.util.HashMap;
import java.util.Map;

import org.apache.commons.beanutils.BeanUtils;
import org.apache.commons.beanutils.ConvertUtils;
import org.apache.commons.beanutils.converters.DateConverter;
import org.apache.commons.beanutils.converters.SqlDateConverter;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

/**
 * 继承apache的BeanUtils,避免调用copyProperties时因date为空而报错的情况
 */
public class BeanUtilPlus1 extends BeanUtils {
	private static Map cache = new HashMap();
	private static Log logger = LogFactory.getFactory().getInstance(
			BeanUtilPlus1.class);

	private BeanUtilPlus1() {
	}

	static {
		//注册sql.date的转换器,即允许BeanUtils.copyProperties时的源目标的sql类型的值允许为空
		ConvertUtils.register(new SqlDateConverter(null),  java.sql.Date.class);
		//ConvertUtils.register(new SqlTimestampConverter(), java.sql.Timestamp.class);
		//注册util.date的转换器,即允许BeanUtils.copyProperties时的源目标的util类型的值允许为空
		ConvertUtils.register(new DateConverter(null), java.util.Date.class);
	}

	public static void copyPropert