在使用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(); } }
運行結果截圖如下:
好了,基本的mybatis調用存儲過程到此,如果你要返回一個數據集合的話,要使用到存儲過程中的游標,同時mapper配置時還要使用resultType,具體情況請見我下一篇博客,