存储过程在小公司用的不多,但是如果业务比较复杂或者性能要求比较苛刻的时候存储过程就派上用场了,ibatis的前期的一些版本貌似不支持存储过程因此我选择了mybatis来做实验。
1.无输入和输出参数的存储过程,我写了一个比较简单的,需要注意的是Oracle无参存储过程不能写括号
CREATE OR REPLACE Procedure cascadeoperation As Begin Delete From teacher Where id=1; Update studentdetail Set address='宁波市海曙区' Where studentid=10; End;
这里执行了2个操作,可能用过mybatis的人会迷惑执行的时候到底使用update标签呢还是delete标签,其实都行,我也试过select标签也是OK的,下面是部分的配置文件
<delete id="cascadeOperation" statementType="CALLABLE" > {call cascadeoperation} </delete>
2.带有输入和输出参数的存储过程,我这里加入了if else的几个判断
CREATE OR REPLACE Procedure queryTeacher(fid In Integer,Type In Varchar,Name Out Varchar) As Begin If Type='1' then Select Name Into Name From student Where id=fid; Else if Type='2' Then Select Name Into Name From teacher Where id=fid; Else Name:='错误'; End If; End If; End;
下面顺便把我在命令行窗口执行的存储过程语句贴出来
Declare Name Varchar2(50); Begin queryteacher(3,'2',Name); DBMS_OUTPUT.put_line(Name); End; /
执行过类似语句的时候可能看不到任何的输出,不要着急只需在命令行使用set serveroutput on;即可来看下我的执行结果
看到结果了吧,下面使用mybatis来执行这个存储过程,下面是映射文件的写法
<select id="queryTeacher" statementType="CALLABLE" parameterType="java.util.Map"> {call queryTeacher(#{fid,mode=IN,jdbcType=INTEGER},#{type,mode=IN,jdbcType=VARCHAR},#{name,mode=OUT,jdbcType=VARCHAR})} </select>
那怎么取得返回的内容呢,其实只要存储过程执行后map里就有值了,java代码大致如下
Map<String,Object> mm=new HashMap<String,Object>(); mm.put("fid", 3); mm.put("type", 2); m.queryTeacher(mm); System.out.println(mm.get("name"));
下面是控制台输出的结果
3.还有一种存储过程,它可以返回一个游标就类似一个集合这种
CREATE OR REPLACE Procedure getTeacher(cur_arg out Sys_Refcursor) As begin open cur_arg for Select * From teacher; End;
这种情况,在mybatis里就稍微有些不同了,此时jdbcType就是CURSOR,javaType则是ResultSet了,这里还可以把结果转成resultMap了,如下所示
<resultMap id="resultMap3" type="org.lxh.module.usefunction.info.Teacher"> <result property="address" column="address"/> <result property="name" column="name"/> <result property="id" column="id"/> </resultMap>
<select id="getAllTeacherInfo" statementType="CALLABLE" parameterType="java.util.Map" > {call GETTEACHER(#{result,jdbcType=CURSOR,mode=OUT,javaType=ResultSet, resultMap=resultMap3})} </select>
这里的话Java代码就稍微复杂一些
Map<String, Object> map = new HashMap<String, Object>(); m.getAllTeacher(map); Set<Map.Entry<String, Object>> set = map.entrySet(); for (Iterator<Map.Entry<String, Object>> it = set.iterator(); it .hasNext();) { Map.Entry<String, Object> entry = (Map.Entry<String, Object>) it .next(); // System.out.println(entry.getKey() + "--->" + // (Teacher)entry.getValue()); List<Teacher> t = (List<Teacher>) entry.getValue(); Iterator<Teacher> itera = t.iterator(); while (itera.hasNext()) { Teacher tt = itera.next(); System.out.println(tt.getName() + "," + tt.getAddress()); } }
mybatis调用存储过程及返回值获取
存储过程的返回结果直接放在 传入参数的map中(paramCusKey)
调用前{INPARA=CUSTOMER_KEY=545}
调用后
{OUTPARA=CUS_USER_ID=E00000049_02, INPARA=CUSTOMER_KEY=545}
//类似用mybatis内置的插入方法自动返回主键一样,(之前有set主键的话就不返回插入后的值了,直接是set值,)返回存储过程输出参数
<select id="getCusKey" parameterType="map" statementType="CALLABLE" resultType="string">
<![CDATA[
{call PD_OPT_ID(#{INPARA,mode=IN,jdbcType=VARCHAR},#{OUTPARA,mode=OUT,jdbcType=VARCHAR})} //这里是存储过程接收返回结果的写入map中的字段
]]>
</select>
public String getCusKey(Map<String,Object> param);
Map<String,Object> paramCusKey = new HashMap<String,Object>();
paramCusKey.put("INPARA", "CUSTOMER_KEY="+tbCusUserBeanVo.getCustomerKey());
tbCusUserDao.getCusKey(paramCusKey);
String out=paramCusKey.get("OUTPARA").toString().substring(paramCusKey.get("OUTPARA").toString().indexOf("=")+1, paramCusKey.get("OUTPARA").toString().length());//获取结果
System.out.println(out);
System.out.println(paramCusKey.get("OUTPARA").toString());
===================================
//mybatis调用存储过程的另一xml写法
<parameterMap type="java.util.Map" id="recommendMap">
<parameter property="inpara" mode="IN" jdbcType="VARCHAR"/>
<parameter property="outpara" mode="OUT" jdbcType="VARCHAR"/>
</parameterMap>
<select id="recommend" parameterMap="recommendMap" statementType="CALLABLE">
<![CDATA[
call PD_CON_OBJ_RECOMMEND(?,?)
]]>
</select>
@Override
public String recommend(String conobjKey) {
StringBuffer inparasb = new StringBuffer("");
inparasb.append("CONOBJ_KEY=");
inparasb.append(conobjKey);
inparasb.append('\r');
inparasb.append('\n');
inparasb.append("RECOMMEND_NAME=");
inparasb.append("");
String inpara = inparasb.toString();
Map<String,String> map = new HashMap<String,String>();
map.put("inpara", inpara);
tbConObjMapper.recommend(map);
return map.get("outpara");
}
public String recommend(Map<String,String> map);