看了下mybatis源码,有调用存储过程的例子,整理下。
参数形式:
create procedure sptest.adder(in addend1 integer, in addend2 integer, out theSum integer)
begin atomic
set theSum = addend1 + addend2;
end
go
<parameterMap type="map" id="testParameterMap">
<parameter property="addend1" jdbcType="INTEGER" mode="IN"/>
<parameter property="addend2" jdbcType="INTEGER" mode="IN"/>
<parameter property="sum" jdbcType="INTEGER" mode="OUT"/>
</parameterMap>
<update id="adderWithParameterMap" parameterMap="testParameterMap" statementType="CALLABLE">
{call sptest.adder(?, ?, ?)}
</update>
public void testAdderAsUpdateWithParameterMap() {
SqlSession sqlSession = sqlSessionFactory.openSession();
try {
Map<String, Object> parms = new HashMap<String, Object>();
parms.put("addend1", 3);
parms.put("addend2", 4);
SPMapper spMapper = sqlSession.getMapper(SPMapper.class);
spMapper.adderWithParameterMap(parms);
assertEquals(7, parms.get("sum"));
parms = new HashMap<String, Object>();
parms.put("addend1", 2);
parms.put("addend2", 3);
spMapper.adderWithParameterMap(parms);
assertEquals(5, parms.get("sum"));
} finally {
sqlSession.close();
}
带输入输出参数的存储过程:
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
<select id="getNamesAndItems" statementType="CALLABLE"
<select id="getNames" parameterType="java.util.Map" statementType="CALLABLE"
resultMap="nameResult">
{call sptest.getnames(
#{lowestId,jdbcType=INTEGER,mode=IN},
#{totalRows,jdbcType=INTEGER,mode=OUT})}
</select>
</select>
public void testCallWithResultSet2_a1() {
SqlSession sqlSession = sqlSessionFactory.openSession();
try {
SPMapper spMapper = sqlSession.getMapper(SPMapper.class);
Map<String, Object> parms = new HashMap<String, Object>();
parms.put("lowestId", 1);
List<Name> names = spMapper.getNamesAnnotated(parms);
assertEquals(3, names.size());
assertEquals(3, parms.get("totalRows"));
} finally {
sqlSession.close();
}
}
返回多个结果集
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 type="org.apache.ibatis.submitted.sptests.Name" id="nameResult">
<result column="ID" property="id"/>
<result column="FIRST_NAME" property="firstName"/>
<result column="LAST_NAME" property="lastName"/>
</resultMap>
<resultMap type="org.apache.ibatis.submitted.sptests.Item" id="itemResult">
<result column="ID" property="id"/>
<result column="ITEM" property="item"/>
</resultMap>
<select id="getNamesAndItems" statementType="CALLABLE"
resultMap="nameResult,itemResult">
{call sptest.getnamesanditems()}
</select>
@Test
public void testGetNamesAndItems() throws SQLException {
SqlSession sqlSession = sqlSessionFactory.openSession();
try {
SPMapper spMapper = sqlSession.getMapper(SPMapper.class);
List<List<?>> results = spMapper.getNamesAndItems();
assertEquals(2, results.size());
assertEquals(4, results.get(0).size());
assertEquals(3, results.get(1).size());
} finally {
sqlSession.close();
}
}
注意:
上面就是几种常用的了。
1 sqlserver oracle sqlserver返回结果集是可以不要out参数的。如果sql中用的是select出结果,不需要配置out参数。多个结果集/结果集可以配置resultMap 来返回LIST,主要是调用selectList方法会自动把结果集加入到list中去的。
2 sql有返回值 用select标签
3 注意sql参数和mybatis参数的对应关系,这个这里就不讲了。
4 注意参数个数
我遇到的异常:
参数不匹配的原因,因为sqlserver 中我是直接返回select临时表结果,不需要配置存储过程输出参数。
list中的内容形式:
吐槽:
感觉网上的内容都一样,没有区别和针对性,还是要看看源码例子。
还有什么问题大家一起探讨啊。
时间晚了,明早还得上班,有些东西可能没说到,希望大家给点实际开发过程中的问题,给点意见。
- 大小: 32.4 KB
- 大小: 6.4 KB
分享到:
相关推荐
怎么使用MyBatis调用存储过程,步骤详细清楚,一看就会配置
完整的mybatis调用存储过程,解压解压后附加就可使用,注释详细
主要介绍了mybatis调用存储过程的实例,非常不错,具有参考借鉴价值,需要的朋友可以参考下
title: 10.3 MyBatis调用存储过程 10.3.3根据id查询数据返回对象10.3 MyBatis调用存储过程 10.3.3根据id查询数据返回对
在已有的spring+mybatis 基本操作oracle数据库的基础上,增加了3个调用存储过程的例子:无返回值、返回结果集、返回多个结果,希望对大家能有所帮助
MyBatis调用MYSQL存储过程 返回多行 引用的包: asm-3.1.jar cglib-2.2.jar commons-logging-1.1.1.jar log4j-1.2.13.jar mybatis-3.0.4.jar mysql-connector-java-5.1.13.jar slf4j-api-1.5.8.jar slf4j-log4j12-...
mybatis调用orclae存储过程
主要介绍了Mybatis调用Oracle存储过程的方法介绍,需要的朋友可以参考下
能不能写个动态的业务,只输入存储过程名称,自动获取存储过程参数,并且参数的数据从前台传递过来...只根据输入不同的存储过程名称、参数内容,自动调用不同的存储过程。 已经使用在多个项目中 全开源项目 请放心下载
spring mvc + mybatis 调用mysql 存储过程
主要介绍了mybatis调用postgresql自定义函数传递数组参数的解决方案,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
JAVA通过MyBatis调用MySql存储过程和函数doc文档合集整理.zip
mybatis调用mysql中的存储过程方法详解
Java调用存储过程--传入集合参数 具体的方法描述分析
主要介绍了SpringMVC与Mybatis集合实现调用存储过程、事务控制实例,有需要的可以了解一下。
本篇文章主要介绍了Mybatis调用MySQL存储过程的简单实现,具有一定的参考价值,感兴趣的小伙伴们可以参考一下。
springboot继承mybatis后,通过mybatis调用oracle数据库中创建的存储过程,并获取通过游标返回的数据。