| | |
| | | import com.baomidou.mybatisplus.core.toolkit.StringUtils; |
| | | import org.slf4j.Logger; |
| | | import org.slf4j.LoggerFactory; |
| | | import org.springframework.beans.BeanUtils; |
| | | import org.springframework.beans.BeanWrapper; |
| | | import org.springframework.beans.BeanWrapperImpl; |
| | | import org.springframework.util.CollectionUtils; |
| | | import org.springframework.util.ObjectUtils; |
| | | |
| | | import java.beans.BeanInfo; |
| | | import java.beans.IntrospectionException; |
| | | import java.beans.Introspector; |
| | | import java.beans.PropertyDescriptor; |
| | | import java.lang.reflect.Field; |
| | | import java.lang.reflect.InvocationTargetException; |
| | | import java.math.BigDecimal; |
| | | import java.util.*; |
| | | |
| | | public class BeanUtilForVCI { |
| | |
| | | return result; |
| | | } |
| | | } |
| | | |
| | | /** |
| | | * 将一个 Map 对象转化为一个 JavaBean |
| | | * @param clazz 要转化的类型 |
| | | * @param map 包含属性值的 map |
| | | * @return 转化出来的 JavaBean 对象 |
| | | * @throws IntrospectionException |
| | | * 如果分析类属性失败 |
| | | * @throws IllegalAccessException |
| | | * 如果实例化 JavaBean 失败 |
| | | * @throws InstantiationException |
| | | * 如果实例化 JavaBean 失败 |
| | | * @throws InvocationTargetException |
| | | * 如果调用属性的 setter 方法失败 |
| | | */ |
| | | public static <T>T convertMap(Class<T> clazz, Map map) throws IntrospectionException, IllegalAccessException, InstantiationException, InvocationTargetException { |
| | | BeanInfo beanInfo = Introspector.getBeanInfo(clazz); // 获取类属性 |
| | | T obj = clazz.newInstance(); |
| | | // 给 JavaBean 对象的属性赋值 |
| | | PropertyDescriptor[] propertyDescriptors = beanInfo.getPropertyDescriptors(); |
| | | for (int i = 0; i< propertyDescriptors.length; i++) { |
| | | PropertyDescriptor descriptor = propertyDescriptors[i]; |
| | | String propertyName = descriptor.getName(); |
| | | |
| | | if (map.containsKey(propertyName)) { |
| | | // 下面一句可以 try 起来,这样当一个属性赋值失败的时候就不会影响其他属性赋值。 |
| | | Object value = map.get(propertyName); |
| | | |
| | | Object[] args = new Object[1]; |
| | | args[0] = value; |
| | | Field privateField = getPrivateField(propertyName, clazz); |
| | | if (privateField == null) { |
| | | } |
| | | privateField.setAccessible(true); |
| | | String type = privateField.getGenericType().toString(); |
| | | if (type.equals("class java.lang.String")) { |
| | | privateField.set(obj, value); |
| | | } else if (type.equals("class java.lang.Boolean")) { |
| | | privateField.set(obj, Boolean.parseBoolean(String.valueOf(value))); |
| | | } else if (type.equals("class java.lang.Long")) { |
| | | privateField.set(obj, Long.parseLong(String.valueOf(value))); |
| | | } else if (type.equals("class java.lang.Integer")) { |
| | | privateField.set(obj, Integer.parseInt(String.valueOf(value))); |
| | | } else if (type.equals("class java.lang.Double")) { |
| | | privateField.set(obj,Double.parseDouble(String.valueOf(value))); |
| | | } else if (type.equals("class java.lang.Float")) { |
| | | privateField.set(obj,Float.parseFloat(String.valueOf(value))); |
| | | } else if (type.equals("class java.math.BigDecimal")){ |
| | | privateField.set(obj,new BigDecimal(String.valueOf(value))); |
| | | }//可继续追加类型 |
| | | } |
| | | } |
| | | return obj; |
| | | } |
| | | /*拿到反射父类私有属性*/ |
| | | private static Field getPrivateField(String name, Class cls) { |
| | | Field declaredField = null; |
| | | try { |
| | | declaredField = cls.getDeclaredField(name); |
| | | } catch (NoSuchFieldException ex) { |
| | | |
| | | if (cls.getSuperclass() == null) { |
| | | return declaredField; |
| | | } else { |
| | | declaredField = getPrivateField(name, cls.getSuperclass()); |
| | | } |
| | | } |
| | | return declaredField; |
| | | } |
| | | |
| | | /** |
| | | * 获取到对象中属性为null的属性名 |
| | | * |
| | | * @param source |
| | | * @return |
| | | */ |
| | | private static String[] getNullPropertyNames(Object source) { |
| | | final BeanWrapper src = new BeanWrapperImpl(source); |
| | | PropertyDescriptor[] pds = src.getPropertyDescriptors(); |
| | | |
| | | Set<String> emptyNames = new HashSet<>(); |
| | | for (PropertyDescriptor pd : pds) { |
| | | Object srcValue = src.getPropertyValue(pd.getName()); |
| | | if (ObjectUtils.isEmpty(srcValue)) { |
| | | emptyNames.add(pd.getName()); |
| | | } |
| | | } |
| | | String[] result = new String[emptyNames.size()]; |
| | | return emptyNames.toArray(result); |
| | | } |
| | | |
| | | /** |
| | | * 拷贝非空对象属性值 |
| | | * |
| | | * @param source |
| | | * @param target |
| | | */ |
| | | public static void copyPropertiesIgnoreNull(Object source, Object target) { |
| | | BeanUtils.copyProperties(source, target, getNullPropertyNames(source)); |
| | | } |
| | | |
| | | /** |
| | | * 集合拷贝非空数据 |
| | | * |
| | | * @param source 数据源 |
| | | * @param target 目标 |
| | | * @param propertyName 要匹配的属性名,例如两个集合使用id进行匹配拷贝 propertyName: "id" |
| | | */ |
| | | public static void copyListPropertiesIgnoreNull(List<?> source, List<?> target, String propertyName) { |
| | | if (CollectionUtils.isEmpty(source)) { |
| | | throw new NullPointerException("copyListPropertiesIgnoreNull source源数据为空!"); |
| | | } |
| | | Map<Object, Object> map = new HashMap<>(source.size()); |
| | | source.forEach(s -> { |
| | | final BeanWrapper sourceBean = new BeanWrapperImpl(s); |
| | | Object value = sourceBean.getPropertyValue(propertyName); |
| | | if (value == null) { |
| | | throw new NullPointerException("copyListPropertiesIgnoreNull获取参数异常"); |
| | | } |
| | | map.put(value, s); |
| | | }); |
| | | target.forEach(s -> { |
| | | final BeanWrapper targetBean = new BeanWrapperImpl(s); |
| | | Object value = targetBean.getPropertyValue(propertyName); |
| | | if (value == null) { |
| | | throw new NullPointerException("copyListPropertiesIgnoreNull获取参数异常"); |
| | | } |
| | | Object o = map.get(value); |
| | | copyPropertiesIgnoreNull(o, s); |
| | | }); |
| | | } |
| | | |
| | | } |