Просмотр исходного кода

将apache 的 对象属性拷贝改为 cglib的 因为Apache的性能不行

wuxw лет назад: 6
Родитель
Сommit
a817bda052

+ 5 - 0
HardwareAdapationService/src/main/java/com/java110/hardwareAdapation/thread/TranslateOwnerToMachineChangeMachine.java

@@ -20,6 +20,7 @@ import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 
 import java.util.ArrayList;
+import java.util.Date;
 import java.util.HashMap;
 import java.util.List;
 import java.util.Map;
@@ -191,8 +192,10 @@ public class TranslateOwnerToMachineChangeMachine implements Runnable {
         info.put("objId", ownerDto.getMemberId());
         info.put("state", "10000");
         info.put("communityId", ownerDto.getCommunityId());
+        info.put("updateTime", new Date());
         machineTranslateServiceDaoImpl.updateMachineTranslate(info);
 
+
     }
 
     private void deleteMachineTranslate(MachineDto tmpMachineDto, OwnerDto ownerDto) {
@@ -202,6 +205,8 @@ public class TranslateOwnerToMachineChangeMachine implements Runnable {
         info.put("objId", ownerDto.getMemberId());
         info.put("statusCd", StatusConstant.STATUS_CD_INVALID);
         info.put("communityId", ownerDto.getCommunityId());
+        info.put("updateTime", new Date());
+
         machineTranslateServiceDaoImpl.updateMachineTranslate(info);
 
     }

+ 156 - 0
java110-utils/src/main/java/com/java110/utils/util/ApacheBeanConvertUtil.java

@@ -0,0 +1,156 @@
+package com.java110.utils.util;
+
+
+import org.apache.commons.beanutils.BeanUtils;
+import org.apache.commons.beanutils.ConvertUtils;
+import org.apache.commons.beanutils.Converter;
+import org.apache.commons.beanutils.PropertyUtils;
+
+import java.text.ParseException;
+import java.text.SimpleDateFormat;
+import java.util.ArrayList;
+import java.util.Date;
+import java.util.List;
+import java.util.Map;
+
+/**
+ * @ClassName BeanConvertUtil
+ * @Description bean 转化工具类
+ * @Author wuxw
+ * @Date 2019/4/24 12:53
+ * @Version 1.0
+ * add by wuxw 2019/4/24
+ **/
+public final class ApacheBeanConvertUtil {
+
+    private ApacheBeanConvertUtil() {
+    }
+
+    static {
+        ConvertUtils.register(new Converter() { //注册一个日期转换器
+
+            public Object convert(Class type, Object value) {
+                Date date1 = null;
+                if (value instanceof String) {
+                    String date = (String) value;
+                    SimpleDateFormat sdf = null;
+                    if (date.contains(":")) {
+                        sdf = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
+                    } else {
+                        sdf = new SimpleDateFormat("yyyy-MM-dd");
+                    }
+                    try {
+                        date1 = sdf.parse(date);
+                    } catch (ParseException e) {
+                        e.printStackTrace();
+                    }
+                    return date1;
+                }
+                return value;
+            }
+        }, Date.class);
+
+
+
+        ConvertUtils.register(new Java110StringConvert(), String.class);
+    }
+
+
+    /**
+     * 对象A转为对象B
+     * 这个也支持map转bean
+     *
+     * @param orgBean 原始对象
+     * @param dstBean 目标对象类
+     * @param <T1>    原始对象
+     * @param <T2>    目标对象
+     * @return 目标对象
+     */
+    public static <T1, T2> T2 covertBean(T1 orgBean, T2 dstBean) {
+
+        try {
+            BeanUtils.copyProperties(dstBean, orgBean);
+        } catch (Exception e) {
+            e.printStackTrace();
+            throw new RuntimeException("bean转换bean失败", e);
+        }
+        return dstBean;
+    }
+
+    /**
+     * 对象A转为对象B (类)
+     * 这个也支持map转bean
+     *
+     * @param orgBean 原始对象
+     * @param t       目标对象类
+     * @param <T1>    原始对象
+     * @param <T2>    目标对象
+     * @return 目标对象
+     */
+    public static <T1, T2> T2 covertBean(T1 orgBean, Class<T2> t) {
+
+        T2 returnModel = null;
+        try {
+            returnModel = t.newInstance();
+            BeanUtils.copyProperties(returnModel, orgBean);
+        } catch (Exception e) {
+            e.printStackTrace();
+            throw new RuntimeException("bean转换bean失败", e);
+        }
+        return returnModel;
+    }
+
+
+    /**
+     * 对象A集合转为对象B集合
+     *
+     * @param orgBeans 原始对象列表
+     * @param t        目标对象类
+     * @param <T1>     原始对象
+     * @param <T2>     目标对象
+     * @return 目标对象
+     */
+    public static <T1, T2> List<T2> covertBeanList(List<T1> orgBeans, Class<T2> t) {
+        List<T2> newBeanList = new ArrayList<T2>();
+        for (T1 orgbean : orgBeans) {
+            T2 newBean = covertBean(orgbean, t);
+            newBeanList.add(newBean);
+        }
+        return newBeanList;
+    }
+
+    /**
+     * bean转换为map对象
+     *
+     * @param orgBean 原始bean
+     * @return map对象
+     */
+    public static Map beanCovertMap(Object orgBean) {
+        Map newMap = null;
+
+        try {
+            newMap = PropertyUtils.describe(orgBean);
+        } catch (Exception e) {
+            throw new RuntimeException("bean转换Map失败", e);
+        }
+
+        return newMap;
+    }
+
+
+    /**
+     * bean集合转换为map对象集合
+     *
+     * @param orgBeans 原始bean 列表
+     * @return map对象 列表
+     */
+    public static List<Map<String, Object>> beanCovertMapList(List<Object> 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;
+    }
+}

+ 39 - 6
java110-utils/src/main/java/com/java110/utils/util/BeanConvertUtil.java

@@ -5,13 +5,18 @@ import org.apache.commons.beanutils.BeanUtils;
 import org.apache.commons.beanutils.ConvertUtils;
 import org.apache.commons.beanutils.Converter;
 import org.apache.commons.beanutils.PropertyUtils;
+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.HashMap;
 import java.util.List;
 import java.util.Map;
+import java.util.Objects;
 
 /**
  * @ClassName BeanConvertUtil
@@ -50,6 +55,7 @@ public final class BeanConvertUtil {
             }
         }, Date.class);
 
+
         ConvertUtils.register(new Java110StringConvert(), String.class);
     }
 
@@ -67,13 +73,38 @@ public final class BeanConvertUtil {
     public static <T1, T2> T2 covertBean(T1 orgBean, T2 dstBean) {
 
         try {
-            BeanUtils.copyProperties(dstBean, orgBean);
+            //BeanUtils.copyProperties(dstBean, orgBean);
+            if (orgBean instanceof Map) {
+                BeanMap beanMap = BeanMap.create(dstBean);
+                //beanMap.putAll((Map)orgBean);
+                objectFieldsPutMap(dstBean, beanMap, (Map) 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 orgMap) {
+        Field[] fields = dstBean.getClass().getDeclaredFields();
+        for (Field field : fields) {
+            if (!orgMap.containsKey(field.getName())) {
+                continue;
+            }
+            Class dstClass = field.getType();
+            //System.out.println("字段类型" + dstClass);
+
+            Object value = orgMap.get(field.getName());
+            //String 转date
+            Object tmpValue = Java110Converter.getValue(value, dstClass);
+            beanMap.put(field.getName(), tmpValue);
+        }
+    }
+
     /**
      * 对象A转为对象B (类)
      * 这个也支持map转bean
@@ -89,7 +120,7 @@ public final class BeanConvertUtil {
         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);
@@ -123,15 +154,17 @@ public final class BeanConvertUtil {
      * @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 newMap;
+        return map;
     }
 
 

+ 85 - 0
java110-utils/src/main/java/com/java110/utils/util/Java110Converter.java

@@ -0,0 +1,85 @@
+package com.java110.utils.util;
+
+import org.springframework.cglib.core.Converter;
+
+import java.math.BigDecimal;
+import java.sql.Timestamp;
+import java.text.ParseException;
+import java.text.SimpleDateFormat;
+import java.util.Date;
+
+/**
+ * @ClassName Java110Converter
+ * @Description TODO
+ * @Author wuxw
+ * @Date 2020/1/28 17:16
+ * @Version 1.0
+ * add by wuxw 2020/1/28
+ **/
+public class Java110Converter implements Converter {
+    static SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
+
+    @Override
+    public Object convert(Object value, Class target, Object context) {
+
+        return getValue(value, target);
+    }
+
+    public static Object getValue(Object value, Class target) {
+        //1.0 String 转 Date
+        if (value instanceof String && target == Date.class) {
+            String date = (String) value;
+            Date newDate = null;
+            if (date.contains(":")) {
+                //sdf = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
+            } else {
+                sdf = new SimpleDateFormat("yyyy-MM-dd");
+            }
+            try {
+                newDate = sdf.parse(date);
+            } catch (ParseException e) {
+                e.printStackTrace();
+            }
+            return newDate;
+        }
+
+        //2.0 Date 转 Date
+        if (value instanceof Date && target == Date.class) {
+            return value;
+        }
+
+        // 3.0 Date 转 String
+        if (value instanceof Date && target == String.class) {
+            Date date = (Date) value;
+            String newDate = null;
+            try {
+                newDate = sdf.format(date);
+            } catch (Exception e) {
+                e.printStackTrace();
+            }
+            return newDate;
+        }
+
+        if (value instanceof BigDecimal) {
+            BigDecimal bd = (BigDecimal) value;
+            return bd.toPlainString();
+        }
+        if (target == String.class) {
+            return String.valueOf(value);
+        }
+
+        if (target == int.class || target == Integer.class) {
+            return Integer.parseInt(String.valueOf(value));
+        }
+
+        if (target == long.class || target == Long.class) {
+            return Long.parseLong(String.valueOf(value));
+        }
+
+        if (target == double.class || target == Double.class) {
+            return Double.parseDouble(String.valueOf(value));
+        }
+
+        return value;
+    }
+}

+ 60 - 22
java110-utils/src/test/java/com/java110/utils/util/BeanConvertUtilTest.java

@@ -14,40 +14,78 @@ import java.util.HashMap;
 import java.util.Map;
 
 public class BeanConvertUtilTest extends TestCase {
-
+    /**
+     * bean 转为bean
+     *
+     * @throws IllegalAccessException
+     * @throws NoSuchMethodException
+     * @throws InvocationTargetException
+     */
     public void testCovertBean() throws IllegalAccessException, NoSuchMethodException, InvocationTargetException {
 
-        CommunityMemberDto communityMemberDto = new CommunityMemberDto();
-        communityMemberDto.setMemberTypeCd("123");
-        communityMemberDto.setStatusCd("1");
-        communityMemberDto.setMemberId("123123");
+        PersonDto personDto = new PersonDto();
+        personDto.setId(1);
+        personDto.setName("wuxw");
+        personDto.setCreateTime(new Date());
+        personDto.setAge(1);
+        PersonVo personVo = new PersonVo();
+        personVo = BeanConvertUtil.covertBean(personDto, personVo);
 
-        Map info = new HashMap();
+        System.out.println("dto 转 vo" + JSONObject.toJSONString(personVo));
+    }
+
+    /**
+     * bean 转为bean
+     *
+     * @throws IllegalAccessException
+     * @throws NoSuchMethodException
+     * @throws InvocationTargetException
+     */
+    public void testCovertBeanAgent() throws IllegalAccessException, NoSuchMethodException, InvocationTargetException {
 
-        //Map _info  = BeanConvertUtil.beanCovertMap(communityMemberDto);
-        Map _info  = BeanUtils.describe(communityMemberDto);
+        PersonVo personVo = new PersonVo();
+        personVo.setId("2");
+        personVo.setName("wuxw");
+        personVo.setCreateTime("2020-01-28 12:12:12");
+        PersonDto personDto = new PersonDto();
+        personDto = BeanConvertUtil.covertBean(personVo, personDto);
 
-        System.out.println(JSONObject.toJSONString(_info));
+        System.out.println("dto 转 vo" + JSONObject.toJSONString(personDto));
     }
 
-    public void testCoverBeanStringToDate(){
-        Map reqJson = new HashMap();
-        reqJson.put("startTime","2019-06-02 00:00:00");
-        //reqJson.put("endTime","2019-06-03");
+    /**
+     * bean 转为bean
+     *
+     * @throws IllegalAccessException
+     * @throws NoSuchMethodException
+     * @throws InvocationTargetException
+     */
+    public void testBeanCovertMap() throws IllegalAccessException, NoSuchMethodException, InvocationTargetException {
 
-        FeeDetailDto feeDetailDto = BeanConvertUtil.covertBean(reqJson, FeeDetailDto.class);
+        PersonDto personVo = new PersonDto();
+        personVo.setName("wuxw");
+        personVo.setCreateTime(new Date());
 
-        System.out.printf("feeDetailDto:"+ JSONObject.toJSONString(feeDetailDto));
-    }
+        Map map = BeanConvertUtil.beanCovertMap(personVo);
 
-    public void testCoverBeanDateToString(){
-        FeeDto feeDto = new FeeDto();
-        feeDto.setStartTime(new Date());
-        feeDto.setAmount("1.00");
+        System.out.println("bean 转 map" + JSONObject.toJSONString(map));
+    }
 
-        ApiFeeVo apiFeeVo = BeanConvertUtil.covertBean(feeDto, ApiFeeVo.class);
+    /**
+     * bean 转为bean
+     *
+     * @throws IllegalAccessException
+     * @throws NoSuchMethodException
+     * @throws InvocationTargetException
+     */
+    public void testMapCovertBean() throws IllegalAccessException, NoSuchMethodException, InvocationTargetException {
 
-        System.out.printf("apiFeeVo:"+ JSONObject.toJSONString(apiFeeVo));
+        Map info = new HashMap();
+        info.put("name", "wuxw");
+        info.put("createTime", new Date());
+        PersonDto personDto = null;
+         personDto = BeanConvertUtil.covertBean(info,PersonDto.class);
 
+        System.out.println("map 转 bean" + JSONObject.toJSONString(personDto));
     }
 }

+ 54 - 0
java110-utils/src/test/java/com/java110/utils/util/PersonDto.java

@@ -0,0 +1,54 @@
+package com.java110.utils.util;
+
+import java.util.Date;
+
+/**
+ * @ClassName PersonDto
+ * @Description TODO
+ * @Author wuxw
+ * @Date 2020/1/28 17:39
+ * @Version 1.0
+ * add by wuxw 2020/1/28
+ **/
+public class PersonDto {
+
+    private int id;
+
+    private String name;
+
+    private Date createTime;
+
+    private int age;
+
+    public String getName() {
+        return name;
+    }
+
+    public void setName(String name) {
+        this.name = name;
+    }
+
+    public Date getCreateTime() {
+        return createTime;
+    }
+
+    public void setCreateTime(Date createTime) {
+        this.createTime = createTime;
+    }
+
+    public int getId() {
+        return id;
+    }
+
+    public void setId(int id) {
+        this.id = id;
+    }
+
+    public int getAge() {
+        return age;
+    }
+
+    public void setAge(int age) {
+        this.age = age;
+    }
+}

+ 43 - 0
java110-utils/src/test/java/com/java110/utils/util/PersonVo.java

@@ -0,0 +1,43 @@
+package com.java110.utils.util;
+
+import java.util.Date;
+
+/**
+ * @ClassName PersonVo
+ * @Description TODO
+ * @Author wuxw
+ * @Date 2020/1/28 17:39
+ * @Version 1.0
+ * add by wuxw 2020/1/28
+ **/
+public class PersonVo {
+
+    private String id;
+    private String name;
+
+    private String createTime;
+
+    public String getName() {
+        return name;
+    }
+
+    public void setName(String name) {
+        this.name = name;
+    }
+
+    public String getCreateTime() {
+        return createTime;
+    }
+
+    public void setCreateTime(String createTime) {
+        this.createTime = createTime;
+    }
+
+    public String getId() {
+        return id;
+    }
+
+    public void setId(String id) {
+        this.id = id;
+    }
+}