当前位置:首页 » 服务存储 » 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 }