将pojo字段复制到另一个pojo的二传手 [英] Copy pojo fields to another pojo's setters
问题描述
假设我有带有公共字段x
和y
的类A
.假设我还有另一个pojo类B
,但是它使用了setter和getter,所以它有setX()和setY().
Let's say I have class A
with public fields x
and y
. And let's say I have another pojo class B
but that uses setters and getters, so it has setX() and setY().
我想使用某种自动方式从A
实例复制到B
并返回.
I'd like to use some automatic way to copy from instance of A
to B
and back.
至少使用默认设置,推土机
With default settings at least, Dozer's
Mapper mapper = new DozerBeanMapper();
B b = mapper.map(a, B.class);
不能正确复制字段.
那么是否有一个简单的配置更改可以使我使用Dozer或其他可以为我完成此任务的库来完成上述工作?
So is there a simple configuration change that allows me to accomplish the above with Dozer, or another library that would do this for me?
推荐答案
我建议您使用:
或者看看这个问题:
我想说API(BeanUtils)和ModelMapper都提供了将pojos的值复制到另一个pojos的一线式.看看@这个:
I'd say that both API's (BeanUtils) and ModelMapper provide one-liners for copy pojos' values to another pojos. Take a look @ this:
http://modelmapper.org/getting-started/
这篇关于将pojo字段复制到另一个pojo的二传手的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!