Java - 如何简化我的java

发布于 2024-11-05 05:59:58 字数 1193 浏览 1 评论 0原文

我正在 Spring 框架中编写一些 java 代码。

我有两个 bean,person 和 person1。它们的结构略有不同,也就是说每个变量的名称略有不同。

我正在尝试将详细信息从一个 bean 复制到另一个 bean。我只想在该值不为空时复制该值。我见过一个名为 BeanUtils 的 API,但是无论它是否为空,都会复制它。

这是我的代码:

if (person != null) {
       if (person.getAddressDetails() != null) {
               if (person.getAddressDetails().getStreetNumber() != null) {
                       person1.getAddressDetails().setStreetNo(person.getAddressDetails().getStreetNumber());
               }

               if (person.getAddressDetails().getStreetName() != null) {
                       person1.getAddressDetails().setStreetName(person.getAddressDetails().getStreetName());
               }
       }

       if (person.getHomeDetails() != null) {
               if (person.getHomeDetails().getPhoneNumber() != null) {
                       person1.getHomeDetails().setSPhoneNo(person.getHomeDetails().getPhoneNumber());
               }
       }
}

我有大约 40 个节点需要复制,这会创建很多丑陋的代码。有没有人有更好的方法来做到这一点?也许如果我做一个映射或其他东西然后循环它?没有把握。

如果没有,有谁知道我是否可以让 BeanUtils 运行一个副本而不复制空值?

原因是第二个 bean person1 已经有一堆值。我只想在有新值可以覆盖它时覆盖它。

像往常一样,变量是更大系统的一部分,我无法标准化名称。

谢谢

I am writing some java code in the Spring Framework.

I've got two beans, person and person1. They have a slightly different structure, that is to say the variable names for each differ slightly.

I'm trying to copy the details from one bean to the other. I only want to copy the value if the value is not null. I've seen an API called BeanUtils, but this will copy it regardless if it is null or not.

Here's my code:

if (person != null) {
       if (person.getAddressDetails() != null) {
               if (person.getAddressDetails().getStreetNumber() != null) {
                       person1.getAddressDetails().setStreetNo(person.getAddressDetails().getStreetNumber());
               }

               if (person.getAddressDetails().getStreetName() != null) {
                       person1.getAddressDetails().setStreetName(person.getAddressDetails().getStreetName());
               }
       }

       if (person.getHomeDetails() != null) {
               if (person.getHomeDetails().getPhoneNumber() != null) {
                       person1.getHomeDetails().setSPhoneNo(person.getHomeDetails().getPhoneNumber());
               }
       }
}

I have about 40 nodes that need to be copied over and this would create so much ugly code. Does anyone have a better way to do this? maybe if i make a mapping or something and then loop through it? not sure.

If not, does anyone know if i can make BeanUtils run a copy without copying the null values?

Reason is that the second bean, person1, already has a bunch of values. I only want to overwrite that if there are new values to overwrite it with.

As usual the variables are part of a much larger system and I can't standardise the names.

Thanks

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

不知所踪 2024-11-12 05:59:59

从另一个方向解决问题,您的源数据对象不应该关心目标对象的约束或业务逻辑要求。

这将是紧耦合,这很糟糕,特别是如果您使用 Spring,您正在做的正是 IoC 容器(例如 Spring)它试图帮助您做的事情。

null 检查 编码放入目标对象的 setXXX() 方法中,如果您不想设置业务规则,那么这是处理业务规则的正确位置如果源属性为 null,则目标属性。

public setXXX(final String s)
{
  if (s == null) { // do nothing }
  else { this.xxx = s; }
}

那么您可以使用您想要的任何映射策略/库,而不必担心源数据 null 状态。盲目设置属性并让目标决定何时忽略传入的 null

Approach the problem from the other direction, your source data object should not care about constraints or business logic requirement of your target object.

That would be tight coupling and that is bad, especially if you are using Spring, you are doing exactly what an IoC container such as Spring it trying to help you not do.

Put the null checking coding in your setXXX() methods of the target object, that is the correct place to handle a business rule if you don't ever want to set the target properties if the source property is null.

public setXXX(final String s)
{
  if (s == null) { // do nothing }
  else { this.xxx = s; }
}

then you could use whatever mapping strategy / library you want and not worry about the source data null status. Blindly setting properties and letting the target decide when to ignore the incoming null.

━╋う一瞬間旳綻放 2024-11-12 05:59:59

我想知道 Dozer 是否可以帮助您。

I wonder if Dozer might help you out.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文