绿皮驿站

分享
BeanUtil - 拷贝bean相同属性的工具
开发背景

项目里面会有各种类型bean,如DO、VO、DTO等等。

里面的属性大部分是相同的,需要一个一个get,set,属性少还好说,如果几十个到几百个呢,显然很影响开发效率。

网上好像有这种工具,可是我找不到,干脆自己写一个。

利用Java反射实现,牺牲一点点性能,换来大大的开发效率,对于一般小项目来说还是挺值得的。


主要功能

从源bean复制到目标bean,名称和类型相同的属性才能拷贝。


详细代码
import java.beans.IntrospectionException;
import java.beans.PropertyDescriptor;
import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.List;

public class BeanUtil {

	public static void copyBean(Object src,Object dest){
		Class<?> srcClass = src.getClass();
		Class<?> destClass = dest.getClass();
		Field[] srcFields = srcClass.getDeclaredFields();
		Field[] destFields = destClass.getDeclaredFields();
		Field srcField = null;
		Field destField = null;
		Method[] srcMethods = new Method[destFields.length];
		Method[] destMethods = new Method[destFields.length];
		int index = 0;
		try {
			for(int i=0;i<srcFields.length;i++){
				srcField = srcFields[i];
				for(int j=0;j<destFields.length;j++){
					destField = destFields[j];
					if(destField != null){
						if(srcField.getType().equals(destField.getType()) && srcField.getName().equals(destField.getName())){
							srcMethods[index] = new PropertyDescriptor(srcField.getName(), srcClass).getReadMethod();
							destMethods[index] = new PropertyDescriptor(destField.getName(), destClass).getWriteMethod();
							index++;
							destFields[j] = null;
						}
					}
				}
			}
		} catch (IntrospectionException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		try {
			for(int i=0;i<index;i++){
				destMethods[i].invoke(dest, srcMethods[i].invoke(src));
			}
		} catch (IllegalAccessException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IllegalArgumentException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (InvocationTargetException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	
	@SuppressWarnings({ "rawtypes", "unchecked" })
	public static void copyList(List srcList,List destList,Class<?> destClass){
		try {
			for(Object src:srcList){
				Object dest=destClass.newInstance();
				copyBean(src,dest);
				destList.add(dest);
			}
		} catch (InstantiationException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IllegalAccessException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
}


测试代码
        public static void main(String[] args) {
		StudentDO studentDO = new StudentDO();
		studentDO.setId(1);
		studentDO.setName("绿皮蛙");
		studentDO.setAge(22);
		
		StudentVO studentVO = new StudentVO();
		
		BeanUtil.copyBean(studentDO, studentVO);
		
		System.out.println(studentDO);    // => StudentDO [id=1, name=绿皮蛙, age=22]
		System.out.println(studentVO);    // => [name=绿皮蛙, age=22]
StudentDO studentDO2 = new StudentDO(); studentDO.setId(2); studentDO.setName("绿皮仔"); studentDO.setAge(20); List<StudentDO> studentDOList = new ArrayList<StudentDO>(); studentDOList.add(studentDO); studentDOList.add(studentDO2); List<StudentVO> studentVOList = new ArrayList<StudentVO>(); BeanUtil.copyList(studentDOList, studentVOList, StudentVO.class); System.out.println(studentDOList); // => [StudentDO [id=2, name=绿皮仔, age=20], StudentDO [id=null, name=null, age=null]] System.out.println(studentVOList); // => [StudentVO [name=绿皮仔, age=20], StudentVO [name=null, age=null]] }


发布于 2017年8月10日   浏览(1,886)