MyBatis Mapping用于获取Oracle中的自定义记录类型列表

时间:2014-11-04 16:03:37

标签: oracle mybatis

我们遇到了mybatis-mapping,我们必须获取存储过程返回的Oracle自定义记录类型列表。

如果您使用mybatis实现了任何此类案例,那么任何人都可以共享示例代码/映射。

封装

create or replace
package p1 as
TYPE T1 IS RECORD
(P_OBJECT_NAME VARCHAR2(80) ,
p_object_status VARCHAR2(25)) ;
TYPE t2 IS TABLE OF t1 index by BINARY_INTEGER;
procedure data_collection_append(t out t2);
procedure data_collection_extract;
end;

包裹结构

create or replace
PACKAGE BODY P1 AS
PROCEDURE DATA_COLLECTION_APPEND(t OUT t2) AS
cursor c1 is select object_name,status from user_objects where rownum = 1;
c c1%rowtype;
i number:=0;
begin
  --t := t2() ;
  open c1;
  loop
    fetch c1 into c;
    EXIT WHEN C1%NOTFOUND;
    dbms_output.put_line(c.object_name||'==>'||c.status);
    T(I).P_OBJECT_NAME:=C.OBJECT_NAME;
    t(i).p_object_status:=c.status;
    i:=i+1;
  end loop; 
end;
procedure data_collection_extract as
t t2;
begin
  data_collection_append(t);
  for i in t.first..t.last
  LOOP
   dbms_output.put_line(t(i).p_object_name||'==>'||t(i).p_object_status);
  end loop; 
end;
END;

映射

    <select id="getLearnerMap" parameterType="Detail" statementType="CALLABLE">
    DECLARE
        v_custom_record p1.T2;

    BEGIN
        v_custom_record := p1.data_collection_append();
        #{objectName, javaType=String, jdbcType=VARCHAR, mode=OUT} := v_custom_record.P_OBJECT_NAME;

    END;
</select>

POJO

 public class Detail {
        String objectName;
        String objectStatus;
         // Getter and Setter Methods
    }

错误 - &gt;已识别的“P_OBJECT_NAME”未在T1中定义。

有人能为我提供解决方案吗?

1 个答案:

答案 0 :(得分:0)

按如下所示更改映射器

<resultMap type="detail" id="myResultMap"  >
        <result property="objectNamee" column="P_OBJECT_NAME" />
        <result property="objectStatus" column="p_object_status" />
</resultMap>

<select id="getLearnerMap" parameterType="spInOut" statementType="CALLABLE">

 {call p1.data_collection_append(#{objList, jdbcType=CURSOR, mode=OUT, resultMap=myResultMap, javaType=java.sql.ResultSet})}

</select>

在您的域目录

中的SpInOut中创建一个类型
public class SpInOut {

    private Object objList;
   //getter setting follows
}

这是一个完整的简单示例

数据库端

创建自定义类型

PACKAGE KP_EMP_PCK AS

  type empType is ref cursor;

END KP_EMP_PCK;

创建存储过程

create or replace PROCEDURE KPLISTEMP
( empList OUT kp_emp_pck.empType
) AS
BEGIN
  open empList for select empid, fname, lname,address from kpemployee order by fname;
END KPLISTEMP;

Java Side


创建域类

public class User {

    private String fName;
    private String company;
    private int age;
    private int salary;
    //getter Setter

}

为SpInOut创建域

public class SpInOut {

        private Object empList;
       //getter setting follows
    }

Mapper界面

public interface EmployeeMapper {

    void getEmployees(SpInOut param);

}

Mapper xml文件

<mapper namespace="com.kp.swasthik.db.oracle.persistence.EmployeeMapper">

    <resultMap type="employee" id="empResultMap"  >
        <id property="empId" column="EMPID" />
        <result property="fName" column="FNAME" />
        <result property="lName" column="LNAME" />
        <result property="address" column="ADDRESS"  />
    </resultMap>

    <select id="getEmployees"  statementType="CALLABLE" parameterType="spInOut" >
        {call kplistemp(#{empList, jdbcType=CURSOR, mode=OUT, resultMap=empResultMap, javaType=java.sql.ResultSet})}
    </select>
</mapper>

最后是服务类

public class EmployeeServiceImpl implements EmployeeService {

    @Autowired
    EmployeeMapper mapper;

    @Override
    public List<Employee> getEmps() {
        try{
            SpInOut data= new SpInOut();
            mapper.getEmployees(data);
            return (List<Employee>) data.getEmpList();
        }catch(Exception e){
            e.printStackTrace();
        }
        return null;
    }

}