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 map = new HashMap();
        map.put("v_cursor", new Dept());
        session.selectOne("TTest.callProcedure02", map);
        System.out.println(map);
    }
    
    
}

 

结果截图如下:

 

 

关于mybatis调用存储过程基本到此, 如果我的博客有不妥, 请各位欢迎评论。

 

你可能感兴趣的:(mybatis学习(九)(调用存储过程(内含游标)))