Search in sources :

Example 11 with Property

use of org.eweb4j.orm.config.bean.Property in project eweb4j-framework by laiweiwei.

the class CommonUtil method mappingPojo.

public static <T> List<T> mappingPojo(List<Map<String, Object>> datas, Class<T> cls) throws Exception {
    if (datas == null || datas.isEmpty())
        return null;
    List<String> columns = new ArrayList<String>();
    for (String col : datas.get(0).keySet()) columns.add(col);
    List<T> list = new ArrayList<T>();
    T t = null;
    for (Map<String, Object> data : datas) {
        t = cls.newInstance();
        ReflectUtil ru = new ReflectUtil(t);
        ORMConfigBean ormBean = ORMConfigBeanCache.get(cls.getName());
        for (Iterator<Property> it = ormBean.getProperty().iterator(); it.hasNext(); ) {
            Property p = it.next();
            String type = p.getType();
            if (type == null)
                continue;
            // 如果查询出来的字段名字没有,则不进行值注入
            boolean flag = false;
            for (String col : columns) {
                if (col.equalsIgnoreCase(p.getColumn())) {
                    flag = true;
                    continue;
                }
            }
            if (!flag)
                continue;
            Method m = ru.getSetter(p.getName());
            if (m == null)
                continue;
            Object value = data.get(p.getColumn());
            if (value == null)
                continue;
            String v = String.valueOf(value);
            if (v == null) {
                v = "";
            }
            if ("int".equalsIgnoreCase(type) || "java.lang.Integer".equalsIgnoreCase(type)) {
                if ("".equals(v.trim())) {
                    v = "0";
                }
                if (value instanceof Boolean)
                    v = ((Boolean) value ? "1" : "0");
                m.invoke(t, Integer.parseInt(v));
            } else if ("long".equalsIgnoreCase(type) || "java.lang.Long".equalsIgnoreCase(type)) {
                if ("".equals(v.trim())) {
                    v = "0";
                }
                if (value instanceof Boolean)
                    v = ((Boolean) value ? "1" : "0");
                m.invoke(t, Long.parseLong(v));
            } else if ("float".equalsIgnoreCase(type) || "java.lang.Float".equalsIgnoreCase(type)) {
                if ("".equals(v.trim())) {
                    v = "0.0";
                }
                if (value instanceof Boolean)
                    v = ((Boolean) value ? "1.0" : "0.0");
                m.invoke(t, Float.parseFloat(v));
            } else if ("double".equalsIgnoreCase(type) || "java.lang.Double".equalsIgnoreCase(type)) {
                if ("".equals(v.trim())) {
                    v = "0.0";
                }
                if (value instanceof Boolean)
                    v = ((Boolean) value ? "1.0" : "0.0");
                m.invoke(t, Double.parseDouble(v));
            } else if ("string".equalsIgnoreCase(type) || "java.lang.String".equalsIgnoreCase(type)) {
                m.invoke(t, v);
            } else if ("boolean".equalsIgnoreCase(type) || "java.lang.Boolean".equalsIgnoreCase(type)) {
                if ("1".equals(v.trim()) || "true".equals(v.trim())) {
                    m.invoke(t, true);
                } else if ("0".equals(v.trim()) || "false".equals(v.trim())) {
                    m.invoke(t, false);
                }
            } else if ("date".equalsIgnoreCase(type) || "java.sql.Date".equalsIgnoreCase(type) || "java.util.Date".equalsIgnoreCase(type)) {
                m.invoke(t, value);
            } else if ("timestamp".equalsIgnoreCase(type) || "java.sql.Timestamp".equalsIgnoreCase(type)) {
                m.invoke(t, value);
            } else if ("time".equalsIgnoreCase(type) || "java.sql.Time".equalsIgnoreCase(type)) {
                m.invoke(t, value);
            } else if ("[B".equalsIgnoreCase(type) || "byte[]".equalsIgnoreCase(type) || "[Ljava.lang.Byte;".equalsIgnoreCase(type)) {
                m.invoke(t, value);
            } else if (PropType.ONE_ONE.equalsIgnoreCase(type) || PropType.MANY_ONE.equalsIgnoreCase(type)) {
                if ("".equals(v))
                    continue;
                Field field = ru.getField(p.getName());
                Class<?> tarClass = field.getType();
                String refField = p.getRelProperty();
                Object tarObj = tarClass.newInstance();
                tarObj = ClassUtil.injectFieldValue(tarObj, refField, new String[] { v });
                m.invoke(t, tarObj);
            } else if (PropType.ONE_MANY.equalsIgnoreCase(type)) {
            } else if (PropType.MANY_MANY.equalsIgnoreCase(type)) {
            } else if (!"".equals(type)) {
                m.invoke(t, String.valueOf(value));
            }
        }
        list.add(t);
    }
    return list.isEmpty() ? null : list;
}
Also used : ORMConfigBean(org.eweb4j.orm.config.bean.ORMConfigBean) ArrayList(java.util.ArrayList) Method(java.lang.reflect.Method) Field(java.lang.reflect.Field) Property(org.eweb4j.orm.config.bean.Property)

Example 12 with Property

use of org.eweb4j.orm.config.bean.Property in project eweb4j-framework by laiweiwei.

the class ORMConfigBeanUtil method getId.

/**
	 * 获取自增长的主键名字
	 * 
	 * @param clazz
	 * @param type
	 *            1的时候获取的是数据库字段名,2的时候获取的是java类的属性名
	 * @return
	 */
public static <T> String getId(T t, int type) {
    Class<?> clazz;
    if (t instanceof Class) {
        clazz = (Class<?>) t;
    } else {
        clazz = t.getClass();
    }
    if (!(t instanceof Class) && Map.class.isAssignableFrom(clazz)) {
        HashMap<String, Object> map = (HashMap<String, Object>) t;
        return (String) map.get("idColumn");
    }
    ORMConfigBean ormBean = ORMConfigBeanCache.get(clazz.getName());
    if (ormBean == null)
        return null;
    for (Property property : ormBean.getProperty()) {
        if (("true".equals(property.getPk()) || "1".equals(property.getPk())) && ("true".equals(property.getAutoIncrement()) || "1".equals(property.getAutoIncrement()))) {
            if (1 == type)
                return property.getColumn();
            else if (2 == type)
                return property.getName();
            break;
        }
    }
    return null;
}
Also used : HashMap(java.util.HashMap) ORMConfigBean(org.eweb4j.orm.config.bean.ORMConfigBean) HashMap(java.util.HashMap) Map(java.util.Map) Property(org.eweb4j.orm.config.bean.Property)

Aggregations

Property (org.eweb4j.orm.config.bean.Property)12 ORMConfigBean (org.eweb4j.orm.config.bean.ORMConfigBean)11 Field (java.lang.reflect.Field)7 ArrayList (java.util.ArrayList)7 ReflectUtil (org.eweb4j.util.ReflectUtil)6 Method (java.lang.reflect.Method)5 JoinColumn (javax.persistence.JoinColumn)4 File (java.io.File)3 Entry (java.util.Map.Entry)3 Column (javax.persistence.Column)3 ManyToMany (javax.persistence.ManyToMany)3 FileWriter (java.io.FileWriter)2 IOException (java.io.IOException)2 HashMap (java.util.HashMap)2 Map (java.util.Map)2 Entity (javax.persistence.Entity)2 Id (javax.persistence.Id)2 JoinTable (javax.persistence.JoinTable)2 Table (javax.persistence.Table)2 ConfigBean (org.eweb4j.config.bean.ConfigBean)2