java实现jdbc查询结果集result转换成对应list集合
发布时间:2016-12-28 来源:查字典编辑
摘要:代码非常的简单,这里就不多废话了,直接奉上publicstaticListconvertToList(ResultSetrs,Classt)...
代码非常的简单,这里就不多废话了,直接奉上
public static <T> List<T> convertToList(ResultSet rs,Class<T> t) throws SQLException { List<Map<String, Object>> list = new ArrayList<Map<String, Object>>(); ResultSetMetaData md = (ResultSetMetaData) rs.getMetaData(); int columnCount = md.getColumnCount(); while (rs.next()) { Map<String, Object> rowData = new HashMap<String, Object>(); for (int i = 1; i <= columnCount; i++) { rowData.put(md.getColumnName(i), rs.getObject(i)); } list.add(rowData); } JSONArray jr = JSONArray.fromObject(list); List<T> resultList = JSONArray.toList(jr, t); return resultList; }
是不是很简单。。给新手参考下的,老鸟们请略过