JavaBean复制工具——BeanCopier

1.BeanCopier在cglib包下,Spring核心包中有包含

 

 

2.使用

  a.对象转对象

    //对象转对象
    public static void bean2bean(Object bean1, Object bean2){
        BeanCopier beanCopier = BeanCopier.create(bean1.getClass(), bean2.getClass(), false);
        beanCopier.copy(bean1, bean2, null);
    }

 

  b.对象转Map

    //对象转Map
    public static void bean2Map(Object bean1, Map<String, Object> map){
        Map<String, Object> beanMap = BeanMap.create(bean1);
        map.putAll(beanMap);
    }

 

  c.Map转对象

    //Map转对象
    public static void map2Bean(Map<String, Object> map ,Object bean1){
        Map<String, Object> beanMap = BeanMap.create(bean1);
        beanMap.putAll(map);
    }

 

 

3.测试

  a.创建两个测试VO

public class Test1VO {

    private int id;
    private String name;
    private Map<String, String> attrs;

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

    public Map<String, String> getAttrs() {
        return attrs;
    }

    public void setAttrs(Map<String, String> attrs) {
        this.attrs = attrs;
    }

    @Override
    public String toString() {
        return "Test1VO{" +
                "id=" + id +
                ", name='" + name + '\'' +
                ", attrs=" + attrs +
                '}';
    }
}
public class Test2VO {

    private int id;
    private String name;
    private Map<String, String> attrs;

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

    public Map<String, String> getAttrs() {
        return attrs;
    }

    public void setAttrs(Map<String, String> attrs) {
        this.attrs = attrs;
    }

    @Override
    public String toString() {
        return "Test2VO{" +
                "id=" + id +
                ", name='" + name + '\'' +
                ", attrs=" + attrs +
                '}';
    }
}

 

  b.测试

    public static void main(String[] args) {
        //对象转对象
//        Test1VO test1 = getTest1();
//        Test2VO test2 = new Test2VO();
//        bean2bean(test1, test2);
//        System.out.println(test2);

        //对象转Map
//        Test1VO test1 = getTest1();
//        Map<String, Object> map = new HashMap<>();
//        bean2Map(test1, map);
//        System.out.println(map);

        //Map转对象
        Map<String, Object> map = getMap();
        Test2VO test2 = new Test2VO();
        map2Bean(map, test2);
        System.out.println(test2);

    }

    private static Test1VO getTest1(){
        Test1VO test1 = new Test1VO();
        test1.setId(1);
        test1.setName("name");
        Map<String, String> attrs = new HashMap<>();
        attrs.put("key1", "value1");
        attrs.put("key2", "value2");
        test1.setAttrs(attrs);
        return test1;
    }

    private static Map<String, Object> getMap(){
        Map<String, Object> map = new HashMap<>();
        map.put("name", "This is name");
        map.put("id", 55);

        Map<String, String> attrs = new HashMap<>();
        attrs.put("key1", "value1");
        attrs.put("key2", "value2");
        map.put("attrs", attrs);
        return map;
    }

 

  c.注意:BeanCopier只拷贝名称和类型都相同的属性

 

 

4.转换器

  a.创建VO

public class Test3VO {

    private int id;
    private boolean name;
    private List<String> attrs;

    public int getId() {
        return id;
    }

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

    public boolean isName() {
        return name;
    }

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

    public List<String> getAttrs() {
        return attrs;
    }

    public void setAttrs(List<String> attrs) {
        this.attrs = attrs;
    }

    @Override
    public String toString() {
        return "Test3VO{" +
                "id=" + id +
                ", name=" + name +
                ", attrs=" + attrs +
                '}';
    }
}

 

  b.创建自定义转换器

public class TestConverter implements Converter {

    @Override
    public Object convert(Object value, Class target, Object context) {
        if("setName".equals(context)){
            if(! StringUtils.hasText((String) value)){
                return false;
            }
            return true;
        }
        if("setAttrs".equals(context)){
            Map<String, String> attrMap = (Map<String, String>) value;
            if(CollectionUtils.isEmpty(attrMap)){
                return null;
            }
            List<String> result = new ArrayList<>();
            for(Map.Entry<String, String> entry : attrMap.entrySet()){
                result.add(entry.getKey() + "-" + entry.getValue());
            }
            return result;
        }
        return value;
    }
}

 

  c.使用

    //对象转对象(转换器)
    private static void bean2bean(Object bean1, Object bean2, Converter converter){
        BeanCopier beanCopier = BeanCopier.create(bean1.getClass(), bean2.getClass(), true);
        beanCopier.copy(bean1, bean2, converter);
    }

 

  d.测试

        //转换器
        Test1VO test1 = getTest1();
        Test3VO test3 = new Test3VO();
        bean2bean(test1, test3, new TestConverter());
        System.out.println(test3);

 

posted @ 2020-04-03 09:58  晨M风  阅读(3360)  评论(0编辑  收藏  举报