将pojo字段复制到另一个pojo的二传手 [英] Copy pojo fields to another pojo's setters

查看:187
本文介绍了将pojo字段复制到另一个pojo的二传手的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

假设我有带有公共字段xy的类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?

推荐答案

我建议您使用:

http://modelmapper.org/

或者看看这个问题:

从字段中复制所有值在一个班级中通过反思到另一个班级

我想说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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆