mybatis调用存储过程
2016-08-27 11:21
357 查看
参数形式:
Sql代码
create procedure sptest.adder(in addend1 integer, in addend2 integer, out theSum integer)
begin atomic
set theSum = addend1 + addend2;
end
go
Xml代码
<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>
lt;update id="adderWithParameterMap" parameterMap="testParameterMap" statementType="CALLABLE">
{call sptest.adder(?, ?, ?)}
</update>
Java代码
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代码:
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
Xml代码
<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>
Java代码
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代码:
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
Xml代码
<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>
Java代码
@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();
}
}
Sql代码
create procedure sptest.adder(in addend1 integer, in addend2 integer, out theSum integer)
begin atomic
set theSum = addend1 + addend2;
end
go
Xml代码
<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>
lt;update id="adderWithParameterMap" parameterMap="testParameterMap" statementType="CALLABLE">
{call sptest.adder(?, ?, ?)}
</update>
Java代码
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代码:
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
Xml代码
<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>
Java代码
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代码:
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
Xml代码
<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>
Java代码
@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();
}
}
相关文章推荐
- Mybatis调用视图和存储过程的方法
- MyBatis 中调用存储过程
- mybatis 调用存储过程 返回游标 实例
- mybatis调用存储过程
- MyBatis调用存储过程,含有返回结果集、return参数和output参数
- Mybatis调用存储过程
- mybatis调用存储过程
- MyBatis学习总结(六)——调用存储过程
- MyBatis的学习总结五:调用存储过程【参考】
- mybatis调用存储过程 示例
- MyBatis调用存储过程
- MyBatis学习总结(六)——调用存储过程
- Mybatis调用存储过程
- MyBatis学习总结——调用存储过程
- mybatis 调用存储过程 返回游标 实例
- mybatis调用存储过程(oracle)
- Mybatis调用存储过程
- mybatis调用存储过程
- MyBatis-调用存储过程
- mybatis调用存储过程