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;
}
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;
}
Aggregations