Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save vahidhedayati/da89e8482aa6e9feb0b178af141d8788 to your computer and use it in GitHub Desktop.
Save vahidhedayati/da89e8482aa6e9feb0b178af141d8788 to your computer and use it in GitHub Desktop.
Return a dynamic map regardless of query using namedParameterJdbcTemplate
List<Map<String, Object>> result = namedParameterJdbcTemplate.query(query, whereParams, rs -> {
List<Map<String, Object>> rows = new ArrayList<>();
ResultSetMetaData metaData = rs.getMetaData();
int columnCount = metaData.getColumnCount();
while (rs.next()) {
Map<String, Object> columns = new LinkedHashMap<>();
for (int i = 1; i <= columnCount; i++) {
columns.put(metaData.getColumnLabel(i), rs.getObject(i));
}
rows.add(columns);
}
return rows;
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment