日期:2014-05-16  浏览次数:20409 次

IBatis调用ORACLE的存储过程、函数的返回结果集例子
 
import java.io.Serializable;  
import java.util.Date;  
  
public class User implements Serializable{  
    private static final long serialVersionUID = -6919964218508186044L;  
    private int id;  
    private String name;  
    private Date birthday;  
    public int getId() {  
        return id;  
    }  
    public void setId(int id) {  
        this.id = id;  
    }  
    public String getName() {  
        return name;  
    }  
    public void setName(String name) {  
        this.name = name;  
    }  
    public Date getBirthday() {  
        return birthday;  
    }  
    public void setBirthday(Date birthday) {  
        this.birthday = birthday;  
    }  
}  


<!DOCTYPE mapper PUBLIC "-//ibatis.apache.org//DTD iBatis Mapper 3.0 //EN" "http://ibatis.apache.org/dtd/ibatis-3-mapper.dtd">  
<mapper namespace="org.yhb.ibatis.dao.UserDAO">  
    <!-- 表结构  
        create table IBATIS_USER  
        (  
          ID       NUMBER not null,  
          NAME     VARCHAR2(20) not null,  
          BRITHDAY DATE not null  
        )  
    -->  
      
    <!-- 存储过程  
        create or replace procedure getAllUser(userList out sys_refcursor)  
        as  
        begin  
           open userList for select * from ibatis_user;  
        end;  
     -->  
       
    <!-- resultMap -->  
    <resultMap type="User" id="userMap">  
        <id column="id" property="id" />  
        <result column="name" property="name" />  
        <result column="birthday" property="birthday" />  
    </resultMap>  
  
    <!-- 调用存储过程 -->  
    <select id="getAllUser" statementType="CALLABLE">  
        {call  
          getAllUser(#{userList,<!-- 参数 -->  
                       mode=OUT,<!-- 参数类型 -->  
                       javajavaType=java.sql.ResultSet,<!-- 参数java类型 -->  
                       jdbcType=CURSOR,<!-- 参数jdbc类型 -->  
                       resultMap=userMap<!-- ResultSet需要resultMap参数 -->  
                       })}  
    </select>  
</mapper>  


@Test  
public void testProcedure() throws Exception {  
    Reader reader = null;  
    reader = Resources.getResourceAsReader("configuration.xml");  
    SqlSessionFactory ssf = new SqlSessionFactoryBuilder().build(reader);  
    reader.close();  
      
    SqlSession session = ssf.openSession();  
    Map map = new HashMap();  
    session.selectOne("org.yhb.ibatis.dao.UserDAO.getAllUser", map);  
    System.out.println(map);  
    //返回的集合被放入了map中  
    List<User> userList = (List<User>) map.get("userList");  
    System.out.println(userList);  
    session.close();  
}  



---------------------------------
--定义包声明
create or replace package pkg_stu
as
type list_stu is ref cursor;--定义游标,通过游标将数据模型结果集返回给关系模型
procedure proc_findStuList(stulist out list_stu,s_id number);
function fun_findStuList(s_id number) return list_stu;
end pkg_stu;

--包主体声明
create or replace package body pkg_stu
  as
  procedure proc_findStuList(stulist out list_stu,s_id number)--实现查找所有学生的存储过程
    is
            sqlString varchar2(500);
    begin
            if s_id = 0 then
               open stulist for select sid,sname,major,birth,socre from student order by sid;
            else
               sqlString := 'select sid,sname,major,birth,socre from student where sid=:sid';
               open stulist for sqlString using s_id;
            end if;
    end proc_findStuList;
  function fun_findStuList(s_id number)   --实现查找所有学生的函数
  return list_stu
  is
         stulist list_stu;
         sqlString varchar2(500);
  begin
         if s_id = 0 then
            open stulist for select sid,sname,major,birth,socre from student order by sid;
         else
            sqlString