處理JdbcTemplate放回的結果集
阿新 • • 發佈:2019-02-15
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 {
}