mybatis 呼叫儲存過程

2021-07-29 12:56:24 字數 1897 閱讀 5331

引數形式: 

create procedure sptest.adder(in addend1 integer, in addend2 integer, out thesum integer)  

begin atomic  

set thesum = addend1 + addend2;   

end  

go  

parametermap>  

lt;update id="adderwithparametermap" parametermap="testparametermap" statementtype="callable">  

update>  

public void testadderasupdatewithparametermap()  finally   

帶輸入輸出引數的儲存過程: 

sql**:

create procedure sptest.getnames(in lowestid int, out totalrows integer)  

reads sql data  

dynamic result sets 1  

begin atomic  

declare cur cursor for select * from sptest.names where id >= lowestid;  

select count(*) into totalrows from sptest.names where id >= lowestid;  

open cur;  

end  

go  

resultmap="nameresult">  

,  #)}  

select>  

select>  

public void testcallwithresultset2_a1()  finally   

}  

返回多個結果集 

sql**:

create procedure sptest.getnamesanditems()  

reads sql data  

dynamic result sets 2  

begin atomic  

declare cur1 cursor for select * from sptest.names;  

declare cur2 cursor for select * from sptest.items;  

open cur1;  

open cur2;  

end  

go  

resultmap>  

resultmap>  

resultmap="nameresult,itemresult">  

select>  

@test  

public void testgetnamesanditems() throws sqlexception  finally   

}  

注意: 

上面就是幾種常用的了。 

1 sqlserver oracle sqlserver返回結果集是可以不要out引數的。如果sql中用的是select出結果,不需要配置out引數。多個結果集/結果集可以配置resultmap 來返回list,主要是呼叫selectlist方法會自動把結果集加入到list中去的。 

2 sql有返回值 用select標籤 

3 注意sql引數和mybatis引數的對應關係,這個這裡就不講了。 

4 注意引數個數 

我遇到的異常: 

引數不匹配的原因,因為sqlserver 中我是直接返回select臨時表結果,不需要配置儲存過程輸出引數。 

list中的內容形式: 

mybatis 呼叫儲存過程

至於為什麼用map作為引數,是因為別人寫的儲存過程 可能沒有返回出參,然後就會出現下面的問題。但是別人幾百行上千行的儲存過程,我是絕對不敢去動的。然後就只能用可以為null的物件去接收返回值了,所以就從實體變為了map。void callpwfsubmit mapmap 獲取儲存過程所需要的引數 p...

mybatis 呼叫儲存過程

mybatis中的statementtype詳解 呼叫儲存過程總共有兩總語句 call 和exec 兩種語句複製一下 示例更改即可使用 call 語句 call sp sanwjimport auto exec 語句 exec sp tmailauto 4651 1.使用 update 標籤 2.i...

mybatis 呼叫儲存過程

呼叫的儲存過程create definer root localhost procedure querystudentname in id char 10 out stuname varchar 10 begin select sname into stuname from student wher...