Comparta una clase de herramienta de copia de propiedades JavaBean, muy fácil de usar.

Uno, la pieza de código

Comparta una clase de herramienta de copia de atributo JavaBean, no hay mucha tontería, solo vaya al código:


import java.lang.reflect.Method;
import java.lang.reflect.Modifier;
import java.util.HashMap;
import java.util.Map;
import java.util.Set;

/**
 * 属性拷贝工具,支持继承,要拷贝的属性必须有get,set的公用方法
 *
 * @author weiyuan
 * @version 1.0
 * @date 2014/11/12 14:05
 */
public class PropertyClone {
    
    
    private final static String SET_METHOD_FIX = "set";

    private final static String GET_METHOD_FIX = "get";

    private PropertyClone() {
    
    

    }

    private static <T> Map<String, Method> findPublicMethod(T t, String fix) {
    
    
        Map<String, Method> methodMap = new HashMap<String, Method>();
        Class<?> clz = t.getClass();
        while (!clz.equals(Object.class)) {
    
    
            Method[] methods = clz.getDeclaredMethods();
            for (Method method : methods) {
    
    
                int status = method.getModifiers();
                if (!Modifier.isPublic(status)) {
    
    
                    continue;
                }
                if (method.getName().startsWith(fix)) {
    
    
                    String field = method.getName().substring(3, 4).toLowerCase() + method.getName().substring(4);
                    if (methodMap.keySet().contains(field)) {
    
    
                        continue;
                    }
                    methodMap.put(field, method);
                }
            }
            clz = clz.getSuperclass();
        }
        return methodMap;
    }

    /**
     * <pre>
     * 属性拷贝工具方法
     * </pre>
     *
     * @param <T>  对象类型
     * @param dest 目标对象
     * @param orig 源对象
     * @param copyNullFlag 当属性值为null时,是否赋值. true:赋值,false:不赋值
     */
    public static <T, K> void copyProperties(T dest, K orig, Boolean copyNullFlag) {
    
    
        copyProperties(dest, orig, copyNullFlag, null);
    }

    /**
     * @param <T>
     * @param <K>
     * @param dest
     * @param orig
     * @param copyNullFlag
     * @param uncope       非克隆属性名
     */
    public static <T, K> void copyProperties(T dest, K orig, Boolean copyNullFlag, Set<String> uncope) {
    
    
        Map<String, Method> destSetMethodMap = findPublicMethod(dest, SET_METHOD_FIX);
        // 源对象中的所有get方法集合
        Map<String, Method> origGetMethodMap = findPublicMethod(orig, GET_METHOD_FIX);
        Set<String> fields = origGetMethodMap.keySet();
        boolean flag = (uncope == null || uncope.size() <= 0) ? false : true;
        for (String field : fields) {
    
    
            if (flag && uncope.contains(field)) {
    
    
                continue;
            }
            Method getMethod = origGetMethodMap.get(field);
            try {
    
    
                Object value = getMethod.invoke(orig, new Object[]{
    
    });
                if (!copyNullFlag) {
    
    
                    if (value == null) {
    
    
                        continue;
                    }
                }
                Method setMethod = destSetMethodMap.get(field);
                if (setMethod != null)
                    setMethod.invoke(dest, value);
            } catch (Exception e) {
    
    
                throw new RuntimeException("实体属性克隆发生异常");
            }
        }
    }
}


Segundo, usa ejemplos

//将origEngity的属性拷贝到destEntity中,false-不拷贝空属性
PropertyClone.copyProperties(destEntity, origEngity, false);

Tres, finalmente

Si no te lo quieres quitar, dale una recompensa. Si no le das una recompensa, al menos presta atención al blogger, jaja, jaja, jeje.

Supongo que te gusta

Origin blog.csdn.net/datuanyuan/article/details/109100013
Recomendado
Clasificación