mybatis学习(八)(mybatis调用oracle中的存储过程)

在使用mybatis时,有时候必须要调用数据库中写好的存储过程来进行对数据的操作.这里就简单介绍一下mybatis中调用存储过程的具体实现;

实例:使用存储过程来根据deptno查询信息

sql语句的存储过程如下:

 --根据部门编号查询部门信息
 create or replace procedure pro_test5(dno in out dept.deptno%type, dname1 out dept.dname%type, loc1 out dept.loc%type)
 as
 begin
    select deptno, dname, loc into dno, dname1, loc1 from dept where dno = deptno;
 end;

mapper文件的配置如下:

<!-- 调用存储过程 -->
    <select id="callProcedure01" parameterType="Dept" statementType="CALLABLE">
<!-- mode是存储过程的类型,分为in、out、in out;三种类型分别对应传入参数, 不传出参数;传出参数,不传入参数;即传入参数,又传出参数。 jdbcType为数据的类型
每一个传入的参数都必须要这样写-->
{call pro_test5(#{deptno, mode=INOUT, jdbcType = INTEGER},#{dname, mode=OUT, jdbcType=VARCHAR}, #{loc, mode=OUT, jdbcType=VARCHAR} )} </select>

测试类测试如下:

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 TTest09(){
        Dept dept = new Dept();
        dept.setDeptno(20);
        session.selectOne("TTest.callProcedure01", dept);//注意:这里会自动向对象里面注入值,因为在存储过程中定义好了的。
        System.out.println(dept);
            
    }
}

实体类如下:

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

运行结果截图如下:

mybatis学习(八)(mybatis调用oracle中的存储过程)

好了,基本的mybatis调用存储过程到此,如果你要返回一个数据集合的话,要使用到存储过程中的游标,同时mapper配置时还要使用resultType,具体情况请见我下一篇博客,