|
@@ -1,14 +1,23 @@
|
|
|
package com.gqy.common.utils.bean;
|
|
|
|
|
|
+import java.beans.BeanInfo;
|
|
|
+import java.beans.Introspector;
|
|
|
import java.lang.reflect.Method;
|
|
|
+import java.text.SimpleDateFormat;
|
|
|
import java.util.ArrayList;
|
|
|
+import java.util.HashMap;
|
|
|
import java.util.List;
|
|
|
+import java.util.Map;
|
|
|
import java.util.regex.Matcher;
|
|
|
import java.util.regex.Pattern;
|
|
|
+import java.util.stream.Collectors;
|
|
|
+import java.beans.PropertyDescriptor;
|
|
|
+import java.util.*;
|
|
|
+
|
|
|
|
|
|
/**
|
|
|
* Bean 工具类
|
|
|
- *
|
|
|
+ *
|
|
|
* @author raycos
|
|
|
*/
|
|
|
public class BeanUtils extends org.springframework.beans.BeanUtils
|
|
@@ -24,7 +33,7 @@ public class BeanUtils extends org.springframework.beans.BeanUtils
|
|
|
|
|
|
/**
|
|
|
* Bean属性复制工具方法。
|
|
|
- *
|
|
|
+ *
|
|
|
* @param dest 目标对象
|
|
|
* @param src 源对象
|
|
|
*/
|
|
@@ -42,7 +51,7 @@ public class BeanUtils extends org.springframework.beans.BeanUtils
|
|
|
|
|
|
/**
|
|
|
* 获取对象的setter方法。
|
|
|
- *
|
|
|
+ *
|
|
|
* @param obj 对象
|
|
|
* @return 对象的setter方法列表
|
|
|
*/
|
|
@@ -70,7 +79,7 @@ public class BeanUtils extends org.springframework.beans.BeanUtils
|
|
|
|
|
|
/**
|
|
|
* 获取对象的getter方法。
|
|
|
- *
|
|
|
+ *
|
|
|
* @param obj 对象
|
|
|
* @return 对象的getter方法列表
|
|
|
*/
|
|
@@ -97,7 +106,7 @@ public class BeanUtils extends org.springframework.beans.BeanUtils
|
|
|
/**
|
|
|
* 检查Bean方法名中的属性名是否相等。<br>
|
|
|
* 如getName()和setName()属性名一样,getName()和setAge()属性名不一样。
|
|
|
- *
|
|
|
+ *
|
|
|
* @param m1 方法名1
|
|
|
* @param m2 方法名2
|
|
|
* @return 属性名一样返回true,否则返回false
|
|
@@ -107,4 +116,163 @@ public class BeanUtils extends org.springframework.beans.BeanUtils
|
|
|
{
|
|
|
return m1.substring(BEAN_METHOD_PROP_INDEX).equals(m2.substring(BEAN_METHOD_PROP_INDEX));
|
|
|
}
|
|
|
+
|
|
|
+ /**
|
|
|
+ * Map转Bean
|
|
|
+ *
|
|
|
+ * @param map Map对象
|
|
|
+ * @param clazz 目标Bean类型
|
|
|
+ * @return Bean对象
|
|
|
+ */
|
|
|
+ public static <T> T mapToBean(Map<String, Object> map, Class<T> clazz) {
|
|
|
+ try {
|
|
|
+ T bean = clazz.newInstance();
|
|
|
+ if (map != null) {
|
|
|
+ for (Map.Entry<String, Object> entry : map.entrySet()) {
|
|
|
+ String propertyName = entry.getKey();
|
|
|
+ Object value = entry.getValue();
|
|
|
+ if (value != null) {
|
|
|
+ // 处理日期类型
|
|
|
+ if (value instanceof String) {
|
|
|
+ String strValue = (String) value;
|
|
|
+ if (isDateString(strValue)) {
|
|
|
+ value = parseDate(strValue);
|
|
|
+ }
|
|
|
+ }
|
|
|
+ setProperty(bean, propertyName, value);
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ return bean;
|
|
|
+ } catch (Exception e) {
|
|
|
+ throw new RuntimeException("Map转Bean异常", e);
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * Bean转Map
|
|
|
+ *
|
|
|
+ * @param bean Bean对象
|
|
|
+ * @return Map对象
|
|
|
+ */
|
|
|
+ public static Map<String, Object> beanToMap(Object bean) {
|
|
|
+ if (bean == null) {
|
|
|
+ return null;
|
|
|
+ }
|
|
|
+ Map<String, Object> map = new HashMap<>();
|
|
|
+ try {
|
|
|
+ BeanInfo beanInfo = Introspector.getBeanInfo(bean.getClass());
|
|
|
+ PropertyDescriptor[] propertyDescriptors = beanInfo.getPropertyDescriptors();
|
|
|
+ for (PropertyDescriptor property : propertyDescriptors) {
|
|
|
+ String key = property.getName();
|
|
|
+ if (key.compareToIgnoreCase("class") == 0) {
|
|
|
+ continue;
|
|
|
+ }
|
|
|
+ Method getter = property.getReadMethod();
|
|
|
+ Object value = getter != null ? getter.invoke(bean) : null;
|
|
|
+ map.put(key, value);
|
|
|
+ }
|
|
|
+ return map;
|
|
|
+ } catch (Exception e) {
|
|
|
+ throw new RuntimeException("Bean转Map异常", e);
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 设置Bean属性值
|
|
|
+ */
|
|
|
+ private static void setProperty(Object bean, String propertyName, Object value) throws Exception {
|
|
|
+ PropertyDescriptor property = new PropertyDescriptor(propertyName, bean.getClass());
|
|
|
+ Method setter = property.getWriteMethod();
|
|
|
+ if (setter != null) {
|
|
|
+ Class<?> paramType = setter.getParameterTypes()[0];
|
|
|
+ Object convertedValue = convertValue(value, paramType);
|
|
|
+ setter.invoke(bean, convertedValue);
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 转换值类型
|
|
|
+ */
|
|
|
+ private static Object convertValue(Object value, Class<?> targetType) {
|
|
|
+ if (value == null) {
|
|
|
+ return null;
|
|
|
+ }
|
|
|
+
|
|
|
+ try {
|
|
|
+ // 处理基本数据类型
|
|
|
+ if (targetType == Integer.class || targetType == int.class) {
|
|
|
+ return Integer.parseInt(value.toString());
|
|
|
+ }
|
|
|
+ if (targetType == Long.class || targetType == long.class) {
|
|
|
+ return Long.parseLong(value.toString());
|
|
|
+ }
|
|
|
+ if (targetType == Double.class || targetType == double.class) {
|
|
|
+ return Double.parseDouble(value.toString());
|
|
|
+ }
|
|
|
+ if (targetType == Float.class || targetType == float.class) {
|
|
|
+ return Float.parseFloat(value.toString());
|
|
|
+ }
|
|
|
+ if (targetType == Boolean.class || targetType == boolean.class) {
|
|
|
+ return Boolean.parseBoolean(value.toString());
|
|
|
+ }
|
|
|
+ if (targetType == Date.class && value instanceof String) {
|
|
|
+ return parseDate((String) value);
|
|
|
+ }
|
|
|
+ // 如果目标类型就是String,直接返回toString结果
|
|
|
+ if (targetType == String.class) {
|
|
|
+ return value.toString();
|
|
|
+ }
|
|
|
+ } catch (Exception e) {
|
|
|
+ throw new RuntimeException("类型转换异常: " + value + " to " + targetType, e);
|
|
|
+ }
|
|
|
+ return value;
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 判断是否为日期字符串
|
|
|
+ */
|
|
|
+ private static boolean isDateString(String str) {
|
|
|
+ return str.matches("\\d{4}-\\d{2}-\\d{2}") ||
|
|
|
+ str.matches("\\d{4}-\\d{2}-\\d{2} \\d{2}:\\d{2}:\\d{2}");
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 解析日期字符串
|
|
|
+ */
|
|
|
+ private static Date parseDate(String dateStr) {
|
|
|
+ try {
|
|
|
+ if (dateStr.length() == 10) {
|
|
|
+ return new SimpleDateFormat("yyyy-MM-dd").parse(dateStr);
|
|
|
+ }
|
|
|
+ return new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").parse(dateStr);
|
|
|
+ } catch (Exception e) {
|
|
|
+ throw new RuntimeException("日期解析异常: " + dateStr, e);
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * List<Map>转List<Bean>
|
|
|
+ */
|
|
|
+ public static <T> List<T> mapListToBeanList(List<Map<String, Object>> mapList, Class<T> clazz) {
|
|
|
+ if (mapList == null || mapList.isEmpty()) {
|
|
|
+ return new ArrayList<>();
|
|
|
+ }
|
|
|
+ return mapList.stream()
|
|
|
+ .map(map -> mapToBean(map, clazz))
|
|
|
+ .collect(Collectors.toList());
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * List<Bean>转List<Map>
|
|
|
+ */
|
|
|
+ public static <T> List<Map<String, Object>> beanListToMapList(List<T> beanList) {
|
|
|
+ if (beanList == null || beanList.isEmpty()) {
|
|
|
+ return new ArrayList<>();
|
|
|
+ }
|
|
|
+ return beanList.stream()
|
|
|
+ .map(BeanUtils::beanToMap)
|
|
|
+ .collect(Collectors.toList());
|
|
|
+ }
|
|
|
+
|
|
|
}
|