实体类参数复制

实体类参数复制:用于接收的VO,复制到实体Bean里面,要求参数名相同,不同的参数名手动get   set添加

package com.bofeng.smart.school.common.bean;

import org.springframework.cglib.beans.BeanCopier;

import java.util.HashMap;
import java.util.Map;

/**
 * 将beancopier做成静态类,方便拷贝
 * <br>创建日期:2015年12月1日
 * <br><b>Copyright 2015 UTOUU All Rights Reserved</b>
 *
 * @author yushaojian
 * @version 1.0
 * @since 1.0
 */
public class BeanUtils {

    public static final Map<String, BeanCopier> BEAN_COPIERS = new HashMap<String, BeanCopier>();

    /**
     * @param source 资源类
     * @param target 目标类
     * @Title: copyProperties
     * @Description:
     *     (1)相同属性名,且类型不匹配时候的处理,ok,但是未满足的属性不拷贝;
     *     (2)get和set方法不匹配的处理,创建拷贝的时候报错,无法拷贝任何属性(当且仅当sourceClass的get方法超过set方法时出现)
     *     (3)BeanCopier
     *     初始化例子:BeanCopier copier = BeanCopier.create(Source.class, Target.class, useConverter=true)
     *     第三个参数userConverter,是否开启Convert,默认BeanCopier只会做同名,同类型属性的copier,否则就会报错.
     *     copier = BeanCopier.create(source.getClass(), target.getClass(), false);
     *     copier.copy(source, target, null);
     *     (4)修复beanCopier对set方法强限制的约束
     *     改写net.sf.cglib.beans.BeanCopier.Generator.generateClass(ClassVisitor)方法
     *     将133行的
     *     MethodInfo write = ReflectUtils.getMethodInfo(setter.getWriteMethod());
     *     预先存一个names2放入
     *      109        Map names2 = new HashMap();
     *      110        for (int i = 0; i < getters.length; ++i) {
     *      111          names2.put(setters[i].getName(), getters[i]);
     *                 }
     *     调用这行代码前判断查询下,如果没有改writeMethod则忽略掉该字段的操作,这样就可以避免异常的发生。
     * @author yushaojian
     * @date 2015年11月25日下午4:56:44
     */
    public static void copyProperties(Object source, Object target) {
        String beanKey = generateKey(source.getClass(), target.getClass());
        BeanCopier copier = null;
        if (!BEAN_COPIERS.containsKey(beanKey)) {
            copier = BeanCopier.create(source.getClass(), target.getClass(), false);
            BEAN_COPIERS.put(beanKey, copier);
        } else {
            copier = BEAN_COPIERS.get(beanKey);
        }
        copier.copy(source, target, null);
    }

    private static String generateKey(Class<?> class1, Class<?> class2) {
        return class1.toString() + class2.toString();
    }

}  

测试用例::

BeanUtils.copyProperties(a,q);
        q.setA(a.getA());
        q.setB(a.getB());

a是有数据的类,q是需要被复制进去数据的类(没数据)

猜你喜欢

转载自blog.csdn.net/weixin_39819191/article/details/81743376