请问Entity可以转换成Record吗?
主要的功能是:
我想在前台查询条件以对象的形式传入后台之后,后台将这个对象反射获取属性和值,根据类型自动生成Cnd对象,省得写一大堆的判断。
我现在的做法是(代码如下):
问题是:我不想让外面传入Record,想传入对象,请问Entity可以转换成Record吗
public static Cnd getCndByEntity(Class clazz, Record rs) throws SQLException {
try {
Cnd cnd=Cnd.NEW();
Cnd.where(new Static("1=1"));
Object obj = clazz.newInstance();
Field[] fields = clazz.getDeclaredFields();
int index = 0;
Set<String> set=((Record) rs).getColumnNames();
Iterator iterator= set.iterator();
while(iterator.hasNext()){//遍历
String columnName = iterator.next().toString();
for (Field field : fields) {
String name = field.getName();
String newColumnName = columnName;
while (((index = newColumnName.indexOf("_")) > 0) && index < newColumnName.length()) { // 两个条件排除了下划线是第一个和最后一个字符的可能性
newColumnName = newColumnName.substring(0, index)
+ Character.toUpperCase(newColumnName.charAt(index + 1))
+ newColumnName.substring(index + 2);
}
if (name.equals(newColumnName)) { // 如果属性相同
Class clType = field.getType();
String typeName = clType.getName();
newColumnName = "set" + Character.toUpperCase(newColumnName.charAt(0))
+ newColumnName.substring(1);
if (typeName.equals("int")) {
int value = rs.getInt(columnName);
Method method = clazz.getMethod(newColumnName, Integer.class);
method.invoke(obj, value);
cnd.and(name,"=",value);
break;
} else if (typeName.equals("double")) {
double value = rs.getDouble(columnName);
Method method = clazz.getMethod(newColumnName, Double.class);
method.invoke(obj, value);
cnd.and(name,"=",value);
break;
}else if (typeName.equals("java.lang.String")) {
String value = rs.getString(columnName);
Method method = clazz.getMethod(newColumnName, String.class);
method.invoke(obj, value);
cnd.and(name,"like","%"+value+"%");
break;
} else if (typeName.equals("java.lang.Integer")) {
Integer value = rs.getInt(columnName);
Method method = clazz.getMethod(newColumnName, Integer.class);
method.invoke(obj, value);
cnd.and(name,"=",value);
break;
} else if (typeName.equals("java.lang.Double")) {
Double value = rs.getDouble(columnName);
Method method = clazz.getMethod(newColumnName, Double.class);
method.invoke(obj, value);
cnd.and(name,"=",value);
break;
} else if (typeName.equals("java.lang.Long")) {
Long value = rs.getLong(columnName);
Method method = clazz.getMethod(newColumnName, Long.class);
method.invoke(obj, value);
cnd.and(name,"=",value);
break;
} else if (typeName.equals("long")) {
long value = rs.getLong(columnName);
Method method = clazz.getMethod(newColumnName, Long.class);
method.invoke(obj, value);
cnd.and(name,"=",value);
break;
} else if (typeName.equals("java.sql.Timestamp")) {
Timestamp value = rs.getTimestamp(columnName);
Method method = clazz.getMethod(newColumnName, Timestamp.class);
method.invoke(obj, value);
cnd.and(name,"=",value);
break;
}
}
}
}
return cnd;
} catch (InstantiationException e) {
return null; //Result.error("实例化实体" + clazz.getName() + "出错");
} catch (IllegalAccessException e) {
return null; // Result.error("无法访问实体" + clazz.getName());
} catch (NoSuchMethodException e) {
return null; // Result.error("反射实体" + clazz.getName() + "出错");
} catch (SecurityException e) {
return null; // Result.error("有不可访问的反射实体属性或方法" + clazz.getName() + "出错");
} catch (IllegalArgumentException e) {
return null; // Result.error("反射给实体方法赋值出错:" + clazz.getName());
} catch (InvocationTargetException e) {
return null; // Result.error("反射出错");
}
}