Hi All,
I am trying to call a procedure in mybatis, and this procedure has one output parameter of table variable. Detail info is below.
1.Table type and procedure
create type testType as table(id int, name varchar(100));
create procedure testTableOut(in name varchar, out result testType)
language sqlscript
as begin
result=select id,name from testTable
where name like '%'||name||'%';
end;
2.create a domain class with responding to this tableType
public class ItemType
{
private int id;
private String name;
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
3.create a class for prcedure in and out paramter
public class Param {
private String name;
private List<ItemType> items;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public List<ItemType> getItems() {
return items;
}
public void setItems(List<ItemType> items) {
this.items = items;
}
}
4.create type handler "javaHandlername" and its getResult method is below, other methods I just write some System.out.println lines.
public Object getResult(CallableStatement cs, int columnIndex)
throws SQLException {
List<ItemType> items= new ArrayList<ItemType>();
Object[] structArray = (Object[]) cs.getArray(columnIndex).getArray();
Struct mystruct = null;
for (Object structObj : structArray) {
ItemType item= new ItemType();
mystruct = (Struct) structObj;
Object[] structAttr = mystruct.getAttributes();
item.setId((int)structAttr[0]);
item.setName((String)structAttr[1]);
item.setStartTime((Date)structAttr[2]);
item.setEndTime((Date)structAttr[3]);
items.add(item);
}
return items;
}
5.Add method in mapper class
public void test(Param param);
6. Add method in mapper.xml
<select id="test" statementType="CALLABLE" parameterType="Param">
call SYSTEM.testTableOut(#{name, jdbcType=VARCHAR, mode=IN}, #{items, javaType=ItemType, jdbcType=ARRAY, jdbcTypeName=testType , mode=OUT,typeHandler=com.test.javaHandlername})
</select>
6.in service class invoke this method
TestMapper cMapper = sqlSession.getMapper(TestMapper.class);
Param param=new Param();
param.setName("test");
param.setItems(new ArrayList<ItemType>());
cMapper.test(param);
return param.getItems();
But it does not work, just run into error
SEVERE: Servlet.service() for servlet [default] in context with path [/com.sap.cisp.xhna.data.analysis] threw exception [org.apache.ibatis.exceptions.PersistenceException:
### Error querying database. Cause: com.sap.db.jdbc.exceptions.JDBCDriverException: SAP DBTech JDBC: Internal JDBC error: output parameter at index 1 was not expected.
Does anyone has solution in this case? Please give a help.