http://redcoatjk.iteye.com/blog/1984967

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

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

 

测试如下:

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;
}
}

 

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;
}
}

 

3 封装类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为空而报错的情况
* @author Yulin
*/
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 copyProperties(Object target, Object source)
throws InvocationTargetException, IllegalAccessException {
//支持对日期copy
org.apache.commons.beanutils.BeanUtils.copyProperties(target, source);
}
}

4 测试类testClient

import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import com.test.util.BeanUtilPlus1;
public class testClient {
/**
* 测试BeanUtilsBean.getInstance().copyProperties对数据进行赋值时候的效果
* java.sql.Date不可为空值
* java.util.Date不可为空值
*
* 使用改进的BeanUtilPlus1.copyProperties
* Date为空时,直接赋空值.
*/
public static void main(String[] args) {
/**
* 初始化数据
*/
Teacher t1=new Teacher();
t1.setId(1);
t1.setName("teacher t1");
Student s1=new Student();
s1.setBooleanValue0(true);
s1.setBooleanValue1(true);
s1.setDoubleValue(1.1);
s1.setIntegerValue(12);
s1.setIntValue(123);
s1.setLongValue0(12L);
s1.setLongValue1(2L);
//          s1.setSqlDateValue(new java.sql.Date(new java.util.Date().getTime()));
//          s1.setUtilDateValue(new java.util.Date());
s1.setStrintValue("student s1");
s1.setTeacher(t1);
try {
//打印出所有内容
Class c = Class.forName("Student");
//              Object o = c.newInstance();
Object o=s1;
Method[] ms= c.getMethods();
//              System.out.println(ms.length);
System.out.println("原始数据");
for(Method m:ms){
if( m.getName().contains("get")){
System.out.println("method  "+m.getName()+"  : ");
System.out.println(m.invoke(o));
}
}
} catch (Exception e) {
e.printStackTrace();
}
Student s2=new Student();
try {
//              BeanUtilsBean.getInstance().copyProperties(s2, s1);  //调用copy方法
BeanUtilPlus1.copyProperties(s2,s1); //调用改写过的copy方法
} catch (IllegalAccessException e) {
e.printStackTrace();
} catch (InvocationTargetException e) {
e.printStackTrace();
}
try {
Class c = Class.forName("Student");
Object o=s2;
Method[] ms= c.getMethods();
System.out.println("新数据");
for(Method m:ms){
if( m.getName().contains("get")){
System.out.println("method  "+m.getName()+"  : ");
System.out.println(m.invoke(o));
}
}
} catch (Exception e) {
e.printStackTrace();
}
}
}

达到效果:

完成student的属性值复制、简单封装BeanUtil方法(原方法在复制时遇到java.util.Date和java.sql.Date为空时将报错.) 封装后,面对util.Date或者sql.Date将直接赋值为null(通过改写注册器实现)