美文网首页
两个entity 之间转换方法

两个entity 之间转换方法

作者: MR崔先生 | 来源:发表于2021-07-21 09:19 被阅读0次

其实常见的BeanUtils有2个:
spring有BeanUtils
apache的commons也有BeanUtils
– spring的BeanUtils commons的BeanUtils
方法 copyProperty和copyProperties copyProperties
参数 src ,dest dest,src
这2个用哪个都行,但是要注意区别。 因为他们2个的src和dest是正好相反的,要特别留意。

1、使用 BeanUtils.copyProperties方法进行对象之间属性的赋值,避免通过get、set方法一个一个属性的赋值

/**
     * 对象属性拷贝 <br>
     * 将源对象的属性拷贝到目标对象
     *
     * @param source 源对象
     * @param target 目标对象
     */
    public static void copyProperties(Object source, Object target) {
        try {
            BeanUtils.copyProperties(source, target);
        } catch (BeansException e) {
            LOGGER.error("BeanUtil property copy  failed :BeansException", e);
        } catch (Exception e) {
            LOGGER.error("BeanUtil property copy failed:Exception", e);
        }
    } 

2、List集合之间的对象属性赋值
/**
* @param input 输入集合
* @param clzz 输出集合类型
* @param <E> 输入集合类型
* @param <T> 输出集合类型
* @return 返回集合
*/
public static <E, T> List<T> convertList2List(List<E> input, Class<T> clzz) {
List<T> output = Lists.newArrayList();
if (CollectionUtils.isNotEmpty(input)) {
for (E source : input) {
T target = BeanUtils.instantiate(clzz);
BeanUtil.copyProperties(source, target);
output.add(target);
}
}
return output;
}

import org.springframework.beans.BeanUtils;

public class TestUtil
{
    @Test
    public void test(){
        Employee ee1=new Employee("A",21,"it");
        Employee ee2=new Employee("B",23,"account");
        User user=new User();
        BeanUtil.copyProperties(ee1, user);
        System.out.println(user);
        System.out.println("-------------分割线--------------");
        List<User> output=new ArrayList<>();
        List<Employee> source= Arrays.asList(ee1,ee2);
        output=BeanUtil.convertList2List(source,User.class);
        for (User str:output) {
            System.out.println(str);
        }
    }
} 
public class User {
    private String name;
    private Integer age;
 
    public String getName() {
        return name;
    }
 
    public void setName(String name) {
        this.name = name;
    }
 
    public Integer getAge() {
        return age;
    }
 
    public void setAge(Integer age) {
        this.age = age;
    }
 
    @Override
    public String toString() {
        return "User{" +
                "name='" + name + '\'' +
                ", age=" + age +
                '}';
    }
} 
public class Employee {
    private String name;
 
    private Integer age;
    private String dept;
 
    public Employee(String name, Integer age, String dept) {
        this.name = name;
        this.age = age;
        this.dept = dept;
    }
 
    public String getName() {
        return name;
    }
 
    public void setName(String name) {
        this.name = name;
    }
 
    public Integer getAge() {
        return age;
    }
 
    public void setAge(Integer age) {
        this.age = age;
    }
 
    public String getDept() {
        return dept;
    }
 
    public void setDept(String dept) {
        this.dept = dept;
    }
 
    @Override
    public String toString() {
        return "Employee{" +
                "name='" + name + '\'' +
                ", age=" + age +
                ", dept='" + dept + '\'' +
                '}';
    }
} 

相关文章

网友评论

      本文标题:两个entity 之间转换方法

      本文链接:https://www.haomeiwen.com/subject/hdodmltx.html