Getting the primitive type return value from PL/SQL function

754 views
Skip to first unread message

wigbam

unread,
May 23, 2011, 12:54:23 PM5/23/11
to mybatis-user
Hi,

I am reasonably new to MyBatis and have been struggling with what I
thought was a simple problem for the whole of the afternoon. I have a
stored function in my Oracle DB which returns an Integer (NUMBER in
Oracle). The questions is how can I get the return value back to Java?
Spending a day online looking for an answer hasn't got me any further
from where I was in the beginning.

PL/SQL:

...

FUNCTION add_govbond (
currency IN VARCHAR2,
TENOR IN VARCHAR2)
return NUMBER is
NEW_ID NUMBER;
BEGIN
SELECT instrument_id_key_seq.nextval INTO new_id FROM DUAL;
INSERT INTO TESTDATA.GOVBOND_KEY(instrument_id, currency, tenor)
values (NEW_ID, CURRENCY, TENOR);
RETURN new_id;
END;

...

And XML config file:

...

<parameterMap id="GovBondKey" type="map">
<parameter property="id" jdbcType="NUMERIC"
javaType="java.lang.Integer" mode="OUT" />
<parameter property="currency" jdbcType="VARCHAR"
javaType="java.lang.String" mode="IN" />
<parameter property="tenor" jdbcType="VARCHAR"
javaType="java.lang.String" mode="IN" />
</parameterMap>

<select id="insertGenericGovernmentBondKey"
statementType="CALLABLE" parameterMap="GovBondKey">
{ ? = call FINAL.add_govbond(?, ?) }
</select>

...


The Java part looks equally trivial:

DAOMapper.java:

...
Integer insertGenericGovernmentBondKey(@Param("currency")
String currency, @Param("tenor") String tenor);
...

Main.java:

...
Integer id =
sqlSession.getMapper(DAOMapper.class).insertGenericGovernmentBondKey(key.getCurrency(),
key.getTenor());
System.out.println("Id: " + id);
...

When the above code executes, id remains null. I have went as far as
debugging MyBatis PreparedStatement result handler and it seems as it
completely ignores out parameters (???), only bothering about
ResultSet, which is, of course, empty in my case.
This seems to be such a trivial/common case, surely there must be a
way of making this work?
Any help would be appreciated. Thanks.

meindert hoving

unread,
May 24, 2011, 3:03:07 AM5/24/11
to mybatis-user
Here is how I do it:
In my mapper (xml) I have a mapper for a POJO that is used for the
procedure call
[code]
<parameterMap id="ratioParameters"
type="esos.shared.data.account.RatioParam">
<parameter javaType="java.lang.Object" jdbcType="INTEGER" mode="OUT"
property="RC" />
<parameter javaType="java.lang.String" jdbcType="VARCHAR" mode="IN"
property="ratio.ratioId" />
<parameter javaType="java.lang.String" jdbcType="VARCHAR" mode="IN"
property="dealerCodes" />
<parameter javaType="esos.shared.data.mybatis.MonthlyTotal$DataSet"
jdbcType="VARCHAR" mode="IN" property="dataSet" />
<parameter javaType="java.lang.String" jdbcType="VARCHAR" mode="IN"
property="resultType" />
<parameter javaType="java.lang.Integer" jdbcType="INTEGER" mode="IN"
property="actMonth" />
<parameter javaType="java.lang.Integer" jdbcType="INTEGER" mode="IN"
property="noOfMonth" />
<parameter javaType="java.lang.Integer" jdbcType="INTEGER" mode="IN"
property="nytdmonths" />
<parameter javaType="java.lang.Integer" jdbcType="INTEGER" mode="IN"
property="debugLevel" />
<parameter javaType="java.math.BigDecimal" jdbcType="DECIMAL"
mode="OUT" property="result" />
</parameterMap>
[/code]
The actual stored proc call is a update tag
[code]
<update id="evalRatio" parameterMap="ratioParameters"
statementType="CALLABLE">
{? =call evalRatioValue( ?, ?, ?, ?, ?, ?, ?, ?, ? )}
</update>
[/code]
And the java interface;
[code]
void evalRatio(RatioParam ratioParam);
[/code]
> sqlSession.getMapper(DAOMapper.class).insertGenericGovernmentBondKey(key.ge­tCurrency(),
Reply all
Reply to author
Forward
0 new messages