BeanUtils.copyProperties设置忽略null字段
生活随笔
收集整理的這篇文章主要介紹了
BeanUtils.copyProperties设置忽略null字段
小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.
1、
/*** 返回實(shí)體的所有 null 字段* @param source* @return*/public static String[] getNullPropertyNames (Object source) {final BeanWrapper src = new BeanWrapperImpl(source);java.beans.PropertyDescriptor[] pds = src.getPropertyDescriptors();Set<String> emptyNames = new HashSet<>();for(java.beans.PropertyDescriptor pd : pds) {Object srcValue = src.getPropertyValue(pd.getName());if (srcValue == null) emptyNames.add(pd.getName());}String[] result = new String[emptyNames.size()];String[] nullPropertyNames = emptyNames.toArray(result);return nullPropertyNames;}2、
/*** 僅更新非null屬性,防止原始值被null覆蓋* @param source* @param id*/ public Project updateNotNull(Project source){Project target = this.get(source.getId());BeanUtils.copyProperties(source, target, getNullPropertyNames(source));Project update = this.update(target);return update; }總結(jié)
以上是生活随笔為你收集整理的BeanUtils.copyProperties设置忽略null字段的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: CSDN豪华盲盒, 一发入魂直接获得iP
- 下一篇: EJB概述