本文介绍了如何使用 MyBatis 调用返回 BOOLEAN 的函数?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用 MyBatis 调用 PL SQL 数据库中的函数.该函数的 OUT 参数之一为 BOOLEAN,如下所示:

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)

我的问题是当我尝试从我的 xml 映射器调用该函数时,我所做的每次尝试 mybatis 都无法识别布尔输出并抛出我和不兼容类型之类的错误.此外,当我尝试从 PLSQL Developer 测试该功能时,它会进行这样的转换:pout_boolean_result := sys.diutil.bool_to_int(pout_boolean_result);并将布尔值返回给我.

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.

是否可以忽略该整数并指定 MyBatis 将输出视为布尔值?我该怎么做?

我的 xml 是这样的:

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>

推荐答案

为了测试一下,我定义了如下函数.

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 被定义为 INTEGER 因为 BOOLEAN 如 APC 指出的那样无效.

foo_output is defined as INTEGER as BOOLEAN is invalid as APC pointed out.

mapper 方法定义为...

The mapper method is defined as...

void getFooFunction(FooFuncParam param);

参数是一个POJO.

public class FooFuncParam {
  private Integer result;
  private String foo;
  private boolean fooOutput;
  private String error;
  // getters/setters
}

这是映射器语句.

<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>

注意 javaType 是指定将 INTEGER 转换为 boolean.
如果函数将 0 设置为 foo_output,则 false 设置为 FooFuncParam.fooOutput.非零值意味着 true.如有必要,您可以编写自定义类型处理程序来更改行为.

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.

这是一个用 Oracle 18c 测试的可执行文件演示和 ojdbc 19.3.0.0.

Here is an executable demo tested with Oracle 18c and ojdbc 19.3.0.0.

这篇关于如何使用 MyBatis 调用返回 BOOLEAN 的函数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-22 09:31