old mode 100644
new mode 100755
| | |
| | | package com.java110.utils.util; |
| | | |
| | | |
| | | import org.apache.commons.beanutils.BeanUtils; |
| | | import com.alibaba.fastjson.JSONArray; |
| | | import com.alibaba.fastjson.JSONObject; |
| | | import com.java110.dto.community.CommunityDto; |
| | | import org.apache.commons.beanutils.ConvertUtils; |
| | | import org.apache.commons.beanutils.Converter; |
| | | import org.apache.commons.beanutils.PropertyUtils; |
| | | import org.apache.commons.lang3.reflect.FieldUtils; |
| | | import org.springframework.cglib.beans.BeanCopier; |
| | | import org.springframework.cglib.beans.BeanMap; |
| | | |
| | | import java.lang.reflect.Field; |
| | | import java.text.ParseException; |
| | | import java.text.SimpleDateFormat; |
| | | import java.util.ArrayList; |
| | | import java.util.Date; |
| | | import java.util.List; |
| | | import java.util.Map; |
| | | import java.util.*; |
| | | |
| | | /** |
| | | * @ClassName BeanConvertUtil |
| | |
| | | static { |
| | | ConvertUtils.register(new Converter() { //注册一个日期转换器 |
| | | |
| | | public Object convert(Class type, Object value) { |
| | | public <T> T convert(Class<T> type, Object value) { |
| | | Date date1 = null; |
| | | if (value instanceof String) { |
| | | if (value instanceof String && type.getClass().equals(Date.class)) { |
| | | String date = (String) value; |
| | | SimpleDateFormat sdf = null; |
| | | if (date.contains(":")) { |
| | |
| | | } catch (ParseException e) { |
| | | e.printStackTrace(); |
| | | } |
| | | return date1; |
| | | return type.cast(date1); |
| | | } |
| | | return value; |
| | | return null; |
| | | } |
| | | |
| | | }, Date.class); |
| | | |
| | | |
| | | ConvertUtils.register(new Java110StringConvert(), String.class); |
| | | } |
| | |
| | | * @return 目标对象 |
| | | */ |
| | | public static <T1, T2> T2 covertBean(T1 orgBean, T2 dstBean) { |
| | | |
| | | if (orgBean == null) { |
| | | return dstBean; |
| | | } |
| | | try { |
| | | BeanUtils.copyProperties(dstBean, orgBean); |
| | | if (orgBean instanceof Map) { |
| | | BeanMap beanMap = BeanMap.create(dstBean); |
| | | objectFieldsPutMap(dstBean, beanMap, (Map<String, Object>) orgBean); |
| | | return dstBean; |
| | | } |
| | | final BeanCopier beanCopier = BeanCopier.create(orgBean.getClass(), dstBean.getClass(), true); |
| | | beanCopier.copy(orgBean, dstBean, new Java110Converter()); |
| | | } catch (Exception e) { |
| | | e.printStackTrace(); |
| | | throw new RuntimeException("bean转换bean失败", e); |
| | | } |
| | | return dstBean; |
| | | } |
| | | |
| | | private static void objectFieldsPutMap(Object dstBean, BeanMap beanMap, Map<String, Object> orgMap) { |
| | | Field[] fields = FieldUtils.getAllFields(dstBean.getClass()); |
| | | for (Field field : fields) { |
| | | String fieldName = field.getName(); |
| | | if (!orgMap.containsKey(fieldName)) { |
| | | continue; |
| | | } |
| | | |
| | | Class<?> targetType = field.getType(); |
| | | Object value = orgMap.get(fieldName); |
| | | if (value == null) { |
| | | continue; |
| | | } |
| | | |
| | | try { |
| | | // ✅ 核心修复:只要是 Map,且目标不是基础类型,就递归转 DTO |
| | | if (value instanceof Map) { |
| | | // 排除基础类型、包装类、String、Date |
| | | boolean isBasicType = targetType.isPrimitive() |
| | | || Number.class.isAssignableFrom(targetType) |
| | | || String.class == targetType |
| | | || Boolean.class == targetType |
| | | || Date.class == targetType; |
| | | |
| | | if (!isBasicType) { |
| | | // 递归创建并转换嵌套对象 |
| | | Object nestedObj = targetType.newInstance(); |
| | | objectFieldsPutMap(nestedObj, BeanMap.create(nestedObj), (Map<String, Object>) value); |
| | | value = nestedObj; |
| | | } |
| | | } |
| | | |
| | | // 原有类型转换(String->Date等) |
| | | Object convertedValue = Java110Converter.getValue(value, targetType); |
| | | beanMap.put(fieldName, convertedValue); |
| | | |
| | | } catch (Exception ignored) { |
| | | // 转换失败不抛错,保持原有逻辑兼容 |
| | | } |
| | | } |
| | | } |
| | | |
| | | /** |
| | |
| | | T2 returnModel = null; |
| | | try { |
| | | returnModel = t.newInstance(); |
| | | BeanUtils.copyProperties(returnModel, orgBean); |
| | | covertBean(orgBean, returnModel); |
| | | } catch (Exception e) { |
| | | e.printStackTrace(); |
| | | throw new RuntimeException("bean转换bean失败", e); |
| | |
| | | * @return map对象 |
| | | */ |
| | | public static Map beanCovertMap(Object orgBean) { |
| | | Map newMap = null; |
| | | |
| | | Map<String, Object> map = new HashMap(); |
| | | try { |
| | | newMap = PropertyUtils.describe(orgBean); |
| | | BeanMap beanMap = BeanMap.create(orgBean); |
| | | for (Object key : beanMap.keySet()) { |
| | | map.put(key + "", beanMap.get(key)); |
| | | } |
| | | } catch (Exception e) { |
| | | throw new RuntimeException("bean转换Map失败", e); |
| | | } |
| | | return map; |
| | | } |
| | | |
| | | return newMap; |
| | | /** |
| | | * bean转换为map对象 |
| | | * |
| | | * @param orgBean 原始bean |
| | | * @return map对象 |
| | | */ |
| | | public static JSONObject beanCovertJson(Object orgBean) { |
| | | |
| | | return JSONObject.parseObject(JSONObject.toJSONString(orgBean)); |
| | | } |
| | | |
| | | /** |
| | | * bean转换为map对象 |
| | | * |
| | | * @param orgBean 原始bean |
| | | * @return map对象 |
| | | */ |
| | | public static JSONArray beanCovertJSONArray(Object orgBean) { |
| | | |
| | | return JSONArray.parseArray(JSONArray.toJSONStringWithDateFormat(orgBean, "yyyy-MM-dd HH:mm:ss")); |
| | | } |
| | | |
| | | |
| | |
| | | } |
| | | return newMaps; |
| | | } |
| | | public static List<Map<String, Object>> beanCovertMapListC(List<CommunityDto> orgBeans) { |
| | | List<Map<String, Object>> newMaps = new ArrayList<Map<String, Object>>(); |
| | | Map<String, Object> newMap = null; |
| | | for (Object orgbean : orgBeans) { |
| | | newMap = beanCovertMap(orgbean); |
| | | newMaps.add(newMap); |
| | | } |
| | | return newMaps; |
| | | } |
| | | } |