zoukankan      html  css  js  c++  java
  • java bean 合并

    package com.hainabo.mgcmall.util;

    import java.beans.BeanInfo;
    import java.beans.Introspector;
    import java.beans.PropertyDescriptor;

    public class BeanUtils {

    public static <M> void merge(M target, M destination) throws Exception {
    BeanInfo beanInfo = Introspector.getBeanInfo(target.getClass());

    // 遍历所有属性
    for (PropertyDescriptor descriptor : beanInfo.getPropertyDescriptors()) {
    // 允许读写
    if (descriptor.getWriteMethod() != null) {
    //Object originalValue = descriptor.getReadMethod().invoke(target);//target中(descriptor)的值
    // Only copy values values where the destination values is null
    //if (originalValue == null) {
    Object defaultValue = descriptor.getReadMethod().invoke(destination);//destination中(descriptor)的值
    if(defaultValue!=null && !"".equals(defaultValue)){
    descriptor.getWriteMethod().invoke(target, defaultValue);//用defaultValue覆盖到target
    }
    //}
    }
    }
    }
    }



    此方法会将 destination中!""&!null 的值覆盖到 target中
  • 相关阅读:
    ssh
    datetime
    网络-sdn(2)
    django-drf
    Vscode前段常用插件
    Vscode离线安装插件
    CSS 实现绘制各种三角形
    flex布局
    js实现全选和取消全选
    购物车用Ajax向后台传参
  • 原文地址:https://www.cnblogs.com/liuq1991/p/7918445.html
Copyright © 2011-2022 走看看