您的位置:首页 > 其它

处理JdbcTemplate放回的结果集

2013-10-20 11:27 246 查看
ResultSet中结果反射

import java.lang.reflect.*;
import java.sql.*;
import java.util.*;
import java.util.Date;

public class ResultToBean {

/**
* 将ResultSet内容映射到实体 须有 ColToProperty注解
* @param rs
* @param clazz
* @return
* @throws SecurityException
* @throws NoSuchMethodException
* @throws InstantiationException
* @throws IllegalAccessException
* @throws SQLException
* @throws IllegalArgumentException
* @throws InvocationTargetException
*/
public static <T> List<T> getBean(ResultSet rs, Class<T> clazz)
throws SecurityException, NoSuchMethodException,
InstantiationException, IllegalAccessException, SQLException,
IllegalArgumentException, InvocationTargetException {
List<T> l = new ArrayList<T>();
Method method = null;
T obj = null;
while (rs.next()) {
obj = clazz.newInstance();
Field fs[] = clazz.getDeclaredFields();
int count = 1;
for (Field f : fs) {
ColToProperty colToProperty = f
.getAnnotation(ColToProperty.class);
if (null == colToProperty) {
continue;
}
String m = f.getName().substring(0, 1).toUpperCase()
+ f.getName().substring(1);
if (f.getType() == java.lang.Boolean.class) {
method = clazz.getDeclaredMethod("set" + m, Boolean.class);
method.invoke(obj, rs.getBoolean(count));
} else if (f.getType() == boolean.class) {
method = clazz.getDeclaredMethod("set" + m, boolean.class);
method.invoke(obj, rs.getBoolean(count));
} else if (f.getType() == java.util.Date.class
|| f.getType() == Date.class) {
method = clazz.getDeclaredMethod("set" + m, Date.class);
method.invoke(obj, rs.getDate(count));
} else if (f.getType() == java.lang.Double.class) {
method = clazz.getDeclaredMethod("set" + m, Double.class);
method.invoke(obj, rs.getDouble(count));
} else if (f.getType() == Double.class) {
method = clazz.getDeclaredMethod("set" + m, double.class);
method.invoke(obj, rs.getDouble(count));
} else if (f.getType() == java.lang.Float.class) {
method = clazz.getDeclaredMethod("set" + m, Float.class);
method.invoke(obj, rs.getFloat(count));
} else if (f.getType() == Float.class) {
method = clazz.getDeclaredMethod("set" + m, float.class);
method.invoke(obj, rs.getFloat(count));
} else if (f.getType() == java.lang.Integer.class) {
method = clazz.getDeclaredMethod("set" + m, Integer.class);
method.invoke(obj, rs.getInt(count));
} else if (f.getType() == int.class) {
method = clazz.getDeclaredMethod("set" + m, int.class);
method.invoke(obj, rs.getInt(count));
} else if (f.getType() == java.lang.Short.class
|| f.getType() == short.class) {
method = clazz.getDeclaredMethod("set" + m, Short.class);
method.invoke(obj, rs.getShort(count));
} else if (f.getType() == java.lang.String.class) {
method = clazz.getDeclaredMethod("set" + m, String.class);
method.invoke(obj, rs.getString(count));
} else if (f.getType() == java.lang.Long.class
|| f.getType() == long.class) {
method = clazz.getDeclaredMethod("set" + m, Long.class);
method.invoke(obj, rs.getLong(count));
} else {
throw new RuntimeException("非基础数据类型,无法正确匹配,请检查ColToProperty是否使用正确!");
}
count++;
}
l.add(obj);
}
return l;
}
}


import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;

@Retention(RetentionPolicy.RUNTIME)
public @interface ColToProperty {

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息