2017-09-04 175 views
0

我需要使用Mybatis调用具有嵌套表类型的输入参数的Oracle存储过程。Oracle嵌套表作为Mybatis存储过程的输入参数

我找不到有关MyBatis的这种特定用法的任何文档或示例。

有没有人做过这个,或看过一个例子?

非常感谢。

+0

从Java调用存储过程时,不支持Pl/SQL类型(例如表,记录等)。在做这件事情时最好的办法是创建一个新的包装程序,它只使用SQL类型作为输入(或输出)。你的Java代码将调用这个包装器过程,这个过程又将调用你原来的Oracle过程(它将不得不准备参数来匹配原始过程)。 –

+0

@dsp_user这不正确 - 您可以将PL/SQL集合类型传递给Java中的存储过程和语句[[1](https://stackoverflow.com/a/42697156/1509264),[2](https:// stackoverflow.com/a/37161584/1509264),[3](https://stackoverflow.com/a/34699771/1509264)]。 – MT0

+0

我想他是专门讨论MyBatis,而不是Java。 – Andres

回答

1

让我们这个例子:

程序PROCEDURERECORD (P_VAL_REC IN Package.RECORD ,P_VAL_NUM IN VARCHAR2 ,P_DAT_VAL过时 );

在你的数据库重新定义SP:

程序PROCEDURERECORD_NEW (P_VAL_REC IN RECORD_TYPE(你的类型创建它) ,P_VAL_NUM IN VARCHAR2 ,P_DAT_VAL过时 );

你应该重新配置你SP豆与春天:

<?xml version="1.0" encoding="UTF-8" standalone="no"?> 
<beans xmlns="http://www.springframework.org/schema/beans" 
xmlns:p="http://www.springframework.org/schema/p" xmlns:util="http://www.springframework.org/schema/util" 
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
xsi:schemaLocation="http://www.springframework.org/schema/beans       http://www.springframework.org/schema/beans/spring-beans-3.0.xsd       http://www.springframework.org/schema/util        http://www.springframework.org/schema/util/spring-util.xsd"> 

<bean id="PROCEDURERECORD_NEW" parent="storedProcedure"> 
    <constructor-arg index="0" value="PROCEDURERECORD" /> 
    <constructor-arg index="1" value="false" /> 
    <property name="params"> 
     <list> 
      <bean parent="sqlRecordParamIn"> 
       <constructor-arg index="0" value="P_VAL_REC" /> 
       <constructor-arg index="2" value="RECORD_TYPE" /> 
      </bean> 
      <bean parent="sqlNumericParamIn"> 
       <constructor-arg value="P_VAL_NUM" /> 
      </bean> 
      <bean parent="sqlDateParamOut"> 
       <constructor-arg value="P_DAT_VAL"/> 
      </bean> 
     </list> 
    </property> 
</bean> 

在您的实现代码,使用SqlStructValue这样的:

@Override 
public DateTime getSpReturn(RecordClass record,Long valNum){ 
    Map<String, Object> args = new HashMap<String, Object>(); 
    args.put("P_VAL_REC", new SqlStructValue<RecordClass>(record,new RecordClassMapper())); 
    args.put("P_VAL_NUM", valNum); 


    Map<String, Object> result = procedureRecordNew.execute(args); 
    return (DateTime)result.get("P_DAT_VAL"); 
} 

至于映射器创建它像这样:

@Component("RecordClassMapper") 
public class RecordClassMapper implements StructMapper<RecordClass> { 

@Override 
public STRUCT toStruct(RecordClass source, Connection conn, String typeName) throws SQLException { 
    Object[] objectProperties = new Object[] { new source.getrecordatr1(), source.getrecordatr2(), source.getrecordatr3() }; 
    return new STRUCT(new StructDescriptor(typeName, conn), conn, objectProperties); 
} 

@Override 
public RecordClass fromStruct(STRUCT struct) throws SQLException { 
    // Auto-generated method stub 
    throw new UnsupportedOperationException("Not implemented"); 
} 

}

+0

加上一个例子。 ..但我需要的是Mybatis,而不是Spring :) – Andres

+0

据我所知,如果你使用java工作,你不能在mybatis中调用这样的SP,我认为用spring解决它会很简单。 (我们的项目中存在同样的问题)。 –

+0

我们称其他SP。你确定你不能调用具有plsql表作为参数的sps吗?它写在什么地方? – Andres

相关问题