mapstruct更新现有实体忽略null空值

用JPA在做更新的时候经常要判断不是null的字段进行set很麻烦。

例如:目标实体 A.a1=1,A.a2=2 源实体 B.a1=null,B.a2=1。要实现转换后 A.a1=1,A.a2=1。

1、class上加注解。这个注解参数好像1.3版本以后才开始有的。

@Mapper(nullValuePropertyMappingStrategy = NullValuePropertyMappingStrategy.IGNORE)

2、必须用更新现有的Bean的方法。

void updateAFromB(B b, @MappingTarget A a);

mapstruct 1.3.1版文档原文

10.6. Controlling mapping result for ‘null’ properties in bean mappings (update mapping methods only).

MapStruct offers control over the property to set in an@MappingTargetannotated target bean when the source property equalsnullor the presence check method results in ‘absent’.

By default the target property will be set to null.

However:

  1. By specifyingnullValuePropertyMappingStrategy = NullValuePropertyMappingStrategy.SET_TO_DEFAULTon@Mapping,@BeanMapping,@Mapperor@MappingConfig, the mapping result can be altered to returndefaultvalues. ForListMapStruct generates anArrayList, forMapaHashMap, for arrays an empty array, forString``""and for primitive / boxed types a representation offalseor0. For all other objects an new instance is created. Please note that a default constructor is required. If not available, use the@Mapping#defaultValue.

  2. By specifyingnullValuePropertyMappingStrategy = NullValuePropertyMappingStrategy.IGNOREon@Mapping,@BeanMapping,@Mapperor@MappingConfig, the mapping result will be equal to the original value of the@MappingTargetannotated target.

The strategy works in a hierarchical fashion. SettingMapping#nullValuePropertyMappingStrategyon mapping level will overridenullValuePropertyMappingStrategyon mapping method level will override@Mapper#nullValuePropertyMappingStrategy, and@Mapper#nullValuePropertyMappingStrategywill override@MappingConfig#nullValuePropertyMappingStrategy.

  • 1
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
在使用mapstruct时,可以将null值转换为空值。具体来说,可以使用mapstruct提供的`nullValueMappingStrategy`来定义null值的处理方式。其中,可以选择的处理方式包括返回默认值、返回空字符串等。通过设置`nullValueMappingStrategy`为`RETURN_DEFAULT`,即可将null值转换为空值。这样,在转换过程中,当源对象的属性为null时,目标对象的属性将被设置为默认值。通过这种方式,可以实现mapstructnull转为空的转换。 同时,在项目中使用mapstruct和lombok时,需要将lombok依赖放在mapstruct依赖的上面,以确保mapstruct可以正常解析并生成对应的代码。在某些情况下,可能会出现属性未赋值的情况,导致最终结果为null。在这种情况下,可以检查代码逻辑,确保属性正确赋值,以避免出现null值。<span class="em">1</span><span class="em">2</span><span class="em">3</span> #### 引用[.reference_title] - *1* [go-interfacetools:与Go界面进行交互的各种工具,例如将地图复制到struct](https://download.csdn.net/download/weixin_42156940/18333780)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_1"}}] [.reference_item style="max-width: 33.333333333333336%"] - *2* [mapstruct 对象转换,属性为null](https://blog.csdn.net/qq_37122312/article/details/126208508)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_1"}}] [.reference_item style="max-width: 33.333333333333336%"] - *3* [使用mapstruct.Mapper 转换对象,为null](https://blog.csdn.net/qq_41169544/article/details/127675917)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_1"}}] [.reference_item style="max-width: 33.333333333333336%"] [ .reference_list ]

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值