⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 copybean.java

📁 hr伯乐管理系统!非常适合java学员做学习参考用!是用集成框架开发的Struts+hrbernet+Spring 开发的
💻 JAVA
字号:
package org.better.hr.comm;

import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;

import org.apache.commons.beanutils.BeanUtils;

public class CopyBean {
	
	/**
	 * 复制成员变量的值
	 * @param dest 目标对象
	 * @param orig 源对象
	 * @throws ClassNotFoundException
	 * @throws IllegalAccessException
	 * @throws InvocationTargetException
	 * @throws NoSuchMethodException
	 */
	public static void copyProperties(Object dest,Object orig) throws ClassNotFoundException, IllegalAccessException, InvocationTargetException, NoSuchMethodException
	{
		String field_name;
		Class clz = orig.getClass();   //获得源对象的类
		Field[] sup = clz.getSuperclass().getDeclaredFields();  //获得源类的父类的所有属性
		
		for(int i=0;i<sup.length;i++)
		{
			field_name = sup[i].getName();  //得到源对象所有属性的名字
			String value = BeanUtils.getProperty(orig, field_name);  //得到源对象属性的值
			if(!sup[i].getType().getName().equals("java.util.Date") && value != null && !value.equals("") && !value.equals("0"))
			{
				BeanUtils.setProperty(dest, field_name, value);  //将源对象的属性值赋到目标对象中
			}
			//System.out.println(field_name + ":  " + BeanUtils.getProperty(dest, field_name));
		}
		
		Field[] sub = clz.getDeclaredFields();  //获得源对象的所有属性
		for(int i=0;i<sub.length;i++)
		{
			field_name = sub[i].getName();
			String value = BeanUtils.getProperty(orig, field_name);
			if(!sub[i].getType().getName().equals("java.util.Date") && value != null && !value.equals("") && !value.equals("0"))
			{
				BeanUtils.setProperty(dest, field_name, value);
			}
			//System.out.println(field_name + ":  " + BeanUtils.getProperty(dest, field_name));
		}
	}
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -