广告
返回顶部
首页 > 资讯 > 后端开发 > Python >使用res:bean属性复制避免null值覆盖版本
  • 619
分享到

使用res:bean属性复制避免null值覆盖版本

2024-04-02 19:04:59 619人浏览 薄情痞子

Python 官方文档:入门教程 => 点击学习

摘要

目录res:bean属性复制避免null值覆盖版本前言代码 copyBeanPropertiesIGoreNullBeanUtils.copyProperties解决null值覆盖可

res:bean属性复制避免null值覆盖版本

前言

  • 最近在设计通用的 Service 和 Controller 层
  • 设计过程中涉及到实体对象(JPA)的更新操作
  • 原因1:JPA 的 saveAndFlush 方法会将把 null 也更新上去
  • 原因2:spring 的 BeanUtils.copyBeanProperties 方法会把 src 所有属性值包括 null 覆盖到 dest,不符合要求
  • 所以,利用反射,写一个属性复制方法代替 spring 的工具方法
  • 另外,controller 层使用 @ModelAttribut 也可以解决这个问题,这就是另一个主题

代码 copyBeanPropertiesIgoreNull



public static void copyBeanPropertiesIgoreNull(Object beanSrc, Object beanDest){
 Class<?> clazzSrc = beanSrc.getClass();
 Class<?> clazzDest = beanDest.getClass();
 //获取所有属性,包括私有的和继承的
 List<Field> allFields = getAllFields(beanSrc);
 try {
 for(Field field:allFields) {
  String fieldName = field.getName(); //属性名
  if("serialVersionUID".equals(fieldName)) {
   continue;
  }
  PropertyDescriptor pd1 = getPropertyDescriptor(fieldName, clazzSrc);
  if(pd1!=null) {
   Method rMethod = pd1.getReadMethod();
   if(rMethod!=null) {
    Object fieldValue = rMethod.invoke(beanSrc); //属性值,引用类型,所以一般实体的属性用 Integer instead of int
    if(fieldValue!=null) { //关键:属性值为 null 就不要覆盖了
     PropertyDescriptor pd2 = getPropertyDescriptor(fieldName, clazzDest);
     if(pd2!=null) {
      Method wMethod = pd2.getWriteMethod();
      if(wMethod!=null) {
        wMethod.invoke(beanDest, fieldValue);
      }
     }
    }
   }
  }
 }
 } catch (IllegalAccessException | InvocationTargetException e) {
  e.printStackTrace();
  throw new RuntimeException(">> copyPropertiesIgoreNull exception", e);
 }
}

BeanUtils.copyProperties解决null值覆盖

这里使用的是Spring提供的BeanUtils的工具类(commons-lang3可参考)。在做数据变更的时候,使用BeanUtils.copyProperties(newdata,dbdata)进行数据变更的时候,由于前台展示的数据不完整。

导致前台传递的数据将后台的原始数据全部覆盖掉。那么如何解决这种null值的覆盖呢。BeanUtils.copyProperties()可以通过添加可变长参数忽略掉具体的某些不需要更新的字段。比如BeanUtils.copyProperties(newdata,dbdata,“id”,“passWord”)。

那么如果字段比较多,使用上面的方法简直要疯掉了。有没有更好的方法呢?

可以自己拓展一个方法,汇总值为null的数据


public static String[] getNullPropertyNames (Object source) {
        final BeanWrapper src = new BeanWrapperImpl(source);
        PropertyDescriptor[] pds = src.getPropertyDescriptors();
        Set<String> emptyNames = new HashSet<String>();
        for(PropertyDescriptor pd : pds) {
            Object srcValue = src.getPropertyValue(pd.getName());
            if (srcValue == null) emptyNames.add(pd.getName());
        }
        String[] result = new String[emptyNames.size()];
        return emptyNames.toArray(result);
    }

通过这个方法就可以将null数据找到,然后在可变长参数中使用方法即可。


BeanUtils.copyProperties(newdata,dbdata,getNullPropertyNames(newdata));

附demo:


public static void main(String[] args) {
    U u = new U("1","zhangsan",null,null,null,"11");
    String[] nullPropertyNames = getNullPropertyNames(u);
    for (String nullPropertyName : nullPropertyNames) {
        System.out.println(nullPropertyName);
    }
}
public static String[] getNullPropertyNames (Object source) {
    final BeanWrapper src = new BeanWrapperImpl(source);
    PropertyDescriptor[] pds = src.getPropertyDescriptors();
    Set<String> emptyNames = new HashSet<String>();
    for(PropertyDescriptor pd : pds) {
        Object srcValue = src.getPropertyValue(pd.getName());
        if (srcValue == null) emptyNames.add(pd.getName());
    }
    String[] result = new String[emptyNames.size()];
    return emptyNames.toArray(result);
}
class U {
        private String id;
        private String name;
        private String field1;
        private String field2;
        private String field3;
        private String field4;
        public U(String id, String name) {
            this.id = id;
            this.name = name;
        }
        public String getId() {
            return id;
        }
        public void setId(String id) {
            this.id = id;
        }
        public String getName() {
            return name;
        }
        public void setName(String name) {
            this.name = name;
        }
        public String getField1() {
            return field1;
        }
        public void setField1(String field1) {
            this.field1 = field1;
        }
        public String getField2() {
            return field2;
        }
        public void setField2(String field2) {
            this.field2 = field2;
        }
        public String getField3() {
            return field3;
        }
        public void setField3(String field3) {
            this.field3 = field3;
        }
        public String getField4() {
            return field4;
        }
        public void setField4(String field4) {
            this.field4 = field4;
        }
        public U(String id, String name, String field1, String field2, String field3, String field4) {
            this.id = id;
            this.name = name;
            this.field1 = field1;
            this.field2 = field2;
            this.field3 = field3;
            this.field4 = field4;
        }
    }

打印的结果:

field1

field2

field3

好了问题解决!以上为个人经验,希望能给大家一个参考,也希望大家多多支持编程网。

--结束END--

本文标题: 使用res:bean属性复制避免null值覆盖版本

本文链接: https://www.lsjlt.com/news/134059.html(转载时请注明来源链接)

有问题或投稿请发送至: 邮箱/279061341@qq.com    QQ/279061341

本篇文章演示代码以及资料文档资料下载

下载Word文档到电脑,方便收藏和打印~

下载Word文档
猜你喜欢
软考高级职称资格查询
编程网,编程工程师的家园,是目前国内优秀的开源技术社区之一,形成了由开源软件库、代码分享、资讯、协作翻译、讨论区和博客等几大频道内容,为IT开发者提供了一个发现、使用、并交流开源技术的平台。
  • 官方手机版

  • 微信公众号

  • 商务合作