mybatis调用存储过程

来源:互联网 发布:aide手机编程 编辑:程序博客网 时间:2024/04/25 16:01

引用:http://lohasle.iteye.com/blog/1669879

看了下mybatis源码,有调用存储过程的例子,整理下。 

参数形式: 

Sql代码  收藏代码
  1. create procedure sptest.adder(in addend1 integerin addend2 integerout theSum integer)  
  2. begin atomic  
  3.   set theSum = addend1 + addend2;   
  4. end  
  5. go  


Xml代码  收藏代码
  1. <parameterMap type="map" id="testParameterMap">  
  2.    <parameter property="addend1" jdbcType="INTEGER" mode="IN"/>  
  3.    <parameter property="addend2" jdbcType="INTEGER" mode="IN"/>  
  4.    <parameter property="sum" jdbcType="INTEGER" mode="OUT"/>  
  5.  </parameterMap>  
  6. lt;update id="adderWithParameterMap" parameterMap="testParameterMap" statementType="CALLABLE">  
  7.    {call sptest.adder(?, ?, ?)}  
  8.  </update>  

Java代码  收藏代码
  1. public void testAdderAsUpdateWithParameterMap() {  
  2.        SqlSession sqlSession = sqlSessionFactory.openSession();  
  3.        try {  
  4.            Map<String, Object> parms = new HashMap<String, Object>();  
  5.            parms.put("addend1"3);  
  6.            parms.put("addend2"4);  
  7.              
  8.            SPMapper spMapper = sqlSession.getMapper(SPMapper.class);  
  9.              
  10.            spMapper.adderWithParameterMap(parms);  
  11.            assertEquals(7, parms.get("sum"));  
  12.              
  13.            parms = new HashMap<String, Object>();  
  14.            parms.put("addend1"2);  
  15.            parms.put("addend2"3);  
  16.            spMapper.adderWithParameterMap(parms);  
  17.            assertEquals(5, parms.get("sum"));  
  18.              
  19.        } finally {  
  20.            sqlSession.close();  
  21.        }  


带输入输出参数的存储过程: 
sql代码:
Sql代码  收藏代码
  1. create procedure sptest.getnames(in lowestId intout totalrows integer)  
  2. reads sql data  
  3. dynamic result sets 1  
  4. BEGIN ATOMIC  
  5.   declare cur cursor for select * from sptest.names where id >= lowestId;  
  6.   select count(*) into totalrows from sptest.names where id >= lowestId;  
  7.   open cur;  
  8. END  
  9. go  



Xml代码  收藏代码
  1. <select id="getNamesAndItems" statementType="CALLABLE"  
  2.     <select id="getNames" parameterType="java.util.Map" statementType="CALLABLE"  
  3.   resultMap="nameResult">  
  4.   {call sptest.getnames(  
  5.     #{lowestId,jdbcType=INTEGER,mode=IN},  
  6.     #{totalRows,jdbcType=INTEGER,mode=OUT})}  
  7. </select>  
  8. </select>  


Java代码  收藏代码
  1. public void testCallWithResultSet2_a1() {  
  2.        SqlSession sqlSession = sqlSessionFactory.openSession();  
  3.        try {  
  4.            SPMapper spMapper = sqlSession.getMapper(SPMapper.class);  
  5.              
  6.            Map<String, Object> parms = new HashMap<String, Object>();  
  7.            parms.put("lowestId"1);  
  8.            List<Name> names = spMapper.getNamesAnnotated(parms);  
  9.            assertEquals(3, names.size());  
  10.            assertEquals(3, parms.get("totalRows"));  
  11.        } finally {  
  12.            sqlSession.close();  
  13.        }  
  14.    }  



返回多个结果集 
sql代码:
Sql代码  收藏代码
  1. create procedure sptest.getnamesanditems()  
  2. reads sql data  
  3. dynamic result sets 2  
  4. BEGIN ATOMIC  
  5.   declare cur1 cursor for select * from sptest.names;  
  6.   declare cur2 cursor for select * from sptest.items;  
  7.   open cur1;  
  8.   open cur2;  
  9. END  
  10. go  



Xml代码  收藏代码
  1. <resultMap type="org.apache.ibatis.submitted.sptests.Name" id="nameResult">  
  2.     <result column="ID" property="id"/>  
  3.     <result column="FIRST_NAME" property="firstName"/>  
  4.     <result column="LAST_NAME" property="lastName"/>  
  5.   </resultMap>  
  6.   
  7.   <resultMap type="org.apache.ibatis.submitted.sptests.Item" id="itemResult">  
  8.     <result column="ID" property="id"/>  
  9.     <result column="ITEM" property="item"/>  
  10.   </resultMap>  
  11.   
  12.   <select id="getNamesAndItems" statementType="CALLABLE"  
  13.     resultMap="nameResult,itemResult">  
  14.     {call sptest.getnamesanditems()}  
  15.   </select>  


Java代码  收藏代码
  1. @Test  
  2.     public void testGetNamesAndItems() throws SQLException {  
  3.         SqlSession sqlSession = sqlSessionFactory.openSession();  
  4.         try {  
  5.             SPMapper spMapper = sqlSession.getMapper(SPMapper.class);  
  6.               
  7.             List<List<?>> results = spMapper.getNamesAndItems();  
  8.             assertEquals(2, results.size());  
  9.             assertEquals(4, results.get(0).size());  
  10.             assertEquals(3, results.get(1).size());  
  11.         } finally {  
  12.             sqlSession.close();  
  13.         }  
  14.     }  

注意: 

上面就是几种常用的了。 
1 sqlserver oracle sqlserver返回结果集是可以不要out参数的。如果sql中用的是select出结果,不需要配置out参数。多个结果集/结果集可以配置resultMap 来返回LIST,主要是调用selectList方法会自动把结果集加入到list中去的。 
2 sql有返回值 用select标签 
3 注意sql参数和mybatis参数的对应关系,这个这里就不讲了。 
4 注意参数个数 

我遇到的异常: 

参数不匹配的原因,因为sqlserver 中我是直接返回select临时表结果,不需要配置存储过程输出参数。 
list中的内容形式: 
 

吐槽: 
感觉网上的内容都一样,没有区别和针对性,还是要看看源码例子。 
还有什么问题大家一起探讨啊。 
时间晚了,明早还得上班,有些东西可能没说到,希望大家给点实际开发过程中的问题,给点意见。 

0 0