We are stuck with mybatis-mapping where we have to fetch list of Oracle Custom Record Types which is returned by a stored procedure.
Can anybody share sample code/mapping if you have any such cases implemented using mybatis.
Package
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;
Package Structure
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;
Mapping
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;
POJO
public class Detail {
String objectName;
String objectStatus;
// Getter and Setter Methods
}
Error --> indentified "P_OBJECT_NAME" not defined in T1.
Can anybody provide me a solution?
解决方案
Change the mapper as follows
{call p1.data_collection_append(#{objList, jdbcType=CURSOR, mode=OUT, resultMap=myResultMap, javaType=java.sql.ResultSet})}
create a type in SpInOut in you domain directory
public class SpInOut {
private Object objList;
//getter setting follows
}
Here is a complete simple example
DB side
Create custom type
PACKAGE KP_EMP_PCK AS
type empType is ref cursor;
END KP_EMP_PCK;
Create a stored procedure
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
Create domain class
public class User {
private String fName;
private String company;
private int age;
private int salary;
//getter Setter
}
Create domain for SpInOut
public class SpInOut {
private Object empList;
//getter setting follows
}
Mapper interface
public interface EmployeeMapper {
void getEmployees(SpInOut param);
}
Mapper xml file
{call kplistemp(#{empList, jdbcType=CURSOR, mode=OUT, resultMap=empResultMap, javaType=java.sql.ResultSet})}
And finally the service class
public class EmployeeServiceImpl implements EmployeeService {
@Autowired
EmployeeMapper mapper;
@Override
public List getEmps() {
try{
SpInOut data= new SpInOut();
mapper.getEmployees(data);
return (List) data.getEmpList();
}catch(Exception e){
e.printStackTrace();
}
return null;
}
}