1. 程式人生 > >Mybatis 呼叫儲存過程 並返回單/多結果集的情況

Mybatis 呼叫儲存過程 並返回單/多結果集的情況

 返回單結果集的情況:

  • 引用:

https://blog.csdn.net/a9529lty/article/details/24401423

儲存過程示例: 
create or replace procedure Fsp_Plan_CheckPrj(v_grantno  varchar2, v_deptcode number,  v_cursor   out sys_refcursor) is 
……………… 
    ---返回統計結果 
    open v_Cursor for 
      select s.plan_code, 
             s.plan_dept, 
             s.plan_amount, 
             s.exec_amount, 
             p.cname       as plan_name, 
             d.cname       as dept_name 
        from Snap_plan_checkprj s 
        left join v_plan p 
          on s.plan_code = p.plan_code 
        left join org_office d 
          on s.plan_dept = d.off_org_code 
       group by s.plan_code, 
                s.plan_dept, 
                s.plan_amount, 
                s.exec_amount, 
                p.cname, 
                d.cname; 
  end; 
end Fsp_Plan_CheckPrj;  
  •  java層程式碼
Map<String, Object> params = new HashMap<String, Object>();
GrantSetting gs = this. grantSettingDao.get(grantCode);
params.put( "grantNo", StringUtils. substring(gs.getGrantNo(), 0, 2));
params.put( "offOrgCode", SecurityUtils.getPersonOffOrgCode());
 
params.put("v_cursor", new ArrayList<Map<String, Object>>());//傳入一個jdbc遊標,用於接收返回引數
this. batisDao. getSearchList("call_Fsp_Plan_CheckPrj", params);
  •   mybatis xml配置
<resultMap type ="java.util.HashMap" id= "cursorMap"><!--配置返回遊標中別名對應的resultMap -->
     <result column ="plan_code" property="plan_code" />
     <result column ="plan_dept" property="plan_dept"  />
     <result column ="plan_amount" property="plan_amount" />
     <result column ="exec_amount" property="exec_amount"  />
     <result column ="plan_name" property="plan_name" />
     <result column ="dept_name" property="dept_name" />
</resultMap >
<!--註明statementType="CALLABLE"表示呼叫儲存過程-->
<select id ="call_Fsp_Plan_CheckPrj" parameterType= "map" statementType="CALLABLE"  resultMap="cursorMap">
                                                         
     {call Fsp_Plan_CheckPrj(#{grantNo, jdbcType=VARCHAR, mode=IN},
                                   #{offOrgCode, jdbcType=INTEGER, mode=IN},
     #{v_cursor, mode=OUT, jdbcType=CURSOR, resultMap=cursorMap})}

<!--傳入傳出引數要註明mode=IN/OUT 並要註明jdbcType(在網上可以查詢mybatis支援哪些jdbcType型別),返回引數要註明對應的resultMap-->
</select >
  • 最後,得到一個可遍歷的list結果集 ,如service層呼叫
   Map<String,Object> map=new HashMap<String, Object>();
        map.put("id", "22333");
   //取得返回的結果集   
   List<List<?>> results = orderDao.getOrders(map);
   //第一條結果集 總數量
   System.out.println(((List<Integer>)results.get(0)).get(0));
   //第二條訂單列表
   System.out.println((List<OrderForm>)results.get(1));

返回多結果集的情況:

  •  引用:
  • 儲存過程的定義
CREATE  PROCEDURE queryTotalNum(OUT totalNum INT , IN tableName varchar(40), IN conditions varchar(300))  
 
begin      
 
    declare stmt varchar(2000);     
 
    declare num int;  
 
    if LENGTH(conditions)>1 then    
 
    begin  
 
        set @sqlstr=concat('SELECT COUNT(*) INTO @num from ',tableName,' where ',conditions);  
 
    end;  
 
    else  
 
    begin  
 
         set @sqlstr=concat('SELECT COUNT(*) INTO @num from ',tableName);  
 
 
    end;  
 
    end if;    
 
    prepare stmt from @sqlstr;      
 
    execute stmt;    
 
    deallocate prepare stmt;  
 
    set totalNum = @num;  
 
	select * from hdx_order_info LIMIT 0, 10 ;
  	select * from hdx_proxy_distributor LIMIT 0, 10 ;
 
    end; 
  • 配置mybatis檔案
<!--Map作為儲存過程輸入輸出引數	-->
  <select id="getNamesAndItems" statementType="CALLABLE" parameterType="Map"  resultMap="HdxOrderInfoMap,TestMap">  
    {call queryTotalNum(#{totalNum,jdbcType=INTEGER,mode=OUT},#{tableName,jdbcType=INTEGER,mode=IN},#{conditions,jdbcType=INTEGER,mode=IN})}  
  </select> 
  • Mapper Dao介面  
public interface IHdxOrderInfoDao {
	
	public List<List<?>> getNamesAndItems(Map<String,Object> map);
	
	
}

附帶上MyBatis 通過包含的jdbcType型別

引用: