问题
i'm using MyBatis to call a function in a PL SQL database. The function have one of the OUT parameter as BOOLEAN like this:
FUNCTION f_foo_function(foo IN VARCHAR, foo_output OUT BOOLEAN, error OUT VARCHAR2)
My problem is when i try to call the function from my xml mapper, every try i do mybatis don't recognize the boolean output and throw me and error like incompatible types. Also when i try to test the function from PLSQL Developer it make a convertion like this
:pout_boolean_result := sys.diutil.bool_to_int(pout_boolean_result);
and return me the boolean as a bit.
It is posible to ignore that integer and specify MyBatis to treat the ouput as a boolean? How can i do that?
My xml is something like this:
<select id="getFooFunction" statementType="CALLABLE">
{#{result, mode=OUT, jdbcType=INTEGER} = call f_foo_function
(
#{foo, mode=IN, jdbcType=VARCHAR},
#{foo_output, mode=OUT, jdbcType=DOUBLE},
#{error, mode=OUT, jdbcType=VARCHAR}
)
}
</select>
回答1:
To test it, I defined the function as follows.
create or replace function f_foo_function(
foo in varchar,
foo_output out integer,
error out varchar2
) return integer is
begin
foo_output := 1;
error := 'No error';
return 99;
end;
foo_output
is defined as INTEGER
as BOOLEAN
is invalid as APC pointed out.
The mapper method is defined as...
void getFooFunction(FooFuncParam param);
The parameter is a POJO.
public class FooFuncParam {
private Integer result;
private String foo;
private boolean fooOutput;
private String error;
// getters/setters
}
And here is the mapper statement.
<update id="getFooFunction" statementType="CALLABLE">
{#{result,jdbcType=INTEGER,mode=OUT} = call f_foo_function(
#{foo,mode=IN},
#{fooOutput,jdbcType=INTEGER,javaType=_boolean,mode=OUT},
#{error,jdbcType=VARCHAR,mode=OUT})}
</update>
Note that javaType
is specified to convert INTEGER
to boolean
.
If the function sets 0
to foo_output
, false
is set to FooFuncParam.fooOutput
.
And non-zero value means true
. If necessary, you can write a custom type handler to change the behavior.
Here is an executable demo tested with Oracle 18c and ojdbc 19.3.0.0.
来源:https://stackoverflow.com/questions/58205549/how-can-i-call-a-function-that-return-a-boolean-using-mybatis