This commit is contained in:
Looly 2022-10-12 14:42:43 +08:00
parent 9ecd578896
commit dff3dfec68
2 changed files with 19 additions and 0 deletions

View File

@ -50,6 +50,10 @@ public class MapToMapCopier extends AbsCopier<Map, Map> {
return;
}
sValue = entry.getValue();
// 忽略空值
if (copyOptions.ignoreNullValue && sValue == null) {
return;
}
final Object targetValue = target.get(sKey);
// 非覆盖模式下如果目标值存在则跳过

View File

@ -575,6 +575,21 @@ public class BeanUtilTest {
Assert.assertNull(BeanUtil.copyProperties(null, Food.class));
}
@Test
public void copyPropertiesMapToMapIgnoreNullTest() {
// 测试MapToMap
final Map<String, Object> p1 = new HashMap<>();
p1.put("isSlow", true);
p1.put("name", "测试");
p1.put("subName", null);
final Map<String, Object> map = MapUtil.newHashMap();
BeanUtil.copyProperties(p1, map, CopyOptions.of().setIgnoreNullValue(true));
Assert.assertTrue((Boolean) map.get("isSlow"));
Assert.assertEquals("测试", map.get("name"));
Assert.assertFalse(map.containsKey("subName"));
}
@Test
public void copyBeanPropertiesFilterTest() {
final Food info = new Food();