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中
  • 相关阅读:
    关于JVM的一些想法
    hashMap理解以及jdk1.7、jdk1.8其中区别
    各数据库如何实现自增
    dubbo遇坑记录
    mysql建表语句问题
    @Configuration
    生成一个唯一的ID
    门面模式
    关于getClass().getClassLoader()
    元素链
  • 原文地址:https://www.cnblogs.com/liuq1991/p/7918445.html
Copyright © 2011-2022 走看看