mybatis调用Oracle存储过程,并接收返回值(游标类型)

初次学习存储过程调用,写得不是很好,有不对的地方大家多多指教,希望能对您有所帮助。

1.普通返回值:

Oracle存储过程:

/*测试用存储过程*/create or replace procedure p_test(v_slid in varchar2,v_sm out varchar2,v_sm2 out varchar2) authid current_user is
begin
  declare
  v_slid2 bdc_rec.slid%type:=rpad(v_slid,20,' ');
  v_djlx bdc_rec.djlx%type;
  v_lcmc bdc_rec.lcmc%type;
  v_count number:=0;
  begin
    select count(slid) into v_count from bdc_rec where slid=v_slid2;
    if v_count<>0 then
    select djlx,lcmc into v_djlx,v_lcmc from bdc_rec where slid=v_slid2;      
    dbms_output.put_line('受理号:'||v_slid2||',登记类型编号:'||v_djlx||',登记类型名称:'||v_lcmc);
    v_sm:='受理号:'||v_slid2||',登记类型编号:'||v_djlx||',登记类型名称:'||v_lcmc;
    v_sm2:='受理号:'||v_slid2||',登记类型编号:'||v_djlx;
    else
      v_sm:='受理号数据为空.';
      dbms_output.put_line('受理号数据为空.');
    end if;
    end;
    exception when no_data_found then
        dbms_output.put_line('no_data_found');
        when too_many_rows then
          raise_application_error(-20000,'对v_slid2查询时,找到多条数据');
    end p_test;v_sm out varchar2,v_sm2 out varchar2) authid current_user is
begin
  declare
  v_slid2 bdc_rec.slid%type:=rpad(v_slid,20,' ');
  v_djlx bdc_rec.djlx%type;
  v_lcmc bdc_rec.lcmc%type;
  v_count number:=0;
  begin
    select count(slid) into v_count from bdc_rec where slid=v_slid2;
    if v_count<>0 then
    select djlx,lcmc into v_djlx,v_lcmc from bdc_rec where slid=v_slid2;      
    dbms_output.put_line('受理号:'||v_slid2||',登记类型编号:'||v_djlx||',登记类型名称:'||v_lcmc);
    v_sm:='受理号:'||v_slid2||',登记类型编号:'||v_djlx||',登记类型名称:'||v_lcmc;
    v_sm2:='受理号:'||v_slid2||',登记类型编号:'||v_djlx;
    else
      v_sm:='受理号数据为空.';
      dbms_output.put_line('受理号数据为空.');
    end if;
    end;
    exception when no_data_found then
        dbms_output.put_line('no_data_found');
        when too_many_rows then
          raise_application_error(-20000,'对v_slid2查询时,找到多条数据');
    end p_test;

mybatis的mapper文件(statementType="CALLABLE"代表调用存储过程):

<select id="ptest" parameterType="HashMap" statementType="CALLABLE">
		{call P_TEST(#{slid,mode=IN,jdbcType=VARCHAR},#{v_sm,mode=OUT,jdbcType=VARCHAR},#{v_sm2,mode=OUT,jdbcType=VARCHAR})}
</select>		{call P_TEST(#{slid,mode=IN,jdbcType=VARCHAR},#{v_sm,mode=OUT,jdbcType=VARCHAR},#{v_sm2,mode=OUT,jdbcType=VARCHAR})}
</select>
备注:参数v_sm、v_sm2对应service层的map.get("v_sm")、map.get("v_sm2")中的key,可以不用对应存储过程中的v_sm、v_sm2。

service层代码:

public String ptest(String slid){
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("slid", slid);
		basicDataTransferDao.ptest(map);
		System.out.println(map.get("v_sm"));
		System.out.println(map.get("v_sm2"));
		return map.get("v_sm").toString();
	}

2.返回值为集合数据:

Oracle存储过程:

create or replace procedure p_testList (v_sjrq in varchar2,v_cursor out sys_refcursor) is
begin
  open v_cursor for select slid,djlx,lcmc from bdc_rec where to_char(sjrq,'yyyy-MM-dd')=v_sjrq;
end ;

mybatis的mapper文件:

<resultMap type ="java.util.HashMap" id= "cursorMap"> <!--配置返回游标中别名对应的resultMap-->  
     <result column ="slid" property="slid" />  
     <result column ="djlx" property="djlx"  />  
     <result column ="lcmc" property="lcmc" />  
</resultMap >
<select id="ptestSysCursor" parameterType="java.util.HashMap" statementType="CALLABLE">
	{call P_TESTLIST(#{sjrq,mode=IN,jdbcType=VARCHAR},#{result,mode=OUT, jdbcType=CURSOR,javaType=ResultSet,resultMap=cursorMap})}
</select>

service层代码:

public String ptestSysCursor(String sjrq){
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("sjrq", sjrq);
		basicDataTransferDao.ptestSysCursor(map);
		ArrayList<Map<String, Object>> cursorList = (ArrayList<Map<String, Object>>) map.get("result");
		if(cursorList!=null&&cursorList.size()!=0){
			for (Map<String, Object> cursor : cursorList) {
				System.out.println(cursor.get("slid")+"\t"+cursor.get("djlx")+"\t"+cursor.get("lcmc"));
			}
			return cursorList.size()+"";
		}
		return null;
	}

结果如下:

Mybatis调用Oracle存储过程返回多个游标的步骤如下: 1. 定义存储过程Oracle数据库中定义存储过程,该存储过程需要返回多个游标,例如: ``` CREATE OR REPLACE PROCEDURE get_multi_cursor( p_id IN NUMBER, p_cursor1 OUT SYS_REFCURSOR, p_cursor2 OUT SYS_REFCURSOR ) AS BEGIN OPEN p_cursor1 FOR SELECT * FROM table1 WHERE id = p_id; OPEN p_cursor2 FOR SELECT * FROM table2 WHERE id = p_id; END; ``` 2. 创建Java类 创建一个Java类,用于封装存储过程的参数和返回结果,例如: ```java public class MultiCursorResult { private List<Table1> table1List; private List<Table2> table2List; // getter and setter } ``` 3. 定义Mapper接口 在Mybatis的Mapper接口中定义调用存储过程的方法,例如: ```java public interface MultiCursorMapper { void getMultiCursor( @Param("id") Integer id, @Param("cursor1") ResultSet[] cursor1, @Param("cursor2") ResultSet[] cursor2 ); } ``` 4. 编写Mapper XML 在Mapper XML中编写调用存储过程的SQL语句,例如: ```xml <select id="getMultiCursor" statementType="CALLABLE"> {call get_multi_cursor(#{id},#{cursor1,mode=OUT,jdbcType=CURSOR,javaType=ResultSet},#{cursor2,mode=OUT,jdbcType=CURSOR,javaType=ResultSet})} </select> ``` 5. 调用Mapper方法 在Java程序中调用Mapper方法,例如: ```java MultiCursorMapper mapper = sqlSession.getMapper(MultiCursorMapper.class); ResultSet[] cursor1 = new ResultSet[1]; ResultSet[] cursor2 = new ResultSet[1]; mapper.getMultiCursor(1, cursor1, cursor2); List<Table1> table1List = new ArrayList<>(); while (cursor1[0].next()) { Table1 table1 = new Table1(); table1.setId(cursor1[0].getInt("id")); table1.setName(cursor1[0].getString("name")); table1List.add(table1); } List<Table2> table2List = new ArrayList<>(); while (cursor2[0].next()) { Table2 table2 = new Table2(); table2.setId(cursor2[0].getInt("id")); table2.setName(cursor2[0].getString("name")); table2List.add(table2); } MultiCursorResult result = new MultiCursorResult(); result.setTable1List(table1List); result.setTable2List(table2List); ```
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值