方法:
public static <T> List<T> processResultSetToList(ResultSet rs, Class<T> clazz) throws Exception { List<T> ls = new ArrayList<T>(); Field[] fields = clazz.getDeclaredFields(); while (rs.next()) { T tt = clazz.newInstance(); for (Field field : fields) { try { /** * 如果Oracle数据库中的类型是 number p.getWriteMethod().invoke(tt, new * Object[] { rs.getObject(p.getName()) }) 会报如下错误 * java.lang.IllegalArgumentException: argument type * mismatch --- 另外如果mysql数据库中的bigint 在进行setXxxx(Integer * val)时候也会抛出该异常 也会抛出该异常 */ Object value = null; try { String fieldName = getColumnName(field); switch (TypeUtils.getMappedType(field)) { case Long: value = Long.valueOf(rs.getLong(fieldName)); break; case Integer: value = Integer.valueOf(rs.getInt(fieldName)); break; case Double: value = Double.valueOf(rs.getDouble(fieldName)); break; case Byte: InputStream ins = rs.getBinaryStream(fieldName); if(ins != null){ value = IOUtils.toByteArray(ins); } break; case Date: /** * 15-12-8 * 转为util.Date */ Timestamp t = rs.getTimestamp(fieldName); if(t!=null){ value = new Date(t.getTime()); } break; default: value = rs.getObject(fieldName); break; } } catch (Exception e) {// 如果仅仅查询Class的部分字段 if (e.getMessage().matches("Column\s+'.+?'\s+not\s+found.")) { switch (TypeUtils.getMappedType(field)) { case Long: case Integer: value = 0; break; default: value = null; break; } } } field.setAccessible(true); if(value != null){ field.set(tt, value); } } catch (IllegalArgumentException e) { e.printStackTrace(); } } ls.add(tt); } DBUtils.close(rs, null, null); return ls; }