Mybatis调存储过程、函数,获取多个出参(Postgre版)

436 阅读1分钟

1、创建存储过程、创建函数

--创建存储过程
create or replace procedure procedure_add_and_sub(a int, b int, INOUT add_result int, INOUT sub_result int)
    language plpgsql
as
$$
begin

    select a + b into add_result;
    select a - b into sub_result;

end;
$$
;
--创建函数
create or replace procedure procedure_str(a varchar, b varchar, INOUT s1 varchar, INOUT s2 varchar)
    language plpgsql
as
$$
begin

    select a || b ||'1' into s1;
    select a || b ||'2' into s2;

end;
$$
;

测试

select function_add_and_sub(2, 1, 0, null);

image.png

call procedure_add_and_sub(2, 1, 0, null);

image.png

2、准备mybatis语句

<select id="testFunction" resultType="java.util.Map">
    select function_add_and_sub(#{a,mode=INOUT,jdbcType=INTEGER}, #{b,mode=INOUT,jdbcType=INTEGER}, null, null)
</select>
<select id="testProcedure" resultType="java.util.Map">
    call procedure_add_and_sub(#{a,jdbcType=INTEGER}, #{b}, null, null)
</select>

3、准备api

@RequestMapping()
public ResponseEntity<?> index(int a, int b) {
    /*mybatis数据类型对不上postgre数据类型*/
    final Map<String, ?> params = Map.of("a", a, "b", b);
    final Map result_1 = testMapper.testProcedure(params);
    final Map result_2 = testMapper.testFunction(params);
    return ResponseEntity.ok("testProcedure: " + result_1.toString() + "\ntestFunction: " + result_2.toString());
}

4、测试请求

image.png