仅将字符串属性从一个对象复制到另一个对象

时间:2022-04-07 07:38:48

I was working with dozer and I had a special scenario which I think dozer doesn't support. Suppose I have a class with different type of properties like String,Ingeger and other primitive types. Now I want to copy only string property from this class to another. Here s a example code :(Its very brief, but it will give proper understanding of problem.)

我正在使用推土机,我有一个特殊的场景,我认为推土机不支持。假设我有一个具有不同类型属性的类,如String,Ingeger和其他基本类型。现在我想只将此类的字符串属性复制到另一个类。这是一个示例代码:(非常简短,但它会正确理解问题。)

Class Source{
private int a;
private boolean b;
private String s1;
//more properties with primitive type.

}
Class Destination{
//    same structure
}

Now I want somethink like,

现在我想要一些想法,

DozerBeanMapper mapper = new DozerBeanMapper();
//    initialize Object of source
//   initialize Object of destination
mapper.map(source,destination);

Is it possible with any dozer ? I am familiar with dozer. But Oper to other APIs like apache beanutil or ModelMapper. Feel free to answer in any of this technology.

是否可以使用任何推土机?推土机我很熟悉。但是对其他API如Oache beanutil或ModelMapper。随意回答任何这项技术。

1 个解决方案

#1


0  

Use populate() and setProperty() of Apache Commons BeanUtils.

使用Apache Commons BeanUtils的populate()和setProperty()。

First populate() an empty bean, than, you can apply an if condition as:

首先populate()一个空bean,你可以将if条件应用为:

if (urBean.getAParameter() instanceof String){
    setProperty(newBeanObject, "propertyName", urBean.getParameter());
}

#1


0  

Use populate() and setProperty() of Apache Commons BeanUtils.

使用Apache Commons BeanUtils的populate()和setProperty()。

First populate() an empty bean, than, you can apply an if condition as:

首先populate()一个空bean,你可以将if条件应用为:

if (urBean.getAParameter() instanceof String){
    setProperty(newBeanObject, "propertyName", urBean.getParameter());
}