http://blog.sina.com.cn/s/blog_6f3ca78f01010pmj.html
iBatic调用与JAVA调用很类似,只是JAVA把参数的注册放到了类里面,而iBatis把参数的注册放到了XML配置文件里,下面两个例子,第一个是返回普通数据,第二个是返回游标。
1:返回普通数据
Map map = new HashMap(); map.put("username", "JACK"); sqlMapClient.queryForObject("pro_test",map); System.out.println(map); 输出结果: {o=NBA, ooo=1, oo=JACK, username=JACK} |
<!-- 存储过程 --> <parameterMap class="hashmap" id="pro_map"> <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; |
2:返回一个游标
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; |
另:ibatis调用oracle的函数,存储过程的方法 IN 和OUT /游标
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;