How to return multiple result sets spring JdbcTemplate

Recently little hair blog, first go *** the company for a few years, completely unable to external networks, then came out again, can last outside the network, but the project has been too busy playing in the yard, but also from the language C # replaced JAVA.

Fortunately, the more similar the two, turn up fairly easily, recently discovered that C # in order to get multiple result sets installed directly DataTable DataSet inside it at the time of the operation sqlserver database, JAVA this

Side wheels very much, but this needs to be done but not as good as C # convenience.

JdbcTemplate currently used to operate the database, and finally found a good solution can be achieved to obtain multiple result sets, thanks to  https://weiku.co/article/177/  bloggers share.

Here is the demo

 
 
/***
* 获取多结果集
* @return
*/
public List<List<Map<String, Object>>> getMultiResult() {


String sql = "select 1 select 2 select 3";

List<List<Map<String, Object>>> result = jdbcTemplate.execute(sql, (CallableStatementCallback<List<List<Map<String, Object>>>>) cs -> {
List<List<Map<String, Object>>> list = new ArrayList<>();
boolean execute = cs.execute();

while (execute) {
ResultSet resultSet = cs.getResultSet();
List<Map<String, Object>> subList = new ArrayList<>();
while (resultSet.next()) {
ResultSetMetaData meta = resultSet.getMetaData();
int colcount = meta.getColumnCount();
Map<String, Object> map = new HashMap<>();
for (int i = 1; i <= colcount; i++) {
String name = meta.getColumnLabel(i);
map.put(name, resultSet.getObject(i));
}
subList.add(map);
}
list.add(subList);
execute = cs.getMoreResults();
}
return list;
});
return result;
}
 

The main thing is to use the callback object to do the processing, java generics due to the technology selection with C # is not the same, so a lot of times C # is not as convenient, but fortunately the compiler can basically deal with these.

(CallableStatementCallback <List <List <the Map <String, Object >>>>) cs -> {this line of code is critical, if not support lambda expressions, can implement this interface, passing a specific category.

Guess you like

Origin www.cnblogs.com/lclblog/p/11698339.html