SimpleJDBCCall 用结果集处理参数
SimpleJDBCCall handle out paramater with resultset
我正在使用 spring jdbc。我想要没有参数的结果集。分开我做了但是在一起我做不到。
CREATE DEFINER=`xxx`@`%` PROCEDURE `client_xxxx`(
IN p_xxxx TINYINT(1) UNSIGNED,
IN p_result SMALLINT(2) UNSIGNED,
OUT p_result BIT ) BEGIN
IF EXISTS(SELECT 1 FROM xxx WHERE xxx = 1 AND xxx = 1) THEN
SELECT ...;
SET p_result = 0;
ELSE
SELECT ...;
SET p_result = 1;
END IF;
END
springjdbc代码
SimpleJdbcCall jdbcCall = new SimpleJdbcCall(dataSource).withProcedureName(sp);
List<Map<String, Object>> list = (List<Map<String, Object>>) jdbcCall.execute(paramsArray).get("#result-set-1");
list
得到结果集和结果集 我怎样才能得到 p_result
我找到了我想念的简单方法。
public Map<String, Object> xxx(String sp, Object... paramsArray) {
SimpleJdbcCall jdbcCall = new SimpleJdbcCall(dataSource).withProcedureName(sp);
return jdbcCall.execute(paramsArray);
}
execute()
给两个参数默认
IE。
1)#result-set-1
2) #update-count-1
#result-set-1
结果集即 select record
和 #update-count-1
returns 更新计数。如果我们想使用带有 out
参数的 select
语句访问结果。我们只需要声明 out
参数。 execute()
给出了 Map<String, Object>
类型的所有东西。
所以从 map 我们可以得到 stored procedure
returns.
的所有多个值
例如我的 SP
喜欢
PROCEDURE xxx(
IN xxxxTINYINT(1) UNSIGNED,
IN xxxSMALLINT(2) UNSIGNED,
OUT p_isconfig BIT
)
BEGIN
SELECT....
SET p_isconfig = 1;
END
所以在 #result-set-1
我得到 select 结果。
p_isconfig
给我结果。如果您有任何困惑,那么您可以迭代地图并确定如何获取 return 参数。
Iterator i = map.keySet().iterator();
while ( i.hasNext() ) {
String key = (String) i.next();
String value = params.get( key );
System.out.println("key: " + key + " --- value: " + value) ;
}
通过这种方式,我在阅读了很多东西后找到了解决方案。如果有人对此解决方案有其他选择,请与我分享。
您可以尝试 morejdbc (available in maven central) 来调用您的过程,它更简洁并且类型安全:
import static org.morejdbc.SqlTypes.BIGINT;
import static org.morejdbc.NamedJdbcCall.call;
import org.morejdbc.*;
...
private JdbcTemplate jdbcTemplate;
...
Out<Integer> out = Out.of(INTEGER);
jdbcTemplate.execute(call("client_xxxx")
.in("p_xxxx", pValue)
.out("p_result", out));
System.out.println("Result is " + out.get());
对于ref_cursor
输出参数和Oracle数据库你可以使用
Out<List<Record>> out = Out.of(OracleSqlTypes.cursor((rs, idx) -> new Record(rs)));
jdbcTemplate.execute(call("client_xxxx")
.in("p_xxxx", pValue)
.out("p_result", out)); // will auto-close ref-cursor
System.out.println("Got result records: " + out.get());
我正在使用 spring jdbc。我想要没有参数的结果集。分开我做了但是在一起我做不到。
CREATE DEFINER=`xxx`@`%` PROCEDURE `client_xxxx`(
IN p_xxxx TINYINT(1) UNSIGNED,
IN p_result SMALLINT(2) UNSIGNED,
OUT p_result BIT ) BEGIN
IF EXISTS(SELECT 1 FROM xxx WHERE xxx = 1 AND xxx = 1) THEN
SELECT ...;
SET p_result = 0;
ELSE
SELECT ...;
SET p_result = 1;
END IF;
END
springjdbc代码
SimpleJdbcCall jdbcCall = new SimpleJdbcCall(dataSource).withProcedureName(sp);
List<Map<String, Object>> list = (List<Map<String, Object>>) jdbcCall.execute(paramsArray).get("#result-set-1");
list
得到结果集和结果集 我怎样才能得到 p_result
我找到了我想念的简单方法。
public Map<String, Object> xxx(String sp, Object... paramsArray) {
SimpleJdbcCall jdbcCall = new SimpleJdbcCall(dataSource).withProcedureName(sp);
return jdbcCall.execute(paramsArray);
}
execute()
给两个参数默认
IE。
1)#result-set-1
2) #update-count-1
#result-set-1
结果集即 select record
和 #update-count-1
returns 更新计数。如果我们想使用带有 out
参数的 select
语句访问结果。我们只需要声明 out
参数。 execute()
给出了 Map<String, Object>
类型的所有东西。
所以从 map 我们可以得到 stored procedure
returns.
例如我的 SP
喜欢
PROCEDURE xxx(
IN xxxxTINYINT(1) UNSIGNED,
IN xxxSMALLINT(2) UNSIGNED,
OUT p_isconfig BIT
)
BEGIN
SELECT....
SET p_isconfig = 1;
END
所以在 #result-set-1
我得到 select 结果。
p_isconfig
给我结果。如果您有任何困惑,那么您可以迭代地图并确定如何获取 return 参数。
Iterator i = map.keySet().iterator();
while ( i.hasNext() ) {
String key = (String) i.next();
String value = params.get( key );
System.out.println("key: " + key + " --- value: " + value) ;
}
通过这种方式,我在阅读了很多东西后找到了解决方案。如果有人对此解决方案有其他选择,请与我分享。
您可以尝试 morejdbc (available in maven central) 来调用您的过程,它更简洁并且类型安全:
import static org.morejdbc.SqlTypes.BIGINT;
import static org.morejdbc.NamedJdbcCall.call;
import org.morejdbc.*;
...
private JdbcTemplate jdbcTemplate;
...
Out<Integer> out = Out.of(INTEGER);
jdbcTemplate.execute(call("client_xxxx")
.in("p_xxxx", pValue)
.out("p_result", out));
System.out.println("Result is " + out.get());
对于ref_cursor
输出参数和Oracle数据库你可以使用
Out<List<Record>> out = Out.of(OracleSqlTypes.cursor((rs, idx) -> new Record(rs)));
jdbcTemplate.execute(call("client_xxxx")
.in("p_xxxx", pValue)
.out("p_result", out)); // will auto-close ref-cursor
System.out.println("Got result records: " + out.get());