java项目 BeanCopier使用说明

 

BeanCopier从名字可以看出了,是一个快捷的bean类复制工具类。

 

如何使用,我就直接丢代码了

public class BeanCopierTest {
    static SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

    public static void main(String[] args) {
//        sameCopy();
//        difCopy();
        difCopyConvert();
//        noSetterCopy();
//        lessSetterCopy();
//        lessSetterCopy2();
    }

    // 属性名称、类型都相同,成功复制
    public static void sameCopy() {
        one one = new one();
        one.setId(1);
        one.setName("one");
        final BeanCopier copier = BeanCopier.create(one.class, two.class, false);
        two two = new two();
        copier.copy(one, two, null);
        System.out.println(one.toString());
        System.out.println(two.toString());
    }

    // 属性名称相同,类型不同,类型不同的不能复制
    public static void difCopy() {
        one one = new one();
        one.setId(1);
        one.setName("one");
        final BeanCopier copier = BeanCopier.create(one.class, oneDif.class, false);
        oneDif oneDif = new oneDif();
        copier.copy(one, oneDif, null);
        System.out.println(one.toString());
        System.out.println(oneDif.toString());
    }

    // 属性名称相同,类型不同的解决方法,使用convert,注意要写上类型相同的情况
    public static void difCopyConvert() {
        one one = new one();
        one.setId(1);
        one.setName("one");
        final BeanCopier copier = BeanCopier.create(one.class, oneDif.class, true);
        oneDif oneDif = new oneDif();
        copier.copy(one, oneDif, new Converter() {
            @Override
            public Object convert(Object value, Class target, Object context) {
                if (value instanceof Integer) {
                    return (Integer) value;
                } else if (value instanceof Timestamp) {
                    Timestamp date = (Timestamp) value;
                    return sdf.format(date);
                } else if (value instanceof BigDecimal) {
                    BigDecimal bd = (BigDecimal) value;
                    return bd.toPlainString();
                } else if (value instanceof String) {
                    return "" + value;
                }
                return null;
            }
        });
        System.out.println(one.toString());
        System.out.println(oneDif.toString());
    }

    // 目标无setter,不能复制,注意此处,网上说会报错,但是新版本并无此报错
    public static void noSetterCopy() {
        one one = new one();
        one.setId(1);
        one.setName("one");
        final BeanCopier copier = BeanCopier.create(one.class, oneNoSetter.class, false);
        oneNoSetter oneNoSetter = new oneNoSetter();
        copier.copy(one, oneNoSetter, null);
        System.out.println(one.toString());
        System.out.println(oneNoSetter.toString());
    }

    // 源无setter,都不能赋值了好吧?
    public static void noSetterCopy2() {

    }

    // 目标少setter,少setter的没值
    public static void lessSetterCopy() {
        one one = new one();
        one.setId(1);
        one.setName("one");
        final BeanCopier copier = BeanCopier.create(one.class, oneLessSetter.class, false);
        oneLessSetter oneLessSetter = new oneLessSetter();
        copier.copy(one, oneLessSetter, null);
        System.out.println(one.toString());
        System.out.println(oneLessSetter.toString());
    }

    // 源少setter,只有有setter的可以复制
    public static void lessSetterCopy2() {
        oneLessSetter oneLessSetter = new oneLessSetter();
//        oneLessSetter.setId(1);
        oneLessSetter.setName("one");
        final BeanCopier copier = BeanCopier.create(oneLessSetter.class, one.class, false);
        one one = new one();
        copier.copy(oneLessSetter, one, null);
        System.out.println(oneLessSetter.toString());
        System.out.println(one.toString());
    }
}

 

下面是pojo类

public class one {
    public 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;
    }

    private int id;
    private String name;

    @Override
    public String toString() {
        return " one{  "
                + " \n id : " + this.id
                + " \n name : " + this.name
                +"  \n }";
    }
}
public class two {
    private int id;
    private String name;

    public 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;
    }

    @Override
    public String toString() {
        return " two{  "
                + " \n id : " + this.id
                + " \n name : " + this.name
                + "  \n }";
    }
}
public class oneDif {
    private Integer id;

    public Integer getId() {
        return id;
    }

    public void setId(Integer id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    private String name;
    @Override
    public String toString() {
        return " oneDif{  "
                + " \n id : " + this.id
                + " \n name : " + this.name
                + " \n }";
    }
}
public class oneLessSetter {
    public int getId() {
        return id;
    }

    public String getName() {
        return name;
    }

    private int id;

    public void setName(String name) {
        this.name = name;
    }

    private String name;

    @Override
    public String toString() {
        return " oneLessSetter{  "
                + " \n id : " + this.id
                + " \n name : " + this.name
                + " \n }";
    }
}
package com.lgp.thinkinjavademos.demo.BeanCopier;

/**
 * @AUTHOR lgp
 * @DATE 2018/6/25 16:06
 * @DESCRIPTION
 **/
public class oneNoSetter {
    public int getId() {
        return id;
    }

    public String getName() {
        return name;
    }

    private int id;
    private String name;

    @Override
    public String toString() {
        return " oneNoSetter{  "
                + " \n id : " + this.id
                + " \n name : " + this.name
                + " \n }";
    }
}

 

放入缓存,提高性能

public class CachedBeanCopier {
    //    创建过的BeanCopier实例放到缓存中,下次可以直接获取,提升性能
    static final Map<String, BeanCopier> BEAN_COPIERS = new HashMap<String, BeanCopier>();

    public static void copy(Object srcObj, Object destObj) {
        String key = genKey(srcObj.getClass(), destObj.getClass());
        BeanCopier copier = null;
        if (!BEAN_COPIERS.containsKey(key)) {
            copier = BeanCopier.create(srcObj.getClass(), destObj.getClass(), false);
            BEAN_COPIERS.put(key, copier);
        } else {
            copier = BEAN_COPIERS.get(key);
        }
        copier.copy(srcObj, destObj, null);
    }

    private static String genKey(Class<?> srcClazz, Class<?> destClazz) {
        return srcClazz.getName() + destClazz.getName();
    }
}

 

 

posted @ 2018-06-25 17:02  lgp20151222  阅读(17602)  评论(1编辑  收藏  举报