使用Mybatis从Oracle函数返回值

时间:2016-12-20 23:42:34

标签: oracle mybatis

我已经看过很多关于这个问题的StackOverflow问题,但找不到任何有意义的问题。 This one最接近,但没有显示如何从函数中获取返回值。

这是我的映射器调用:

public Long callMyFunction(@Param("recordId") Long recordId, @Param("otherId") Long otherId, @Param("date") Date date, @Param("comments") String comments);

这是映射器XML:

<select id="callMyFunction" parameterType="map" statementType="CALLABLE"  resultType="java.lang.Long">
    {  #{resultId,javaType=java.lang.Long,jdbcType=NUMERIC,mode=OUT} = call MYSCHEMA.MYPACKAGE.my_function(
    #{recordId,jdbcType=NUMERIC,mode=IN},
    #{otherId,jdbcType=NUMERIC,mode=IN},
    #{date,jdbcType=DATE,mode=IN},
    #{comments,jdbcType=VARCHAR,mode=IN})}
</select>

调用有效,但返回值(resultId)始终为null。

有人能发现问题吗?

1 个答案:

答案 0 :(得分:2)

如果要直接返回结果,则SQL调用必须是: try: f = foo() a = f if f is not None else b except AttributeError: a = b

如果你想继续在过程调用样式中调用函数,这意味着结果是OUT 参数(你env声明它是OUT)。 最小的更改包括向mapper方法签名添加一个参数: SELECT MYSCHEMA.MYPACKAGE.my_function(...) FROM DUAL

在XML中:忘记 resultType ,这是 select 的。电话: public Long callMyFunction(@Param("recordId") Long recordId, @Param("otherId") Long otherId, @Param("date") Date date, @Param("comments") String comments, @Param("resultIdContainer") Map<String, Long> resultIdContainer);

不是我在这里使用地图来包含resutlId:需要间接:函数会将参数'result'值写入稍后你可以读取的地方(在你的mapper调用之后),你也可以使用一个类resultId属性。

相关问题