Java - 如何简化我的java
我正在 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
从另一个方向解决问题,您的源数据对象不应该关心目标对象的约束或业务逻辑要求。
这将是紧耦合,这很糟糕,特别是如果您使用 Spring,您正在做的正是 IoC 容器(例如 Spring)它试图帮助您不做的事情。
将
null 检查
编码放入目标对象的setXXX()
方法中,如果您不想设置业务规则,那么这是处理业务规则的正确位置如果源属性为null
,则目标属性。那么您可以使用您想要的任何映射策略/库,而不必担心源数据
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 yoursetXXX()
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 isnull
.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 incomingnull
.我想知道 Dozer 是否可以帮助您。
I wonder if Dozer might help you out.