|
@@ -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"));
|
|
|
+
|
|
|
+ }
|
|
|
+
|
|
|
+}
|
|
|
+
|