1. 程式人生 > >SSM-Mybatis呼叫Oracle儲存過程返回結果集(遊標)示例

SSM-Mybatis呼叫Oracle儲存過程返回結果集(遊標)示例

1.建立一個包

此處建立一個包,是為了建立儲存過程時,用遊標作為out輸出引數時宣告為遊標型別用的.

--建立一個包
create or replace
package types
as 
type empListCursor is ref cursor;
end types;

2.建立儲存過程

用in表示儲存過程的輸入引數,用out表示儲存過程的輸出引數,此處輸出引數為遊標.

CREATE OR REPLACE PROCEDURE 
QUERYEMPSBYDEPTNO(pdeptno in Integer,empList out types.empListCursor) is
BEGIN
if pdeptno=0 then open empList for select * from emp;
else open empList for select * from emp where deptno=pdeptno; end if; END QUERYEMPSBYDEPTNO;

3.EmpMapper.xml

<?xml version="1.0" encoding="UTF-8"?> 
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.casic.dao.EmpMapper"> <resultMap id="resultMap3" type="com.casic.model.Emp"> <result property="empno" column="empno"/> <result property="ename" column="ename"/> <result property="job" column="job"/> <result property="mgr" column="mgr"/> <result
property="hiredate" column="hiredate"/>
<result property="sal" column="sal"/> <result property="comm" column="comm"/> <result property="deptno" column="deptno"/> </resultMap> <!-- 根據部門編號 查詢員工資訊列表 --> <!-- statementType="CALLABLE" :表明呼叫的是儲存過程; parameterType="java.util.Map" :引數是一個map,所以在傳參時需要傳入一個map集合.(我嘗試了其他的型別,比如int,不行!這點跟mysql不一樣); --> <select id="queryEmpByDeptno" statementType="CALLABLE" parameterType="java.util.Map" > <!--傳入傳出引數要註明mode=IN/OUT 並要註明jdbcType(在網上可以查詢mybatis支援哪些jdbcType型別), 返回引數要註明對應的resultMap 注意:這裡pdeptno,result都是引數map集合的key值. --> {call QUERYEMPSBYDEPTNO(#{pdeptno,mode=IN,jdbcType=INTEGER},#{result,jdbcType=CURSOR,mode=OUT,javaType=ResultSet, resultMap=resultMap3})} </select> </mapper>

4.EmpMapper.java

package com.casic.dao;
import java.util.List;
import java.util.Map;
import com.casic.model.Emp;
public interface EmpMapper { 
/* * 根據部門編號載入員工資訊列表 */ 
List<Emp> queryEmpByDeptno(Map<String, Object> param );
}

5.EmpService.java

package com.casic.service;
import java.util.List;
import java.util.Map;
import com.casic.model.Emp;public interface EmpService {  
/* * 根據部門編號,載入員工資訊列表 */ 
List<Emp> queryDeptEmps(Map<String, Object> param);
}

6.EmpServiceImpl.java

package com.casic.service.impl;
import java.util.List;
import java.util.Map;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.casic.dao.EmpMapper;
import com.casic.model.Emp;
import com.casic.service.EmpService;
@Service("empService")
public class EmpServiceImp implements EmpService { 
@Autowired 
private EmpMapper empMapper; 
public List<Emp> queryDeptEmps(Map<String, Object> param) { 
//查詢的過程中,已經將結果集封裝到了param集合中 
empMapper.queryEmpByDeptno(param); 
//根據key獲取到結果集,並進行強轉 
List<Emp> empList=(List<Emp>)param.get("result"); 
return empList; 
}
}

7.EmpController.java

package com.casic.controller;
import java.util.HashMap;
import java.util.List;import java.util.Map;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import com.casic.model.Emp;
import com.casic.service.EmpService;
import oracle.jdbc.driver.OracleTypes;
@Controller
@RequestMapping("/empController")
public class EmpController { 
@Autowired 
private EmpService empService;  
/* * 根據部門編號查詢員工資訊列表 */ 
@RequestMapping("/queryEmp") 
public String showDeptEmps(Emp emp,Model model){
 Map<String, Object> param = new HashMap<String, Object>(); 
//對於in引數賦值 
param.put("pdeptno",emp.getDeptno()); 
//對於out引數 申明 param.put("result",OracleTypes.CURSOR); 
List<Emp> emps = empService.queryDeptEmps(param); 
model.addAttribute("emps", emps); 
return "showEmps"; 
}
}

8.showEmps.jsp

<%@ page language="java" contentType="text/html; charset=utf-8" pageEncoding="utf-8"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<%@ taglib prefix="fmt" uri="http://java.sun.com/jsp/jstl/fmt" %> 
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html> 
<head> 
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1"> <title>emp list</title> 
</head> 
<body> 
<table cellspacing="1" bordercolor="#990000" cellpadding="1" border="1px">
 <tr> 
  <td> 
  <form action="queryEmp" method="get"> 
    <label>部門編號:</label> 
    <select name="deptno"> 
        <option value="0">全部</option> 
        <option value="10">10</option> 
        <option value="20">20</option> 
        <option value="30">30</option> 
        <option value="40">40</option> 
    </select> 
    <input type="submit" value="Research"> 
  </form> 
  </td> 
</tr> 

<tr> 
    <th>序號</th> 
    <th>編號</th> 
    <th>姓名</th> 
    <th>職位</th>
     <th>領導編號</th>
     <th>入職日期</th>
     <th>工資</th> 
    <th>獎金</th> 
    <th>部門編號</th>  
</tr> 
<c:forEach items="${emps}" var="emp" varStatus="vs"> 
    <tr> 
          <td>${vs.count }</td> 
          <td>${emp.empno }</td> 
          <td>${emp.ename }</td> 
          <td>${emp.job }</td> 
          <td>${emp.mgr }</td> 
          <td><fmt:formatDate pattern="yyyy-MM-dd" value="${emp.hiredate}" /></td> 
          <td>${emp.sal }</td> 
          <td>${emp.comm }</td> 
          <td>${emp.deptno }</td> 
    </tr> 
</c:forEach> 
</table> 
</body>
</html>