http://blog.sina.com.cn/s/blog_6f3ca78f01010pmj.htmlhtml
iBatic調用與JAVA調用很相似,只是JAVA把參數的註冊放到了類裏面,而iBatis把參數的註冊放到了XML配置文件裏,下面兩個例子,第一個是返回普通數據,第二個是返回遊標。java
Map map = new HashMap();sql map.put("username", "JACK");oracle sqlMapClient.queryForObject("pro_test",map);函數 System.out.println(map);ui 輸出結果:this {o=NBA, ooo=1, oo=JACK, username=JACK}spa |
<!-- 存儲過程 --> xml <parameterMap class="hashmap" id="pro_map">htm <parameter property="username" javaType="String" jdbcType="VARCHAR" mode="INOUT"/> <parameter property="o" javaType="String" jdbcType="VARCHAR" mode="OUT"/> <parameter property="ooo" javaType="java.lang.Integer" jdbcType="INTEGER"mode="OUT"/> <parameter property="oo" javaType="String" jdbcType="VARCHAR" mode="OUT"/> </parameterMap> <procedure id="pro_test" parameterMap="pro_map"> {call user_account_proc(?,?,?,?)} </procedure> |
create or replace procedure user_account_proc ( uname in out varchar, ugroupname out varchar, uid out number, ugro out varchar ) as begin select groupname,username,userid into ugroupname,ugro,uid from user_account where username = uname; end; |
Map map1 = new HashMap(); sqlMapClient.queryForObject("pro_cursor",map1); System.out.println(map1.get("backcursor")); 輸出結果: [ {userid=1, username=JACK, userpwd=BEIJING, groupname=NBA}, {userid=2, username=TOM, userpwd=SHANGHAI, groupname=NBA}, {userid=3, username=MARY, userpwd=SHANGHAI, groupname=IBM} ] |
配置文件: <resultMap class="hashmap" id="backmap"> <result property="userid" column="USERID"/> <result property="username" column="USERNAME"/> <result property="userpwd" column="USERPWD"/> <result property="groupname" column="GROUPNAME"/> </resultMap> <parameterMap class="hashmap" id="pro_cursor_map"> <parameter property="backcursor" javaType="java.sql.ResultSet" jdbcType="ORACLECURSOR" mode="OUT" resultMap="backmap"/> </parameterMap> <procedure id="pro_cursor" parameterMap="pro_cursor_map"> {call user_account_proc1(?)} </procedure> |
過程: create or replace procedure user_account_proc1 ( my_cursor out sys_refcursor ) as begin open my_cursor for select * from user_account; end; |
1、對於所有是in 類型的參數過程,採用下面的方法調用(ibatis版本是2.3)
好比過程:
Procedure Flashback_Op(v_table_owner Varchar2,v_table_name Varchar2,v_FlashbackScn Number,v_xid Varchar2,v_dbid number);
調用方法:
<parameterMap id="tableDml_recycle" class="java.util.Map">
<parameter property="owner" jdbcType="varchar"
javaType="java.lang.String" mode="IN " />
<parameter property="name" jdbcType="varchar"
javaType="java.lang.String" mode="IN " />
<parameter property="scn" jdbcType="NUMBER"
javaType="java.math.BigDecimal" mode="IN " />
<parameter property="dbId" jdbcType="NUMBER"
javaType="java.math.BigDecimal" mode="IN " />
</parameterMap>
<procedure id="tableDml_zcRecycle" parameterMap="tableDml_recycle">
{call TassetREC.Flashback_Op(?,?,?,?,?)}
</procedure>
2、對於返回類型是遊標 (OUT)的函數,採用以下方式調用。
函數:
Function GetFirstPageNumRows(v_where varchar2,v_dbid Number,v_Numrows varchar2,v_queryid varchar2) return sys_refcursor ;
調用方式:
<parameterMap id="searchParam" class="java.util.Map">
<parameter property="result" jdbcType="ORACLECURSOR"
javaType="java.sql.ResultSet" mode="OUT " resultMap="auditselect_resultList" />
<parameter property="ipAddress"
jdbcType="varchar" javaType="java.lang.String" mode="IN" />
<parameter property="dbId" jdbcType="NUMBER"
javaType="java.math.BigDecimal" mode="IN"/>
<parameter property="countNumber" jdbcType="NUMBER"
javaType="java.lang.Integer" mode="IN"/>
<parameter property="clientId" jdbcType="VARCHAR2"
javaType="java.lang.String" mode="IN"/>
</parameterMap>
<procedure id="firstPage" parameterMap="searchParam">
{? = call tlgadmin.GetFirstPageNumRows(?,?,?,?)} (注意返回值在前面用?表明)
</procedure>
java代碼這麼取值:
@SuppressWarnings("unchecked")
public List getAuditSelectByPages(Map map) {
getSqlMapClientTemplate().queryForList("firstPage", map);//調用
List arryList = (ArrayList) map.get("result");//取值
return arryList;
}
3、對於返回值爲通常數據類型的函數:
函數:
Function DeleteCommand(v_commandid Number,v_commandsetid Number default 0,v_dbid Number default 0) return Number ;
採用以下方式處理(把返回值看成出參來處理OUT):
<parameterMap id="CommandDeleteById" class="java.util.Map">
<parameter property="result" jdbcType="VARCHAR"
javaType="java.lang.String" mode="OUT" />
<parameter property="commandId" jdbcType="NUMBER"
javaType="java.lang.Integer" mode="IN" />
<parameter property="commandsetId" jdbcType="NUMBER"
javaType="java.lang.Integer" mode="IN" />
<parameter property="dbId" jdbcType="NUMBER"
javaType="java.math.BigDecimal" mode="IN" />
</parameterMap>
<procedure id="Command_deleteById"
parameterMap="CommandDeleteById">
{ ? = call truleadmin.DeleteCommand(?,?,?)}
</procedure>
java代碼
this.getSqlMapClientTemplate().delete("Command_deleteById", map); String num = (String) map.get("result"); return num;