Oracle嵌套表作为Mybatis存储过程的输入参数

时间:2017-09-04 12:55:13

标签: java oracle stored-procedures mybatis

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

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

以前是否有人这样做过,或者看过一个例子?

非常感谢。

1 个答案:

答案 0 :(得分:1)

让我们举个例子:

程序程序  (P_VAL_REC IN Package.RECORD  ,P_VAL_NUM IN VARCHAR2  ,P_DAT_VAL OUT DATE  );

在您的数据库中重新定义SP:

PROCEDURE PROCEDURERECORD_NEW  (P_VAL_REC IN RECORD_TYPE(您在类型中创建它)  ,P_VAL_NUM IN VARCHAR2  ,P_DAT_VAL OUT DATE  );

你应该用spring重新配置SP bean:

<?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");
}

}