ResultSet转换成list,速度异常慢。
ResultSetMetaData metaData = rs.getMetaData();
int col_len = metaData.getColumnCount();
Map<String, Object> map = null;
while (rs.next()) {
map = new HashMap<>();
for (int i = 0; i < col_len; i++) {
String cols_name = metaData.getColumnName(i + 1);
Object cols_value = rs.getObject(cols_name);
if (cols_value == null) {
cols_value = "";
}
map.put(cols_name, cols_value);
}
list.add(map);
}
主要原因是metaData.getColumnName()调用会执行表查询,在大量调用后耗时显现出来。
代码修改如下可以解决:
ResultSetMetaData metaData = rs.getMetaData();
int cols = metaData.getColumnCount();
String[] names = new String[cols + 1];
for (int i = 1; i <= cols; i++) {
names[i] = metaData.getColumnName(i);
}
Map<String, Object> map = null;
while (rs.next()) {
map = new HashMap<>();
for (int i = 0; i < cols; i++) {
Object cols_value = rs.getObject(names[i + 1]);
if (cols_value == null) {
cols_value = "";
}
map.put(names[i + 1], cols_value);
}
list.add(map);
}```