首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 服务器 > Apache >

利用Apache BeanUtilsBean的copyProperties开展对象属性值copy

2013-12-04 
利用Apache BeanUtilsBean的copyProperties进行对象属性值copypublic class Student {private int intValu

利用Apache BeanUtilsBean的copyProperties进行对象属性值copy
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/setterpublic 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/setterpublic 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 copyProperties(Object target, Object source)throws InvocationTargetException, IllegalAccessException {//支持对日期copyorg.apache.commons.beanutils.BeanUtils.copyProperties(target, source);}}

?

3 测试类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();}}}

?

热点排行