mybatis 调用存储过程 包含输入输出参数多结果集

存储过程

一、首先定义存储过程,数据库环境mysql

  1. CREATE  PROCEDURE queryTotalNum(OUT totalNum INT , IN tableName varchar(40), IN conditions varchar(300))    
  2.    
  3. begin        
  4.    
  5.     declare stmt varchar(2000);       
  6.    
  7.     declare num int;    
  8.    
  9.     if LENGTH(conditions)>1 then      
  10.    
  11.     begin    
  12.    
  13.         set @sqlstr=concat('SELECT COUNT(*) INTO @num from ',tableName,' where ',conditions);    
  14.    
  15.     end;    
  16.    
  17.     else    
  18.    
  19.     begin    
  20.    
  21.          set @sqlstr=concat('SELECT COUNT(*) INTO @num from ',tableName);    
  22.   
  23.    
  24.     end;    
  25.    
  26.     end if;      
  27.    
  28.     prepare stmt from @sqlstr;        
  29.    
  30.     execute stmt;      
  31.    
  32.     deallocate prepare stmt;    
  33.    
  34.     set totalNum = @num;    
  35.    
  36.     select * from hdx_order_info LIMIT 0, 10 ;  
  37.     select * from hdx_proxy_distributor LIMIT 0, 10 ;  
  38.   
  39.     end;   


二、配置mybatis文件

[html] view plain copy
  1. <!--Map作为存储过程输入输出参数  -->  
  2.   <select id="getNamesAndItems" statementType="CALLABLE" parameterType="Map"  resultMap="HdxOrderInfoMap,TestMap">    
  3.     {call queryTotalNum(#{totalNum,jdbcType=INTEGER,mode=OUT},#{tableName,jdbcType=INTEGER,mode=IN},#{conditions,jdbcType=INTEGER,mode=IN})}    
  4.   </select>   

 

三、DAO类

[java] view plain copy
  1. public interface IHdxOrderInfoDao {  
  2.       
  3.     public List<List<?>> getNamesAndItems(Map<String,Object> map);  
  4.       
  5.       
  6. }  


四、services接口

[java] view plain copy
  1. public interface IHdxOrderInfoService  {  
  2.    
  3.  public List<List<?>> getNamesAndItems(Map<String,Object> map);  
  4. }  

 

五、services实现

[java] view plain copy
  1. public class HdxOrderInfoServiceImpl implements IHdxOrderInfoService{  
  2.     private IHdxOrderInfoDao hdxOrderInfoDao = null;  
  3.   
  4.       
  5.     public IHdxOrderInfoDao getHdxOrderInfoDao() {  
  6.         return hdxOrderInfoDao;  
  7.     }  
  8.   
  9.   
  10.     public void setHdxOrderInfoDao(IHdxOrderInfoDao hdxOrderInfoDao) {  
  11.         this.hdxOrderInfoDao = hdxOrderInfoDao;  
  12.     }  
  13.   
  14.   
  15.     public List<List<?>> getNamesAndItems(Map<String, Object> map) {  
  16.           
  17.         List<List<?>> list = this.hdxOrderInfoDao.getNamesAndItems(map);  
  18.           
  19.                 //totalNum是存储过程中的输出参数  
  20.         System.out.println(map.get("totalNum"));  
  21.           
  22.         //List<HdxOrderInfo> list0 = (List<HdxOrderInfo>)list.get(0);  
  23.         //List<HdxProxyDistributor> list1 = (List<HdxProxyDistributor>)list.get(1);  
  24.           
  25.         return list   
  26.     }  
  27. }  
  • 发表于 2017-11-13 16:51
  • 阅读 ( 1604 )
  • 分类:mysql

你可能感兴趣的文章

相关问题

0 条评论

请先 登录 后评论
不写代码的码农
- Nightmare

33 篇文章

作家榜 »

  1. 威猛的小站长 124 文章
  2. Jonny 65 文章
  3. 江南烟雨 36 文章
  4. - Nightmare 33 文章
  5. doublechina 31 文章
  6. HJ社区-肖峰 29 文章
  7. 伪摄影 22 文章
  8. Alan 14 文章