如何使用 MapStruct 忽略零值

发布于 2025-01-10 18:29:59 字数 242 浏览 3 评论 0原文

我正在尝试根据 http 请求发送的内容更新 postgres 记录,这是不可预测的(可以是属性的任意组合)。现在,我想做的是让 MapStruct 忽略“默认”Java 值(即未包含在请求正文中的值),但它只忽略 null 值,因此不忽略默认 int 类型值(即 0)。我已经用表达式完成了它,但这不是一个非常优雅的解决方案。我也尝试过使用 Integer 类型,但 MapStruct 仍然将其视为普通 int 。我的问题是:有没有一种解决方法可以忽略等于零的属性?

I am trying to update a postgres record based on what the http request is sending, which is unpredictable (can be any combination of attributes). Now, what i want to do is to get MapStruct to ignore "default" Java values (i.e. the ones not included in the request body) but it only ignores null values, so not default int type values (which is 0). I've accomplished it with expressions but it is not a very elegant solution. I've also tried using Integer type but still MapStruct treats it like a common int. My question is: ¿Is there a workaround ignoring properties that are equal to zero?

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

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

发布评论

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

评论(1

天气好吗我好吗 2025-01-17 18:29:59

Mapstruct 1.5.0(当前为 1.5.0.Beta2)引入了 @Condition 注解。通过此功能,您可以定义自己的检查,以确定何时映射值。

例如,要映射所有非 0int,您可以执行以下操作:

@Condition
boolean shouldMap(int number) {
    return number != 0;
}

有关详细信息,请参阅 mapstruct 参考指南中的条件映射。

With mapstruct 1.5.0 (currently at 1.5.0.Beta2) the @Condition annotation is introduced. With this you can define your own checks for when to map values.

for example to map all ints that are not 0 you could do the following:

@Condition
boolean shouldMap(int number) {
    return number != 0;
}

For more information see Conditional Mapping at the mapstruct reference guide.

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