日期:2014-05-16  浏览次数:20394 次

Spring JdbcTemplate 查询结果 实体生成工具
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import java.lang.reflect.Field;
import java.util.*;

/**
 * Created by  黄永光
 * User: Administrator
 * Date: 2011-8-11
 * Time: 10:30:15
 * <p/>
 * 将实体工具类,Spring查询对象封装实体工具.
 */

public class EntityTool {

    static Log LOGGER = LogFactory.getLog(EntityTool.class);

    /**
     * 将map转成实体,map中的key必须与实体中的属性名相同才可以转换成功
     *
     * @param entityMap  实体map,属性键值对,其中key为实体的属性名,value为属性的值
     * @param entityType 实体类
     * @param <T>
     * @return
     */
    public static <T> T converToEntity(Map<String, Object> entityMap, Class<T> entityType) {
        T result = null;
        try {
            result = entityType.newInstance();
            Map<String, Field> fieldsMap = getFieldMap(entityType);
            Field field = null;
            for (Map.Entry<String, Object> entry : entityMap.entrySet()) {
                field = fieldsMap.get(entry.getKey().toLowerCase());
                if (field == null) {
                    LOGGER.error("键值" + entry.getKey().toLowerCase() + "在" + entityType.getName() + "中找不到对应的属性!");
                    throw new Exception();

                }
                field.setAccessible(true);
                Object value = entry.getValue();

                if (value == null) {
                    continue;
                    
                }

                if (isBasicType(field.getType())) {
                    //  System.out.println(field.getName()+"="+value);
                    System.out.println(field.getType());

                    if (field.getType().equals(boolean.class) || field.getType().equals(Boolean.class)) {
                        if (value.equals(new Boolean(true) || value.equals(true)) || value.equals(new Boolean(true) || value.equals(true))) {
                            field.set(result, value);
                        } else {
                            if (value.toString().equals("1")) {
                                field.set(result, true);
                            } else {
                                field.set(result, false);
                            }
                        }
                    } else {
                        field.set(result, value);
                    }


                } else {
                    /*
                    设置复合类型属性,只设id如果存在
                     */
                    Class fildType = field.getType();
                    Field idField = null;
                    try {
                        idField = getDeclaredField("id", fildType, true);


                    } catch (NoSuchFieldException e) {
                        continue;
                    }
                    if (idField != null) {
                        idField.setAccessible(true);
                        Object compositeFieldValue = fildType.newInstance();
                        idField.set(compositeFieldValue, value);
                        field.set(result, compositeFieldValue);
                    } else {
                        LOGGER.warn("复合属性" + entry.getKey().toLowerCase() + "没有id属性,没有赋值!");
                    }
                }

            }
        } catch (InstantiationException e) {
            e.printStackTrace();
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        }

        return result;
    }

    /**
     * 得到某类的属性
     * @param fieldName      属性名
     * @param clazz          类
     * @param withSuperClass 是否从父类中取属性
     * @return
     */
    private static Field getDeclaredField(String fieldName, Class clazz, boolean withSuperClass) throws NoSuchFieldException {
        Field field = null;
        try {
            field = clazz.getDeclaredField(fieldName);
        } catch (NoSuchFieldException e) {
            if (field == null && withSuperClass) {
                Class superClazz = clazz.getSup