2017-09-04 174 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

相關問題