//T多表查询中的一 如:年级
//M多表查询中的多 如:学生
//field年级类在学生集合的属性名
//field2学生类中年级对象的属性名
//resultSet多表查询得到的结果集
public static <T, M> T test(Class<T> tClass, Class<M> mClass, String field, String field2, ResultSet resultSet) {
Field[] tFields = tClass.getDeclaredFields();
Method[] tMethods = tClass.getMethods();
Field[] mFields = mClass.getDeclaredFields();
Method[] mMethods = mClass.getMethods();
T t = null;
M m = null;
try {
m = mClass.newInstance();
for (Field mField : mFields) {
for (Method mMethod : mMethods) {
if (mMethod.getName().startsWith("set")) {
String mFieldByMethodName = mMethod.getName().substring(3, mMethod.getName().length()).toLowerCase();
if (mFieldByMethodName.equals(field)) {
} else {
if (mFieldByMethodName.equals(mField.getName())) {
mMethod.invoke(m, resultSet.getObject(mField.getName()));
}
}
}
}
}
t = tClass.newInstance();
for (Field tField : tFields) {
for (Method tMethod : tMethods) {
if (tMethod.getName().startsWith("set")) {
String tFieldByMethodName = tMethod.getName().substring(3, tMethod.getName().length()).toLowerCase();
if (tFieldByMethodName.equals(tField.getName())) {
if (tFieldByMethodName.equals(field2)) {
} else {
tMethod.invoke(t, resultSet.getObject(tField.getName()));
}
}
if (tFieldByMethodName.equals(field2)) {
List list = new ArrayList();
list.add(m);
tMethod.invoke(t, list);
}
}
}
}
} catch (InstantiationException e) {
e.printStackTrace();
} catch (IllegalAccessException e) {
e.printStackTrace();
} catch (InvocationTargetException e) {
e.printStackTrace();
} catch (SQLException e) {
e.printStackTrace();
}
return t;
}