对象转换工具类

自己写的对象转换工具类

import java.lang.reflect.Field;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;

public class ObjectConverter<T> {

    private Class<T> clazz;

    private Field[] fields;

    public ObjectConverter(Class<T> clazz) {
        this.clazz = clazz;
        this.fields = ObjectUtil.getObjectFields(clazz);
    }

    /**
     * 将Map转成java对象
     * 
     * @param map
     * @return
     * @throws InstantiationException
     * @throws IllegalAccessException
     */
    public List<T> toObjectList(List<Map<String, Object>> mapList, String entityKeyPrefix) throws InstantiationException, IllegalAccessException {
        List<T> objectList = new ArrayList<T>();
        for (Map<String, Object> map : mapList) {
            T instance = clazz.newInstance();
            instance = merge(instance, map, entityKeyPrefix);
            objectList.add(instance);
        }
        return objectList;
    }

    /**
     * 将Map转成java对象
     * 
     * @param map
     * @return
     * @throws InstantiationException
     * @throws IllegalAccessException
     */
    public T toObject(Map<String, Object> map, String entityKeyPrefix) throws InstantiationException, IllegalAccessException {

        T instance = clazz.newInstance();

        return merge(instance, map, entityKeyPrefix);
    }

    /**
     * 将Map转成java对象
     * 
     * @param instance
     * @param map
     * @return
     * @throws InstantiationException
     * @throws IllegalAccessException
     */
    public T merge(T instance, Map<String, Object> map, String entityKeyPrefix) throws InstantiationException, IllegalAccessException {

        if (entityKeyPrefix == null) {
            entityKeyPrefix = "";
        }

        for (Field f : fields) {

            f.setAccessible(true);

            String fieldName = f.getName();

            Object value = map.get(entityKeyPrefix + fieldName);
            if (value != null) {
                f.set(instance, value);
            }
        }

        return instance;
    }

    /**
     * 将ResultSet数据集转换成Java对象
     * 
     * @param rs
     * @return
     * @throws SQLException
     * @throws InstantiationException
     * @throws IllegalAccessException
     */
    public T toObject(ResultSet rs, String entityKeyPrefix) throws SQLException, InstantiationException, IllegalAccessException {

        T instance = clazz.newInstance();

        return merge(instance, rs, entityKeyPrefix);
    }

    /**
     * 将ResultSet数据集转换成Java对象
     * 
     * @param rs
     * @return
     * @throws SQLException
     * @throws InstantiationException
     * @throws IllegalAccessException
     */
    public T merge(T instance, ResultSet rs, String entityKeyPrefix) throws SQLException, InstantiationException, IllegalAccessException {

        if (entityKeyPrefix == null) {
            entityKeyPrefix = "";
        } else {
            entityKeyPrefix = entityKeyPrefix + "_";
        }

        for (Field f : fields) {

            f.setAccessible(true);

            String fieldName = f.getName();
            String dbFieldName = StringUtil.camelToUnderline(fieldName);

            Object value = rs.getObject(entityKeyPrefix + dbFieldName);

            if (value != null) {
                f.set(instance, value);
            }
        }

        return instance;
    }

    /**
     * 将JSONArray转成java对象
     * 
     * @param map
     * @return
     * @throws SQLException
     * @throws IllegalAccessException
     * @throws InstantiationException
     */
    public List<T> toObjectList(JSONArray jsonArray) throws InstantiationException, IllegalAccessException {
        List<T> objectList = new ArrayList<T>();

        Iterator<Object> it = jsonArray.iterator();
        while (it.hasNext()) {
            JSONObject jsonObject = (JSONObject) it.next();
            T instance = toObject(jsonObject);
            objectList.add(instance);
        }

        return objectList;
    }

    /**
     * 将JSON对象转换成对象实例
     * 
     * @param rs
     * @return
     * @throws SQLException
     * @throws InstantiationException
     * @throws IllegalAccessException
     */
    public T toObject(JSONObject jsonObject) throws InstantiationException, IllegalAccessException {

        T instance = clazz.newInstance();

        return merge(instance, jsonObject);
    }

    /**
     * 将JSON对象转换成对象实例
     * 
     * @param instance
     * @param jsonObject
     * @return
     * @throws IllegalArgumentException
     * @throws IllegalAccessException
     */
    public T merge(T instance, JSONObject jsonObject) throws IllegalArgumentException, IllegalAccessException {

        for (Field f : fields) {

            f.setAccessible(true);

            String fieldName = f.getName();

            Object value = jsonObject.get(fieldName);

            if (value != null) {
                f.set(instance, value);
            }
        }

        return instance;
    }

    /**
     * 合并两个对象,将对象2的不为空的属性复制到对象1中,并返回对象1
     * 
     * @param object1
     * @param object2
     * @return
     * @throws IllegalArgumentException
     * @throws IllegalAccessException
     */
    public T merge(T object1, T object2) {
        for (Field f : fields) {
            f.setAccessible(true);
            try {
                Object value = f.get(object2);
                if (value != null) {
                    f.set(object1, value);
                }
            } catch (IllegalArgumentException e) {
            } catch (IllegalAccessException e) {
            }
        }
        return object1;
    }

}
import java.lang.reflect.Field;
import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.List;

public class ObjectUtil {

    
    /**
     * 获取一个类中具有get方法的字段
     * @param clazz
     * @return
     */
    public static Field[] getObjectFields(Class<?> clazz) {

        Method[] method = clazz.getMethods();
        List<Field> list = new ArrayList<Field>();

        // 获取有get方法的字段
        Field[] fields = clazz.getDeclaredFields();
        for (int j = 0; j < fields.length; j++) {
            Field f = fields[j];
            String fieldName = f.getName();
            String m = "get" + fieldName.toUpperCase().charAt(0) + fieldName.substring(1);
            for (int i = 0; i < method.length; i++) {
                if (method[i].getName().endsWith(m)) {
                    list.add(f);
                }
            }
        }

        // List转成数组
        int size = list.size();
        Field[] fieldArray = new Field[size];
        for (int i = 0; i < size; i++) {
            Field f = list.get(i);
            fieldArray[i] = f;
        }
        return fieldArray;
    }
}
原文地址:https://www.cnblogs.com/lhp2012/p/4607218.html