當前位置:首頁 » 服務存儲 » mybatis調用存儲過程事務處理
擴展閱讀
webinf下怎麼引入js 2023-08-31 21:54:13
堡壘機怎麼打開web 2023-08-31 21:54:11

mybatis調用存儲過程事務處理

發布時間: 2023-07-24 00:04:34

⑴ mybatis調用存儲過程

<select id="selectUserById" statementType="CALLABLE" useCache="false">
{
call select_user_by_id(
#{userId,mode=IN},
#{realname,mode=OUT,jdbcType=VARCHAR},
#{username,mode=OUT,jdbcType=VARCHAR},
#{email,mode=OUT,jdbcType=VARCHAR},
#{phone,mode=OUT,jdbcType=VARCHAR},
#{remarks,mode=OUT,jdbcType=VARCHAR},
#{createDate,mode=OUT,jdbcType=TIMESTAMP}
)
}
</select>

⑵ MyBatis怎麼調用Postgresql存儲過程

1.調用沒有OUT參數的存儲過程:
創建存儲過程:
create
or
replace
function
get_code(a1
varchar(32))
returns
varchar(32)
as
$$
declare
the_result
varchar(32);
begin
the_result
:=
name
from
t_project
where
id
=
a1;
return
the_result;
end;
$$
language
plpgsql;
sqlMap配置文件:
<select
id="f1"
resultType="String"
parameterType="map"
statementType="CALLABLE"
useCache="false">
<![CDATA[
select
get_code(
#{a1,mode=IN,jdbcType=VARCHAR}
)
]]>
</select>
註:不使用OUT參數的存儲過程可以直接用
select
程序:
public
String
generateCode(String
a1)
{
Map<String,String>
paramMap
=
new
HashMap<String,String>();
paramMap.put("a1",
a1);
SqlSession
sqlSession
=
getSqlSession();
String
result
=
sqlSession.selectOne("f1",
paramMap);
return
result;
}
2.使用OUT參數的存儲過程:
創建存儲過程:
create
or
replace
function
testproc(a1
varchar(32),out
a2
varchar(32),out
a3
varchar(32))
as
$$
declare
begin
select
id
into
a2
from
t_project
where
id=a1;
select
name
into
a3
from
t_project
where
id=a1;
return;
end;
$$
language
plpgsql;
sqlMap配置文件:
<select
id="generateCode1"
parameterType="map"
statementType="CALLABLE"
useCache="false">
{
call
testproc(
#{a1,mode=IN,jdbcType=VARCHAR},
#{a2,mode=OUT,jdbcType=VARCHAR},
#{a3,mode=OUT,jdbcType=VARCHAR}
)
}
</select>
程序:
public
Map
generateCode1(String
a1)
{
Map<String,String>
paramMap
=
new
HashMap<String,String>();
paramMap.put("a1",
k1);
SqlSession
sqlSession
=
getSqlSession();
sqlSession.selectOne("generateCode1",
paramMap);
return
paramMap;
}
帶輸出參數的存儲過程,sqlSession.selectOne("generateCode1",
paramMap);
將paramMap傳入之後mybatis調用存儲過程,將paramMap進行填充
paramMap最後的值:{a1=R20148800900,
a2=R20148800900,
a3=項目名稱}

⑶ mybatis怎麼使用存儲過程

mybatis 配置 裡面 寫好存儲過程,然後直接調用。

編輯userMapper.xml
編輯userMapper.xml文件,添加如下的配置項

1 <!--
2 查詢得到男性或女性的數量, 如果傳入的是0就女性否則是男性
3 -->
4 <select id="getUserCount" parameterMap="getUserCountMap" statementType="CALLABLE">
5 CALL mybatis.ges_user_count(?,?)
6 </select>
7
8 <!--
9 parameterMap.put("sexid", 0);
10 parameterMap.put("usercount", -1);
11 -->
12 <parameterMap type="java.util.Map" id="getUserCountMap">
13 <parameter property="sexid" mode="IN" jdbcType="INTEGER"/>
14 <parameter property="usercount" mode="OUT" jdbcType="INTEGER"/>
15 </parameterMap>

四、編寫單元測試代碼

1 package me.gacl.test;
2
3 import java.util.HashMap;
4 import java.util.List;
5 import java.util.Map;
6
7 import me.gacl.custom.model.ConditionUser;
8 import me.gacl.domain.User;
9 import me.gacl.util.MyBatisUtil;
10 import org.apache.ibatis.session.SqlSession;
11 import org.junit.Test;
12
13 /**
14 * @author gacl
15 * 測試調用存儲過程
16 */
17 public class Test6 {
18
19 @Test
20 public void testGetUserCount(){
21 SqlSession sqlSession = MyBatisUtil.getSqlSession();
22 /**
23 * 映射sql的標識字元串,
24 * me.gacl.mapping.userMapper是userMapper.xml文件中mapper標簽的namespace屬性的值,
25 * getUserCount是select標簽的id屬性值,通過select標簽的id屬性值就可以找到要執行的SQL
26 */
27 String statement = "me.gacl.mapping.userMapper.getUserCount";//映射sql的標識字元串
28 Map<String, Integer> parameterMap = new HashMap<String, Integer>();
29 parameterMap.put("sexid", 1);
30 parameterMap.put("usercount", -1);
31 sqlSession.selectOne(statement, parameterMap);
32 Integer result = parameterMap.get("usercount");
33 System.out.println(result);
34 sqlSession.close();
35 }
36 }