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中

相关文章:

  • 2021-11-09
  • 2022-02-07
  • 2021-05-27
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
猜你喜欢
  • 2022-01-07
  • 2021-11-19
  • 2022-02-17
  • 2022-12-23
  • 2021-11-01
  • 2022-02-25
  • 2021-06-10
相关资源
相似解决方案