mybatis learning (ix) (call a stored procedure (containing a cursor))

I believe oracle stored procedure, everyone is familiar with, and sometimes it does need to be some special operations. When a stored procedure to return a large data set, you need to use the cursor during storage. When the results came out, and how to inject these data will mybatis to the corresponding object in it? In mybatis to use to resultMap.

Example: Application of the process output all the information stored in the table dept.

sql statement is as follows:

- creating a package, the creation of a reference cursor type in this package, the type named: t_cursor 

 Create  or  Replace Package test_pack
  AS 
 type t_cursor IS REF Cursor ;
 End test_pack; 




- Create a stored procedure that has an out parameters, the type of the parameter is just created in the package referenced cursor type 
Create  or  Replace  Procedure CU_DEPT_TEST (v_cursor OUT test_pack.t_cursor)
 iS 
the begin 
     Open   v_cursor for  SELECT  *  from Dept;
  End ;

 

Next is the configuration file of mapper, as follows:

 

<!-- 调用储存过程(内含游标) -->
    
    <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>    
    

 

Entity classes as follows:

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

 

Test classes are as follows:

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);
    }
    
    
}

 

Screenshot results as follows:

 

 

About mybatis call a stored procedure basic to this, there is nothing wrong if my blog, please welcome comments.

 

Guess you like

Origin www.cnblogs.com/1998xujinren/p/11229645.html