java中使用ibatis来调用存储过程(下)

3.3 游标循环最佳策略
我们在进行PL/SQL编程时,经常需要循环读取结果集的数据。进行逐行处理,这个过程就需要对游标进行循环。对游标进行循环的方法有多种,我们在此一一分析。

  1. create or replace procedure proccycle(p varchar2)   
  2. as   
  3. cursor c_postype is select pos_type, description from pos_type_tbl where rownum < 6;   
  4. v_postype varchar2(20);   
  5. v_description varchar2(50);   
  6. begin   
  7. open c_postype;   
  8.   if c_postype%found then   
  9.      dbms_output.put_line('found true');   
  10.    elsif c_postype%found = false then   
  11.      dbms_output.put_line('found false');   
  12.   else  
  13.      dbms_output.put_line('found null');   
  14.    end if;   
  15.    loop   
  16.     fetch c_postype into v_postype,v_description ;   
  17.     exit when c_postype%notfound;   
  18.     dbms_output.put_line('postype:'||v_postype||',description:'||v_description);   
  19.    end loop;   
  20.    close c_postype;   
  21. dbms_output.put_line('---loop end---');   
  22.    open c_postype;   
  23.      fetch c_postype into v_postype,v_description;   
  24.     while c_postype%found loop   
  25.        dbms_output.put_line('postype:'||v_postype||',description:'||v_description);   
  26.        fetch c_postype into v_postype,v_description ;   
  27.      end loop;   
  28.   
  29.    close c_postype;   
  30. dbms_output.put_line('---while end---');   
  31.   for v_pos in c_postype loop   
  32.      v_postype := v_pos.pos_type;   
  33.      v_description := v_pos.description;   
  34.      dbms_output.put_line('postype:'||v_postype||',description:'||v_description);   
  35.    end loop;   
  36.    dbms_output.put_line('---for end---');   
  37. end;  
create or replace procedure proccycle(p varchar2)
as 
cursor c_postype is select pos_type, description from pos_type_tbl where rownum < 6;
v_postype varchar2(20);
v_description varchar2(50);
begin
open c_postype;
  if c_postype%found then
    dbms_output.put_line('found true');
  elsif c_postype%found = false then
    dbms_output.put_line('found false');
  else
    dbms_output.put_line('found null');
  end if;
  loop
   fetch c_postype into v_postype,v_description ;
   exit when c_postype%notfound;
   dbms_output.put_line('postype:'||v_postype||',description:'||v_description);
  end loop;
  close c_postype;
dbms_output.put_line('---loop end---');
  open c_postype;
    fetch c_postype into v_postype,v_description;
    while c_postype%found loop
      dbms_output.put_line('postype:'||v_postype||',description:'||v_description);
      fetch c_postype into v_postype,v_description ;
    end loop;

  close c_postype;
dbms_output.put_line('---while end---');
  for v_pos in c_postype loop
    v_postype := v_pos.pos_type;
    v_description := v_pos.description;
    dbms_output.put_line('postype:'||v_postype||',description:'||v_description);
  end loop;
  dbms_output.put_line('---for end---');
end;


使用游标之前需要开打游标,open cursor,循环完后再关闭游标close cursor.
这是使用游标应该慎记于心的法则。
上面的过程演示了游标循环的三种方法。
在讨论循环方法之前,我们先看看%found和%notfound这些游标的属性。

  1. open c_postype;   
  2. if c_postype%found then   
  3.     dbms_output.put_line('found true');   
  4. elsif c_postype%found = false then   
  5.     dbms_output.put_line('found false');   
  6. else  
  7.     dbms_output.put_line('found null');   
  8. end if;  
open c_postype;
  if c_postype%found then
    dbms_output.put_line('found true');
  elsif c_postype%found = false then
    dbms_output.put_line('found false');
  else
    dbms_output.put_line('found null');
  end if;

在打开一个游标之后,马上检查它的%found或%notfound属性,它得到的结果即不是true也不是false.而是null.必须执行一条fetch语句后,这些属性才有值。

第一种使用loop 循环

  1. loop   
  2.     fetch c_postype into v_postype,v_description ;   
  3.     exit when c_postype%notfound;   
  4.     ……   
  5. end loop  
loop
   fetch c_postype into v_postype,v_description ;
   exit when c_postype%notfound;
   ……
end loop

这里需要注意,exit when语句一定要紧跟在fetch之后。必避免多余的数据处理。
处理逻辑需要跟在exit when之后。这一点需要多加小心。
循环结束后要记得关闭游标。

第二种使用while循环。

  1.     fetch c_postype into v_postype,v_description;   
  2. while c_postype%found loop   
  3.     ……   
  4.        fetch c_postype into v_postype,v_description ;   
  5. end loop;  
fetch c_postype into v_postype,v_description;
while c_postype%found loop
   ……
      fetch c_postype into v_postype,v_description ;
end loop;


我们知道了一个游标打开后,必须执行一次fetch语句,游标的属性才会起作用。所以使用while 循环时,就需要在循环之前进行一次fetch动作。
而且数据处理动作必须放在循环体内的fetch方法之前。循环体内的fetch方法要放在最后。否则就会多处理一次。这一点也要非常的小心。
总之,使用while来循环处理游标是最复杂的方法。

第三种 for循环

  1. for v_pos in c_postype loop   
  2.     v_postype := v_pos.pos_type;   
  3.     v_description := v_pos.description;   
  4.     …   
  5. end loop;  
for v_pos in c_postype loop
    v_postype := v_pos.pos_type;
    v_description := v_pos.description;
    …
  end loop;

可见for循环是比较简单实用的方法。
首先,它会自动open和close游标。解决了你忘记打开或关闭游标的烦恼。
其它,自动定义了一个记录类型及声明该类型的变量,并自动fetch数据到这个变量中。
我们需要注意v_pos 这个变量无需要在循环外进行声明,无需要为其指定数据类型。
它应该是一个记录类型,具体的结构是由游标决定的。
这个变量的作用域仅仅是在循环体内。
把v_pos看作一个记录变量就可以了,如果要获得某一个值就像调用记录一样就可以了。
如v_pos.pos_type
由此可见,for循环是用来循环游标的最好方法。高效,简洁,安全。
但遗憾的是,常常见到的却是第一种方法。所以从今之后得改变这个习惯了。

3.4 select into不可乎视的问题
我们知道在pl/sql中要想从数据表中向变量赋值,需要使用select into 子句。
但是它会带动来一些问题,如果查询没有记录时,会抛出no_data_found异常。
如果有多条记录时,会抛出too_many_rows异常。
这个是比较糟糕的。一旦抛出了异常,就会让过程中断。特别是no_data_found这种异常,没有严重到要让程序中断的地步,可以完全交给由程序进行处理。

  1. create or replace procedure procexception(p varchar2)   
  2. as   
  3.    v_postype varchar2(20);   
  4. begin   
  5.     select pos_type into v_postype from pos_type_tbl where 1=0;   
  6.      dbms_output.put_line(v_postype);   
  7. end;   
  8.       
create or replace procedure procexception(p varchar2)
as 
  v_postype varchar2(20);
begin
   select pos_type into v_postype from pos_type_tbl where 1=0;
    dbms_output.put_line(v_postype);
end;

执行这个过程

SQL> exec procexception('a');   
  1. 报错   
  2. ORA-01403: no data found   
  3. ORA-06512: at "LIFEMAN.PROCEXCEPTION", line 6  
  4. ORA-06512: at line 1  
SQL> exec procexception('a');
报错
ORA-01403: no data found
ORA-06512: at "LIFEMAN.PROCEXCEPTION", line 6
ORA-06512: at line 1


处理这个有三个办法
1. 直接加上异常处理。

create or replace procedure procexception(p varchar2)   
  1. as   
  2.    v_postype varchar2(20);   
  3.      
  4. begin   
  5.     select pos_type into v_postype from pos_type_tbl where 1=0;   
  6.      dbms_output.put_line(v_postype);   
  7. exception   
  8.    when no_data_found then   
  9.      dbms_output.put_line('没找到数据');   
  10. end;  
create or replace procedure procexception(p varchar2)
as 
  v_postype varchar2(20);
  
begin
   select pos_type into v_postype from pos_type_tbl where 1=0;
    dbms_output.put_line(v_postype);
exception 
  when no_data_found then
    dbms_output.put_line('没找到数据');
end;

这样做换汤不换药,程序仍然被中断。可能这样不是我们所想要的。
2. select into做为一个独立的块,在这个块中进行异常处理

 

create or replace procedure procexception(p varchar2)   

  1. as   
  2.    v_postype varchar2(20);   
  3.      
  4. begin   
  5.    begin   
  6.     select pos_type into v_postype from pos_type_tbl where 1=0;   
  7.      dbms_output.put_line(v_postype);   
  8. exception   
  9.    when no_data_found then   
  10.      v_postype := '';   
  11.    end;   
  12.    dbms_output.put_line(v_postype);   
  13. end;  
create or replace procedure procexception(p varchar2)
as 
  v_postype varchar2(20);
  
begin
  begin
   select pos_type into v_postype from pos_type_tbl where 1=0;
    dbms_output.put_line(v_postype);
 exception 
  when no_data_found then
    v_postype := '';
  end;
  dbms_output.put_line(v_postype);
end;

这是一种比较好的处理方式了。不会因为这个异常而引起程序中断。
3.使用游标

create or replace procedure procexception(p varchar2)   
  1. as   
  2.    v_postype varchar2(20);   
  3.    cursor c_postype is select pos_type   from pos_type_tbl where 1=0;   
  4. begin   
  5.    open c_postype;   
  6.      fetch c_postype into v_postype;   
  7.    close c_postype;   
  8.    dbms_output.put_line(v_postype);   
  9. end;  
create or replace procedure procexception(p varchar2)
as 
  v_postype varchar2(20);
  cursor c_postype is select pos_type  from pos_type_tbl where 1=0;
begin
  open c_postype;
    fetch c_postype into v_postype;
  close c_postype;
  dbms_output.put_line(v_postype);
end;

这样就完全的避免了no_data_found异常。完全交由程序员来进行控制了。

第二种情况是too_many_rows 异常的问题。
Too_many_rows 这个问题比起no_data_found要复杂一些。
给一个变量赋值时,但是查询结果有多个记录。
处理这种问题也有两种情况:
1. 多条数据是可以接受的,也就是说从结果集中随便取一个值就行。这种情况应该很极端了吧,如果出现这种情况,也说明了程序的严谨性存在问题。
2. 多条数据是不可以被接受的,在这种情况肯定是程序的逻辑出了问题,也说是说原来根本就不会想到它会产生多条记录。
对于第一种情况,就必须采用游标来处理,而对于第二种情况就必须使用内部块来处理,重新抛出异常。
多条数据可以接受,随便取一条,这个跟no_data_found的处理方式一样,使用游标。
我这里仅说第二种情况,不可接受多条数据,但是不要忘了处理no_data_found哦。这就不能使用游标了,必须使用内部块。

  1. create or replace procedure procexception2(p varchar2)   
  2. as   
  3.    v_postype varchar2(20);   
  4.   
  5. begin   
  6.    begin   
  7.      select pos_type into v_postype from pos_type_tbl where rownum < 5;   
  8.    exception   
  9.      when no_data_found then   
  10.        v_postype :=null;   
  11.      when too_many_rows then   
  12.        raise_application_error(-20000,'对v_postype赋值时,找到多条数据');   
  13.    end;   
  14. dbms_output.put_line(v_postype);   
  15. end;  
create or replace procedure procexception2(p varchar2)
as 
  v_postype varchar2(20);
 
begin
  begin
    select pos_type into v_postype from pos_type_tbl where rownum < 5;
  exception
    when no_data_found then
      v_postype :=null;
    when too_many_rows then
      raise_application_error(-20000,'对v_postype赋值时,找到多条数据');
  end;
 dbms_output.put_line(v_postype);
end;

需要注意的是一定要加上对no_data_found的处理,对出现多条记录的情况则继续抛出异常,让上一层来处理。
总之对于select into的语句需要注意这两种情况了。需要妥当处理啊。

 

3.5 在存储过程中返回结果集 我们使用存储过程都是返回值都是单一的,有时我们需要从过程中返回一个集合。即多条数据。这有几种解决方案。比较简单的做法是写临时表,但是这种做法不灵活。而且维护麻烦。我们可以使用嵌套表来实现.没有一个集合类型能够与java的jdbc类型匹配。这就是对象与关系数据库的阻抗吧。数据库的对象并不能够完全转换为编程语言的对象,还必须使用关系数据库的处理方式

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值