我已经看到了一些从simpleJdbcCall.executeFunction
获取单个原始结果的例子。我需要知道如何访问一组结果。 execute
会返回Map
。您必须向SqlOutParameter
提供executeFunction
并告诉它要返回的类型。好吧,我将返回映射到MyCustomClass.class
,它返回null。我知道该函数将返回3 BIGINT
个参数值。
public class MyDao {
private final JdbcTemplate theTemplate;;
//@Autowired ctor
public long createCategory(final CategoryEntity category) {
final SimpleJdbcCall jdbcCall = getJdbcCall();
return mapResult(jdbcCall.executeFunction(MyResultEntity.class, fromEntity(category)));
}
private long mapResult(MyResultEntity result) {
return result.getOut_id();//FAIL
}
private SqlParameterSource fromEntity(final MyEntity category) {
Map<String,Object> params = new HashMap<>();
params.put("in_name", category.getName());
params.put("in_description", category.getDescription());
SqlParameterSource result = new MapSqlParameterSource(params);
return result;
}
private SimpleJdbcCall getJdbcCall() {
//I need the value returned by "out_id"
SimpleJdbcCall jdbcCall = new SimpleJdbcCall(theTemplate)
.withSchemaName("mySchema")
.withFunctionName("my_function")
.declareParameters(
new SqlOutParameter("out_id", Types.BIGINT),
new SqlOutParameter("out_2", Types.BIGINT),
new SqlOutParameter("out_3", Types.BIGINT)
);
return jdbcCall;
}
}
答案 0 :(得分:1)
我发现您也可以使用execute
来调用函数。然后只需使用Map
即可获得结果:
public long createCategory(final MyEntity category) {
final SimpleJdbcCall jdbcCall = getJdbcCall();
Map<String, Object> results = jdbcCall.execute(fromEntity(category));
return (long) results.get("out_id");
}