Hint
This article is generated by AI translation.
Multiple Results
The multipleExecute series of methods in JdbcTemplate solves the problem of SQL statements producing multiple values that are hard to identify and retrieve.
When It Happens
- Sending multiple unsplit SQL statements in a single request.
Example: send the entire block to MySQL
set @userName = convert(? USING utf8);
select * from test_user where name = @userName;
select * from test_user where name != @userName;infoSome databases (e.g., MySQL) require enabling multi-statement support, such as
allowMultiQueries=truein the JDBC URL. - Stored procedures returning multiple result sets, intentionally or not.
Example: MySQL procedure yielding two result sets
create procedure proc_multi_result(in userName varchar(200))
begin
select * from test_user where name = @userName;
select * from test_user where name != @userName;
end; - Other situations that produce multiple result sets.
Usage
One argument, two result sets: matches vs mismatches
String multipleSql = " set @userName = convert(? USING utf8);" +
" select * from test_user where name = @userName;" +
" select * from test_user where name != @userName;";
Map<String, Object> result = jdbc.multipleExecute(multipleSql, "muhammad");
- See Arguments for all supported binding styles.
Return Value
- By default, each result set is represented as List/Map. The example above yields a
Listcontaining twoMapentries. - You can embed rules in SQL to control how each result set is handled.
1. Annotate statements with @{resultSet} rules
➊ set @userName = convert(? USING utf8); ➋ @{resultUpdate,name=upd}
➌ select * from test_user where name = @userName; ➍ @{resultSet,name=res1,javaType=net.demo.dto.User}
➎ select * from test_user where name != @userName; ➏ @{resultSet,name=res2,javaType=net.demo.dto.User}
Explanation: