使用 SimpleJdbcCall 从 oracle 函数获取返回值

αƞj*_*jiβ 3 java oracle spring

我将 Oracle 函数定义为:

function get_user_by_term (inUserTerm number) return number;
Run Code Online (Sandbox Code Playgroud)

现在我想使用 Spring SimpleJdbCCall 调用这个函数,但不确定如何读取返回值,因为我在函数中没有参数。我无法更改 Oracle 函数代码。

到目前为止我在 Java 中的代码是:

SimpleJdbcCall simpleJdbcCall = new SimpleJdbcCall(dataSource)
        .withSchemaName("SCHMA").withCatalogName("PKG_USER")
        .withProcedureName("get_user_by_term");
Map<String, Object> inParamMap = new HashMap<String, Object>();
inParamMap.put("inUserTerm ", userTermId );
SqlParameterSource in = new MapSqlParameterSource(inParamMap);
simpleJdbcCall.execute(in);
Run Code Online (Sandbox Code Playgroud)

αƞj*_*jiβ 5

经过研究我发现以下内容:

  1. 应该使用方法而不是withProcedureName()方法。withFunctionName()
  2. 而不是使用返回类型参数的execute()方法。executeFunction()

所以完整的代码就像

SimpleJdbcCall simpleJdbcCall = new SimpleJdbcCall(dataSource)
        .withSchemaName("SCHMA").withCatalogName("PKG_USER")
        .withFunctionName("get_user_by_term");
Map<String, Object> inParamMap = new HashMap<String, Object>();
inParamMap.put("inUserTerm ", userTermId );
SqlParameterSource in = new MapSqlParameterSource(inParamMap);
Long userId = simpleJdbcCall.executeFunction(BigDecimal.class, in).longValue();
Run Code Online (Sandbox Code Playgroud)

希望这对其他人也有帮助。