use of org.eweb4j.util.ReflectUtil in project eweb4j-framework by laiweiwei.
the class SelectSqlCreator method selectOne.
public String selectOne() throws SqlCreateException {
// 反射工具
ReflectUtil ru = new ReflectUtil(this.t);
StringBuilder condition = new StringBuilder();
Method m = ru.getGetter(idField);
if (m == null) {
throw new SqlCreateException("can not find id getter");
}
try {
condition.append(idColumn).append(" = '").append(m.invoke(this.t)).append("'");
} catch (Exception e) {
throw new SqlCreateException(m + " invoke exception " + e.toString());
}
return String.format("SELECT * FROM %s WHERE %s ;", selectAllColumn, table, condition.toString());
}
use of org.eweb4j.util.ReflectUtil in project eweb4j-framework by laiweiwei.
the class SelectSqlCreator method selectWhere.
/**
* 各种条件查询
*
* @param fields
* 构成条件的属性名数组(填写的应该是对象属性名,不是数据库字段名)
* @param values
* 构成条件的字段值
* @param likeType
* 匹配类型 -1左匹配 0全匹配 1右匹配
* @param isLike
* 是否模糊查询
* @param orderField
* 对哪个字段进行排序
* @param oType
* 升序还是降序
* @return 字符串
* @param currentPage
* 第几页
* @param numPerPage
* 每页显示多少条
* @return
* @throws SqlCreateException
*/
public String selectWhere(String[] fields, String[] values, int likeType, boolean isLike, boolean isNot, boolean isOR, String orderField, int oType, int currentPage, int numPerPage) throws SqlCreateException {
String oper = " = ";
String connector = " AND ";
String left = " '";
String right = "' ";
if (isNot)
oper = "<>";
if (isLike) {
oper = " LIKE ";
left = " '%";
right = "%' ";
if (isNot)
oper = " NOT LIKE ";
switch(likeType) {
case LikeType.LEFT_LIKE:
left = " '";
break;
case LikeType.RIGHT_LIKE:
right = "' ";
break;
case LikeType.ALL_LIKE:
}
}
if (isOR)
connector = " OR ";
String[] type = { oper, connector, left, right };
if (orderField == null) {
orderField = idColumn;
orderField = OrderColumnUtil.getOrderColumn(orderField, dbType);
} else
orderField = ORMConfigBeanUtil.getColumn(clazz, orderField);
StringBuilder condition = new StringBuilder();
String[] columns = ORMConfigBeanUtil.getColumns(clazz, fields);
for (int i = 0; i < columns.length; ++i) {
if (values != null)
if (condition.length() > 0)
condition.append(type[1]).append(columns[i]).append(" ").append(type[0]).append(type[2]).append(values[i]).append(type[3]);
else
condition.append(columns[i]).append(" ").append(type[0]).append(type[2]).append(values[i]).append(type[3]);
else {
ReflectUtil ru = new ReflectUtil(t);
Method getter = ru.getGetter(fields[i]);
if (getter == null)
continue;
try {
Object _value = getter.invoke(t);
if (_value == null)
continue;
Object value = null;
if (ClassUtil.isPojo(_value.getClass())) {
Field f = ru.getField(fields[i]);
OneToOne oneAnn = getter.getAnnotation(OneToOne.class);
if (oneAnn == null)
oneAnn = f.getAnnotation(OneToOne.class);
ManyToOne manyToOneAnn = null;
if (oneAnn == null) {
manyToOneAnn = getter.getAnnotation(ManyToOne.class);
if (manyToOneAnn == null)
manyToOneAnn = f.getAnnotation(ManyToOne.class);
}
if (oneAnn != null || manyToOneAnn != null) {
JoinColumn joinColAnn = getter.getAnnotation(JoinColumn.class);
if (joinColAnn == null)
joinColAnn = f.getAnnotation(JoinColumn.class);
if (joinColAnn != null && joinColAnn.referencedColumnName().trim().length() > 0) {
String refCol = joinColAnn.referencedColumnName();
String refField = ORMConfigBeanUtil.getField(_value.getClass(), refCol);
ReflectUtil tarRu = new ReflectUtil(_value);
Method tarFKGetter = tarRu.getGetter(refField);
value = tarFKGetter.invoke(_value);
} else {
ReflectUtil tarRu = new ReflectUtil(_value);
String tarFKField = ORMConfigBeanUtil.getIdField(_value.getClass());
if (tarFKField != null) {
Method tarFKGetter = tarRu.getGetter(tarFKField);
value = tarFKGetter.invoke(_value);
}
}
}
if (value == null)
continue;
} else
value = _value;
if (condition.length() > 0)
condition.append(type[1]).append(columns[i]).append(" ").append(type[0]).append(type[2]).append(value).append(type[3]);
else
condition.append(columns[i]).append(" ").append(type[0]).append(type[2]).append(value).append(type[3]);
} catch (Exception e) {
e.printStackTrace();
throw new SqlCreateException(getter + " invoke exception " + e.toString());
}
}
}
String orderType = OrderType.ASC_ORDER == oType ? "ASC" : "DESC";
if (currentPage > 0 && numPerPage > 0)
return divPage(currentPage, numPerPage, orderField, oType, condition.toString());
else {
return String.format("SELECT %s FROM %s WHERE %s ORDER BY %s %s ;", selectAllColumn, table, condition.toString(), orderField, orderType);
}
}
use of org.eweb4j.util.ReflectUtil in project eweb4j-framework by laiweiwei.
the class UpdateSqlCreator method makeSQL.
private Sql makeSQL(T t) throws SqlCreateException {
Sql sql = new Sql();
Class<?> clazz = t.getClass();
String table;
String[] columns;
String[] fields;
Object[] values = null;
String idColumn;
String idField;
Object idValue = null;
HashMap<String, Object> map = null;
if (Map.class.isAssignableFrom(clazz)) {
map = (HashMap) t;
table = (String) map.get("table");
idColumn = (String) map.get("idColumn");
idField = idColumn;
idValue = map.get("idValue");
columns = (String[]) map.get("columns");
fields = columns;
values = (Object[]) map.get("values");
} else {
table = ORMConfigBeanUtil.getTable(clazz, false);
columns = ORMConfigBeanUtil.getColumns(clazz);
fields = ORMConfigBeanUtil.getFields(clazz);
idColumn = ORMConfigBeanUtil.getIdColumn(clazz);
idField = ORMConfigBeanUtil.getIdField(clazz);
}
StringBuilder valuesSb = new StringBuilder();
ReflectUtil ru = new ReflectUtil(t);
try {
if (map == null) {
Method idGetter = ru.getGetter(idField);
if (idGetter == null)
throw new SqlCreateException("can not find id getter");
idValue = idGetter.invoke(t);
}
for (int i = 0; i < columns.length; i++) {
String column = columns[i];
String field = fields[i];
Object value = null;
// id 字段不允许
if (idColumn != null && idColumn.equalsIgnoreCase(column))
continue;
if (map != null && values != null) {
value = values[i];
} else {
Method getter = ru.getGetter(field);
if (getter == null)
continue;
Object _value = getter.invoke(t);
if (_value == null)
continue;
if (ClassUtil.isPojo(_value.getClass())) {
Field f = ru.getField(field);
OneToOne oneAnn = getter.getAnnotation(OneToOne.class);
if (oneAnn == null)
oneAnn = f.getAnnotation(OneToOne.class);
ManyToOne manyToOneAnn = null;
if (oneAnn == null) {
manyToOneAnn = getter.getAnnotation(ManyToOne.class);
if (manyToOneAnn == null)
manyToOneAnn = f.getAnnotation(ManyToOne.class);
}
if (oneAnn != null || manyToOneAnn != null) {
JoinColumn joinColAnn = getter.getAnnotation(JoinColumn.class);
if (joinColAnn == null)
joinColAnn = f.getAnnotation(JoinColumn.class);
if (joinColAnn != null && joinColAnn.referencedColumnName().trim().length() > 0) {
String refCol = joinColAnn.referencedColumnName();
String refField = ORMConfigBeanUtil.getField(_value.getClass(), refCol);
ReflectUtil tarRu = new ReflectUtil(_value);
Method tarFKGetter = tarRu.getGetter(refField);
value = tarFKGetter.invoke(_value);
} else {
ReflectUtil tarRu = new ReflectUtil(_value);
String tarFKField = ORMConfigBeanUtil.getIdField(_value.getClass());
if (tarFKField != null) {
Method tarFKGetter = tarRu.getGetter(tarFKField);
value = tarFKGetter.invoke(_value);
}
}
}
if (value == null)
continue;
} else
value = _value;
}
if (valuesSb.length() > 0)
valuesSb.append(",");
// valuesSb.append(column).append(" = '").append(value).append("'");
valuesSb.append(column).append(" = ? ");
sql.args.add(value);
}
} catch (Exception e) {
throw new SqlCreateException("" + e.toString(), e);
}
// String condition = new StringBuilder().append(idColumn).append(" = ").append("'").append(idValue).append("'").toString();
String condition = new StringBuilder().append(idColumn).append(" = ? ").toString();
sql.args.add(idValue);
sql.sql = String.format("UPDATE %s SET %s WHERE %s ;", table, valuesSb, condition);
return sql;
}
use of org.eweb4j.util.ReflectUtil in project eweb4j-framework by laiweiwei.
the class DataSourceCreator method create.
public static DataSource create(final DBInfoConfigBean dbInfo) throws Exception {
ConfigBean cb = (ConfigBean) SingleBeanCache.get(ConfigBean.class.getName());
Class<?> cls = Thread.currentThread().getContextClassLoader().loadClass(cb.getOrm().getDataSource());
DataSource ds = (DataSource) cls.newInstance();
List<Property> properties = dbInfo.getProperty();
// 通过反射将配置信息注入到数据源对象中
for (Property property : properties) {
String name = property.getKey();
String value = property.getValue();
if (null == name || name.trim().length() == 0)
continue;
if (null == value || value.trim().length() == 0)
continue;
ReflectUtil ru2 = new ReflectUtil(ds);
Method m2 = ru2.getSetter(name);
if (m2 == null)
continue;
Class<?> clazz = m2.getParameterTypes()[0];
if (int.class.isAssignableFrom(clazz) || Integer.class.isAssignableFrom(clazz) || long.class.isAssignableFrom(clazz) || Long.class.isAssignableFrom(clazz))
m2.invoke(ds, Integer.parseInt(value));
else if (float.class.isAssignableFrom(clazz) || Float.class.isAssignableFrom(clazz) || double.class.isAssignableFrom(clazz) || Double.class.isAssignableFrom(clazz))
m2.invoke(ds, Float.parseFloat(value));
else if (boolean.class.isAssignableFrom(clazz) || Boolean.class.isAssignableFrom(clazz))
m2.invoke(ds, Boolean.parseBoolean(value));
else
m2.invoke(ds, value);
}
return ds;
}
use of org.eweb4j.util.ReflectUtil in project eweb4j-framework by laiweiwei.
the class BeanXMLWriter method writeRecursion.
private <T> void writeRecursion(final Element bean, T t) throws Exception {
ReflectUtil ru = new ReflectUtil(t);
Field[] fields = ru.getFields();
for (Field f : fields) {
String n = f.getName();
Method m = ru.getGetter(n);
if (m == null)
continue;
if ("clazz".equals(n))
n = "class";
Object obj = m.invoke(t);
Skip skip = f.getAnnotation(Skip.class);
if (skip != null)
continue;
AttrTag attrTag = f.getAnnotation(AttrTag.class);
Readonly readonly = f.getAnnotation(Readonly.class);
if (readonly != null)
continue;
if (ClassUtil.isPojo(f.getType())) {
// 属性为class,进入递归
Class<?> cls = this.classes.get(n);
if (cls == null)
continue;
if (obj == null)
obj = cls.newInstance();
writeRecursion(bean.addElement(n), cls.cast(obj));
} else if (attrTag != null) {
if (obj == null)
obj = "";
bean.addAttribute(n, String.valueOf(obj));
} else if (ClassUtil.isListClass(f)) {
Collection<?> list = (Collection<?>) obj;
Class<?> cls = this.classes.get(n);
if (list.size() == 0 && cls != null)
writeRecursion(bean.addElement(n), cls.newInstance());
else
for (Iterator<?> it = list.iterator(); it.hasNext(); ) writeRecursion(bean.addElement(n), it.next());
} else if (ClassUtil.isListString(f)) {
Collection<?> list = (Collection<?>) obj;
for (Iterator<?> it = list.iterator(); it.hasNext(); ) {
Object v2 = it.next();
if (v2 == null)
v2 = "";
bean.addElement(n).addText(String.valueOf(v2));
}
} else {
if (obj == null)
obj = "";
bean.addElement(n).addText(String.valueOf(obj));
}
}
}
Aggregations