Jelajahi Sumber

提交文件

songxy 1 tahun lalu
induk
melakukan
0a4ed46043

+ 227 - 0
BJYSYBZ_S/src/main/java/com/zjugis/ysgzybz/utils/BeanUtil.java

@@ -0,0 +1,227 @@
+package com.zjugis.ysgzybz.utils;
+
+
+import org.springframework.beans.BeanUtils;
+import org.springframework.beans.BeanWrapper;
+import org.springframework.beans.BeanWrapperImpl;
+
+import java.beans.BeanInfo;
+import java.beans.Introspector;
+import java.beans.PropertyDescriptor;
+import java.io.ByteArrayInputStream;
+import java.lang.reflect.Field;
+import java.math.BigDecimal;
+import java.time.LocalDate;
+import java.time.LocalDateTime;
+import java.util.Arrays;
+import java.util.HashMap;
+import java.util.List;
+import java.util.Map;
+import java.util.stream.Collectors;
+
+public class BeanUtil {
+
+
+    public static <T> T toBean(Map map, Class<T> clazz){
+
+        T obj = BeanUtils.instantiateClass(clazz);
+        BeanWrapper beanWrapper = new BeanWrapperImpl(obj);
+        beanWrapper.setPropertyValues(map);
+
+        return obj;
+    }
+
+
+    /**
+     * 将JavaBean对象封装到Map集合当中
+     * @param bean
+     * @return
+     * @throws Exception
+     */
+    public static Map<String, Object> bean2map(Object bean) throws Exception
+    {
+        //创建Map集合对象
+        Map<String,Object> map=new HashMap<String, Object>();
+        //获取对象字节码信息,不要Object的属性
+        BeanInfo beanInfo = Introspector.getBeanInfo(bean.getClass(),Object.class);
+        //获取bean对象中的所有属性
+        PropertyDescriptor[] list = beanInfo.getPropertyDescriptors();
+        for (PropertyDescriptor pd : list) {
+            String key = pd.getName();//获取属性名
+            Object value = pd.getReadMethod().invoke(bean);//调用getter()方法,获取内容
+            map.put(key.toUpperCase(), value);//增加到map集合当中
+        }
+        return map;
+    }
+
+
+    /**
+     * 将Map集合中的数据封装到JavaBean对象中
+     * @param map        集合
+     * @param classType 封装javabean对象
+     * @throws Exception
+     */
+    public static <T> T map2bean(Map<String, Object> map,Class<T> classType) throws Exception
+    {
+        //获取对象字节码信息,不要Object的属性
+        return map2bean(map, classType.newInstance(), classType);
+    }
+
+    /**
+     * 将Map集合中的数据封装到JavaBean对象中
+     * @param map        集合
+     * @param classType 封装javabean对象
+     * @throws Exception
+     */
+    public static <T> T map2bean(Map<String, Object> map,Class<T> classType, List<String> filters) throws Exception
+    {
+        //获取对象字节码信息,不要Object的属性
+        return map2bean(map, classType.newInstance(), classType, filters);
+    }
+
+    public static <T> T map2bean(Map<String, Object> map, T t) throws Exception
+    {
+        return  map2bean(map, t, t.getClass());
+    }
+
+    public static <T> T map2bean(Map<String, Object> map, T t, List<String> filters) throws Exception
+    {
+        return  map2bean(map, t, t.getClass(), filters);
+    }
+
+    public static <T> T map2bean(Map<String, Object> map, T t, Class clazz) throws Exception
+    {
+        //获取对象字节码信息,不要Object的属性
+        BeanInfo beanInfo = Introspector.getBeanInfo(clazz,Object.class);
+        //获取bean对象中的所有属性
+        PropertyDescriptor[] list = beanInfo.getPropertyDescriptors();
+        for (PropertyDescriptor pd : list) {
+            String key = pd.getName();    //获取属性名
+            Object value=map.get(key);  //获取属性值
+            String typeName = pd.getPropertyType().getName();
+            if(value != null && ClazzUtil.isJavaClass(pd.getPropertyType())) {
+                if(typeName.indexOf("Double") != -1) {
+                    pd.getWriteMethod().invoke(t, Double.valueOf(value.toString()));//调用属性setter()方法,设置到javabean对象当中
+                } else if(typeName.indexOf("int") != -1 || typeName.indexOf("Integer") != -1) {
+                    pd.getWriteMethod().invoke(t, Integer.valueOf(value.toString()).intValue());
+                }else {
+                    pd.getWriteMethod().invoke(t, value);//调用属性setter()方法,设置到javabean对象当中
+                }
+
+            }
+        }
+        return t;
+    }
+
+    public static <T> T map2bean(Map<String, Object> map, T t, Class clazz, List<String> filters) throws Exception
+    {
+        //获取对象字节码信息,不要Object的属性
+        BeanInfo beanInfo = Introspector.getBeanInfo(clazz,Object.class);
+        //获取bean对象中的所有属性
+        PropertyDescriptor[] list = beanInfo.getPropertyDescriptors();
+        for (PropertyDescriptor pd : list) {
+
+            String key = pd.getName();    //获取属性名
+            if(filters.contains(key)) continue;
+            Object value=map.get(key);  //获取属性值
+            String typeName = pd.getPropertyType().getName();
+            if(value != null && ClazzUtil.isJavaClass(pd.getPropertyType())) {
+                if(typeName.indexOf("Double") != -1) {
+                    pd.getWriteMethod().invoke(t, Double.valueOf(value.toString()));//调用属性setter()方法,设置到javabean对象当中
+                } else if(typeName.indexOf("int") != -1) {
+                    pd.getWriteMethod().invoke(t, Integer.valueOf(value.toString()).intValue());
+                }else {
+                    pd.getWriteMethod().invoke(t, value);//调用属性setter()方法,设置到javabean对象当中
+                }
+
+            }
+        }
+        return t;
+    }
+
+    public static <T> T map2beanIgnore(Map<String, Object> map, T t, Class clazz) throws Exception{
+        return map2beanIgnore(map, t, clazz, true);
+    }
+
+    public static <T> T map2beanIgnore(Map<String, Object> map, T t, Class clazz, boolean upperCase) throws Exception
+    {
+        //获取对象字节码信息,不要Object的属性
+        BeanInfo beanInfo = Introspector.getBeanInfo(clazz,Object.class);
+        //获取bean对象中的所有属性
+        PropertyDescriptor[] list = beanInfo.getPropertyDescriptors();
+        for (PropertyDescriptor pd : list) {
+            String key = pd.getName();    //获取属性名
+            Object value=map.get(upperCase ? key.toUpperCase() : key.toLowerCase());  //获取属性值、
+            String typeName = pd.getPropertyType().getName();
+            if(value != null && ClazzUtil.isJavaClass(pd.getPropertyType())) {
+
+                try {
+                    if(typeName.contains("Double")) {
+                        pd.getWriteMethod().invoke(t, Double.valueOf(value.toString()));//调用属性setter()方法,设置到javabean对象当中
+                    } else if(typeName.contains("String")) {
+                        pd.getWriteMethod().invoke(t, String.valueOf(value));//调用属性setter()方法,设置到javabean对象当中
+                    } else if(typeName.contains("Long")) {
+                        pd.getWriteMethod().invoke(t, Long.valueOf(value.toString()));//调用属性setter()方法,设置到javabean对象当中
+                    } else if(typeName.contains("Integer")) {
+                        pd.getWriteMethod().invoke(t, Integer.valueOf(value.toString()));//调用属性setter()方法,设置到javabean对象当中
+                    } else if(typeName.contains("BigDecimal")) {
+                        pd.getWriteMethod().invoke(t, new BigDecimal(value.toString()));
+                    } else if(typeName.contains("ByteArrayInputStream")) {
+                        pd.getWriteMethod().invoke(t,new ByteArrayInputStream((byte [])value));
+                    } else if(typeName.contains("sql.Date")) {
+                        if(value.getClass().toString().contains("String")) {
+                            pd.getWriteMethod().invoke(t,java.sql.Date.valueOf(LocalDate.parse(value.toString())));
+                        } else {
+                            pd.getWriteMethod().invoke(t,java.sql.Date.valueOf(((LocalDateTime) value).toLocalDate()));
+
+                        }
+                    } else if(typeName.contains("int")) {
+                        pd.getWriteMethod().invoke(t, Integer.valueOf(value.toString()));
+                    } else {
+                        pd.getWriteMethod().invoke(t, value);//调用属性setter()方法,设置到javabean对象当中
+                    }
+                } catch (Exception e) {
+                    pd.getWriteMethod().invoke(t, (Object) null);
+                }
+            }
+        }
+        return t;
+    }
+
+
+
+
+    public Object getValue(String expectType, String valueType, Object value) {
+        Object expectValue = null;
+
+
+
+        return null;
+
+    }
+
+
+    public static String[] getFields(Object object) {
+
+        return getFields(object.getClass(), new String[]{});
+    }
+
+    public static String[] getFields(Object object, String[] filter) {
+
+        return getFields(object.getClass(), filter);
+    }
+
+    public static String[] getFields(Class clazz, String[] filter) {
+        Field[] fields = clazz.getFields();
+        List<String> fieldStr = Arrays.asList(fields).stream().filter(v -> !Arrays.asList(filter).contains(v.getName())).map(Field::getName).collect(Collectors.toList());
+
+        return fieldStr.toArray(new String[fieldStr.size()]);
+    }
+    public static void main(String[] args) {
+
+        System.out.println(LocalDate.parse("2022-07-08"));
+
+    }
+
+}
+

+ 43 - 0
BJYSYBZ_S/src/main/java/com/zjugis/ysgzybz/utils/ClazzUtil.java

@@ -0,0 +1,43 @@
+package com.zjugis.ysgzybz.utils;
+
+import java.lang.reflect.Field;
+import java.util.Arrays;
+import java.util.List;
+import java.util.stream.Collectors;
+
+public class ClazzUtil {
+
+    /**
+     * 判断一个类是JAVA类型还是用户定义类型
+     * @param clz
+     * @return
+     */
+    public static boolean isJavaClass(Class<?> clz) {
+        return clz != null && clz.getClassLoader() == null;
+    }
+
+    public static boolean isDoubleClazz(Object o) {
+
+        return o != null && o.getClass().getSimpleName().equals("Double");
+    }
+    /**
+     *
+     * @param clazz
+     * @return
+     */
+    public static Field[] getFields(Class clazz) {
+        return clazz.getDeclaredFields();
+    }
+
+    /**
+     * 获取所有数组
+     * @param clazz
+     * @return
+     */
+    public static List<String> getFieldNames(Class clazz) {
+        Field[] fields = getFields(clazz);
+        return Arrays.stream(fields).map(field -> field.getName()).collect(Collectors.toList());
+    }
+
+
+}

+ 86 - 0
BJYSYBZ_S/src/main/resources/mapper/BjkgMapper.xml

@@ -0,0 +1,86 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
+<mapper namespace="com.zjugis.ysgzybz.dao.BjkgMapper">
+    <insert id="insertShape">
+      INSERT INTO sde.ybz_bj_kg(
+        objectid,
+        dymc,
+        pfsj,
+        pfwh,
+        dkbh,
+        dldm,
+        dlmc,
+        dlbm,
+        zdldm,
+        jrbl,
+        mj,
+        rjl,
+        ldl,
+        jzmd,
+        jzgd,
+        xglx,
+        fjss1,
+        fjss2,
+        gxyq,
+        txyq,
+        ghdt,
+        xzyd,
+        gdmj,
+        fl,
+        tdm,
+        bz,
+        ssqx,
+        sffd,
+        dabh,
+        jcmj,
+        gkyq,
+        lstz,
+        featureguid,
+        updatetime,
+        taskinid,
+        shape,
+        shape_length,
+        shape_area
+      )
+      values (
+        #{entity.objectid},
+        #{entity.dymc},
+        #{entity.pfsj},
+        #{entity.pfwh},
+        #{entity.dkbh},
+        #{entity.dldm},
+        #{entity.dlmc},
+        #{entity.dlbm},
+        #{entity.zdldm},
+        #{entity.jrbl},
+        #{entity.mj},
+        #{entity.rjl},
+        #{entity.ldl},
+        #{entity.jzmd},
+        #{entity.jzgd},
+        #{entity.xglx},
+        #{entity.fjss1},
+        #{entity.fjss2},
+        #{entity.gxyq},
+        #{entity.txyq},
+        #{entity.ghdt},
+        #{entity.xzyd},
+        #{entity.gdmj},
+        #{entity.fl},
+        #{entity.tdm},
+        #{entity.bz},
+        #{entity.ssqx},
+        #{entity.sffd},
+        #{entity.dabh},
+        #{entity.jcmj},
+        #{entity.gkyq},
+        #{entity.lstz},
+        #{entity.featureguid},
+        #{entity.updatetime},
+        #{entity.taskinid},
+        public.st_geomfromwkb(#{entity.shape}, 4490),
+        #{entity.shapeLength},
+        #{entity.shapeArea}
+      )
+    </insert>
+</mapper>