前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >BeanCopier使用说明

BeanCopier使用说明

作者头像
ydymz
发布2018-09-10 17:22:02
3.6K0
发布2018-09-10 17:22:02
举报
文章被收录于专栏:lgp20151222

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

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

代码语言:javascript
复制
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类

代码语言:javascript
复制
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 }";
    }
}
代码语言:javascript
复制
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 }";
    }
}
代码语言:javascript
复制
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 }";
    }
}
代码语言:javascript
复制
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 }";
    }
}
代码语言:javascript
复制
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 }";
    }
}

放入缓存,提高性能

代码语言:javascript
复制
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();
    }
}

本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
原始发表:2018-06-25 ,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档