| | |
| | | package com.zy.common.service.erp; |
| | | |
| | | import com.core.common.Cools; |
| | | import com.zy.common.properties.ErpDbProperties; |
| | | import lombok.extern.slf4j.Slf4j; |
| | | import org.springframework.beans.factory.annotation.Autowired; |
| | |
| | | import java.io.ByteArrayOutputStream; |
| | | import java.io.ObjectInputStream; |
| | | import java.io.ObjectOutputStream; |
| | | import java.lang.reflect.Constructor; |
| | | import java.lang.reflect.Field; |
| | | import java.lang.reflect.InvocationTargetException; |
| | | import java.lang.reflect.Modifier; |
| | | import java.sql.*; |
| | | import java.util.ArrayList; |
| | | import java.util.Date; |
| | | import java.util.HashMap; |
| | | import java.util.List; |
| | | import java.util.Map; |
| | |
| | | List<T> list = new ArrayList<>(); |
| | | if (null != result) { |
| | | for (Map<String, Object> entity : result) { |
| | | list.add(Cools.conver(entity, cls)); |
| | | list.add(conver(entity, cls)); |
| | | } |
| | | } |
| | | return list; |
| | |
| | | /********************************* 核心层 *********************************/ |
| | | /*****************************************************************************/ |
| | | |
| | | /** |
| | | * map 转 对象 |
| | | */ |
| | | public static <T> T conver(Map<? extends String, ?> map, Class<T> cls){ |
| | | T instance = null; |
| | | try { |
| | | Constructor<T> constructor = cls.getDeclaredConstructor(); |
| | | boolean constructorAccessible = constructor.isAccessible(); |
| | | constructor.setAccessible(true); |
| | | instance = constructor.newInstance(); |
| | | constructor.setAccessible(constructorAccessible); |
| | | } catch (IllegalAccessException | InstantiationException | NoSuchMethodException | InvocationTargetException e) { |
| | | e.printStackTrace(); |
| | | } |
| | | Class<?> prototype = cls; |
| | | do { |
| | | for (Field field : prototype.getDeclaredFields()){ |
| | | if (Modifier.isFinal(field.getModifiers()) |
| | | || Modifier.isStatic(field.getModifiers()) |
| | | || Modifier.isTransient(field.getModifiers())){ |
| | | continue; |
| | | } |
| | | String fieldName = field.getName(); |
| | | Object val = null; |
| | | if (map.containsKey(fieldName)){ |
| | | val = map.get(fieldName); |
| | | } |
| | | if (val != null){ |
| | | boolean fieldAccessible = field.isAccessible(); |
| | | field.setAccessible(true); |
| | | Class<?> type = field.getType(); |
| | | try { |
| | | if (type.equals(Date.class)) { |
| | | Constructor<?> constructor = type.getDeclaredConstructor(Date.class); |
| | | boolean constructorAccessible = constructor.isAccessible(); |
| | | constructor.setAccessible(true); |
| | | field.set(instance, constructor.newInstance(val)); |
| | | constructor.setAccessible(constructorAccessible); |
| | | } else { |
| | | Constructor<?> constructor = type.getDeclaredConstructor(String.class); |
| | | boolean constructorAccessible = constructor.isAccessible(); |
| | | constructor.setAccessible(true); |
| | | field.set(instance, constructor.newInstance(String.valueOf(val))); |
| | | constructor.setAccessible(constructorAccessible); |
| | | } |
| | | } catch (IllegalAccessException |
| | | | InstantiationException |
| | | | InvocationTargetException |
| | | | NoSuchMethodException e) { |
| | | System.err.println("convert error ===> Class["+prototype+"],Field:["+fieldName+"],Type:["+type+"],Value:["+val+"]"); |
| | | } |
| | | field.setAccessible(fieldAccessible); |
| | | } |
| | | } |
| | | prototype = prototype.getSuperclass(); |
| | | } while (!Object.class.equals(prototype)); |
| | | return instance; |
| | | } |
| | | |
| | | private List<Map<String, Object>> executeQuery(String sql) { |
| | | Connection conn = null; |