天天看點

mybatis學習(九)(調用存儲過程(内含遊标))

相信oracle的存儲過程,大家都不陌生,有時候的确需要它來進行一些特殊的操作。當一個存儲過程要傳回一個大的資料集的時候,就需要遊标在存儲過程中使用。當查詢結果出來後,又如何在mybatis中将這些資料注入到相應的對象中呢?在mybatis中要用到resultMap。

執行個體:運用儲存過程輸出dept表中的所有資訊。

sql語句如下:

--建立一個包,在這個包中建立一種引用遊标類型,該類型名為:t_cursor

 create or replace package test_pack
 as
 type t_cursor is ref cursor;
end test_pack;




--建立一個存儲過程,該過程有一個出參,該參數的類型為剛剛建立包中的引用遊标類型
create or replace procedure CU_DEPT_TEST(v_cursor out test_pack.t_cursor)
is
begin
     open  v_cursor for select * from dept;
 end;      

接下來是mapper檔案的配置,如下:

<!-- 調用儲存過程(内含遊标) -->
    
    <select id="callProcedure02" parameterType="map" statementType="CALLABLE">   
        {call CU_DEPT_TEST(#{v_cursor, mode=OUT, jdbcType=CURSOR, resultMap=CALL_1})}
     </select>
    
    <resultMap type="Dept" id="CALL_1">
        <id column="deptno" property="deptno"/>
        <result column="dname" property="dname"/>
        <result column="loc" property="loc"/>
    </resultMap>    
          

實體類如下:

package com.yc.mybatis;

public class Dept {

    private int deptno;
    private String dname;
    private String loc;
    public int getDeptno() {
        return deptno;
    }
    public void setDeptno(int deptno) {
        this.deptno = deptno;
    }
    public String getDname() {
        return dname;
    }
    public void setDname(String dname) {
        this.dname = dname;
    }
    public String getLoc() {
        return loc;
    }
    public void setLoc(String loc) {
        this.loc = loc;
    }
    @Override
    public String toString() {
        return "Dept [deptno=" + deptno + ", dname=" + dname + ", loc=" + loc + "]";
    }
    @Override
    public int hashCode() {
        final int prime = 31;
        int result = 1;
        result = prime * result + deptno;
        result = prime * result + ((dname == null) ? 0 : dname.hashCode());
        result = prime * result + ((loc == null) ? 0 : loc.hashCode());
        return result;
    }
    @Override
    public boolean equals(Object obj) {
        if (this == obj)
            return true;
        if (obj == null)
            return false;
        if (getClass() != obj.getClass())
            return false;
        Dept other = (Dept) obj;
        if (deptno != other.deptno)
            return false;
        if (dname == null) {
            if (other.dname != null)
                return false;
        } else if (!dname.equals(other.dname))
            return false;
        if (loc == null) {
            if (other.loc != null)
                return false;
        } else if (!loc.equals(other.loc))
            return false;
        return true;
    }
    public Dept(int deptno, String dname, String loc) {
        super();
        this.deptno = deptno;
        this.dname = dname;
        this.loc = loc;
    }
    public Dept() {
        super();
    }
    
    
    
    
}      

Dept.java

測試類如下:

package com.yc.mybatis;

import java.io.IOException;
import java.io.InputStream;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
import org.junit.Test;

public class TestTest01 {
    InputStream is = null;
    SqlSessionFactory factory = null;
    SqlSession session = null;
    {
        try {
            is = Resources.getResourceAsStream("mybatis-config.xml");
            factory = new SqlSessionFactoryBuilder().build(is);
            session = factory.openSession();
        }catch (IOException e) {
            e.printStackTrace();
        }
    }

    @Test
    public void TTest10(){
        Map<String, Dept> map = new HashMap<String, Dept>();
        map.put("v_cursor", new Dept());
        session.selectOne("TTest.callProcedure02", map);
        System.out.println(map);
    }
    
    
}      

結果截圖如下:

mybatis學習(九)(調用存儲過程(内含遊标))

關于mybatis調用存儲過程基本到此, 如果我的部落格有不妥, 請各位歡迎評論。