9

我已经在谷歌上搜索了一段时间,似乎找不到任何真正的答案。

我有一个 Oracle 存储过程,它有许多 in 参数,这些参数的类型是表行类型的表。例如:

在包装中声明:

TYPE param1_type_t IS TABLE OF table1%ROWTYPE;
TYPE param2_type_t IS TABLE OF table2%ROWTYPE;
TYPE param3_type_t IS TABLE OF table3%ROWTYPE;

甲骨文程序:

PROCEDURE my_proc
(
   parameter1    IN param1_type_t,
   parameter2    IN param2_type_t,
   parameter3    IN param3_type_t
)

在 Java 方面,我有 3 个对应的对象列表,它们代表 Java 中填充的每个参数。在这种情况下是否可以使用 MyBatis 调用 Oracle 过程?

<update id="callOracleSP" statementType="CALLABLE">
    {CALL my_proc( #{param1, mode=IN},
                   #{param2, mode=IN},
                   #{param3, mode=IN}
                 )
    }
</update>

对象本身是简单的 VO,具有 String 和 Integer 属性以及它们各自的 getter 和 setter。

我不确定如何进行。我是否需要以某种方式将 Java 对象列表映射到 Oracle 类型?

4

2 回答 2

12

我不知道您是否已经这样做了,但是您需要定义 Oracle 对象。

CREATE OR REPLACE TYPE SCHEMA."YOUR_OBJECT" AS OBJECT
(
    field_one    varchar2(50),
    field_two    varchar2(100)
);
/
CREATE OR REPLACE TYPE SCHEMA."YOUR_OBJECT_ARRAY" AS TABLE OF YOUR_OBJECT;
/

然后,您可以编写类型处理程序以将 Java 对象映射到 Oracle 对象。

import oracle.sql.ARRAY;
import oracle.sql.ArrayDescriptor;
import oracle.sql.STRUCT;
import oracle.sql.StructDescriptor;
....
public class YourTypeHandler implements TypeHandler
{
....
    public void setParameter(PreparedStatement ps, int i, Object parameter, JdbcType jdbcType) throws SQLException
    {
        List<YourObject> objects = (List<YourObject>) parameter;

        StructDescriptor structDescriptor = StructDescriptor.createDescriptor("YOUR_OBJECT", ps.getConnection());

        STRUCT[] structs = new STRUCT[objects.size()];
        for (int index = 0; index < objects.size(); index++)
        {
            YourObject pack = packs.get(index);
            Object[] params = new Object[2];
            params[0] = pack.getFieldOne();
            params[1] = pack.getFieldTwo();
            STRUCT struct = new STRUCT(structDescriptor, ps.getConnection(), params);
            structs[index] = struct;
        }

        ArrayDescriptor desc = ArrayDescriptor.createDescriptor("YOUR_OBJECT_ARRAY", ps.getConnection());
        ARRAY oracleArray = new ARRAY(desc, ps.getConnection(), structs);
        ps.setArray(i, oracleArray);
    }
}

然后调用程序,

call your_proc
(
#{yourObjects, javaType=Object, jdbcType=ARRAY, jdbcTypeName=YOUR_OBJECT_ARRAY, mode=IN, typeHandler=YourObjectArrayTypeHandler}
)
于 2012-11-27T04:27:57.507 回答
1

Andy Pryor 的回答非常好,我对其进行了测试,并且确实有效。但它在 typeHandler 有一个错误:

call your_proc
(
#{yourObjects, javaType=Object, jdbcType=ARRAY, jdbcTypeName=YOUR_OBJECT_ARRAY, mode=IN, typeHandler=YourObjectArrayTypeHandler}
)

应该:

call your_proc
(
#{yourObjects, javaType=Object, jdbcType=ARRAY, jdbcTypeName=YOUR_OBJECT_ARRAY, mode=IN, typeHandler=YourTypeHandler}
)

TypeHandler 也有错误:(没有“包”,我的版本中方法参数有一些差异)

@Override
public void setParameter(PreparedStatement ps, int i, Object parameter, String arg3) throws SQLException {
    List<YourObject> objects = (List<YourObject>) parameter;

    StructDescriptor structDescriptor = StructDescriptor.createDescriptor("YOUR_OBJECT", ps.getConnection());

    STRUCT[] structs = new STRUCT[objects.size()];
    for (int index = 0; index < objects.size(); index++)
    {
        YourObject pack = objects.get(index);
        Object[] params = new Object[2];
        params[0] = pack.getFieldOne();
        params[1] = pack.getFieldTwo();
        STRUCT struct = new STRUCT(structDescriptor, ps.getConnection(), params);
        structs[index] = struct;
    }

    ArrayDescriptor desc = ArrayDescriptor.createDescriptor("YOUR_OBJECT_ARRAY", ps.getConnection());
    ARRAY oracleArray = new ARRAY(desc, ps.getConnection(), structs);
    ps.setArray(i, oracleArray);
}

这是 xml 映射的示例:

  <parameterMap id="updateHierPersonAssignMap" class="java.util.Map" >                      
    <parameter property="p_array" jdbcType="ARRAY" javaType="Object" mode="IN" typeHandler="com.aamtech.ria.model.domain.typehandler.YourTypeHandler"/>
  </parameterMap>
  <procedure id="updateHierPersonAssign" parameterMap="updateHierPersonAssignMap" >
    <![CDATA[ 
        { call ria_am_util_pkg.j_update_hier_person_assign( ? ) }
    ]]>
  </procedure>  

以下是从 DAO 调用它的方法:

public void update(List array) {
    Map<String, Object> queryParams = new HashMap<String, Object>();
    queryParams.put("p_array", array);
    try {
        client.update("HashMapResult.updateHierPersonAssign", queryParams);
    } catch (SQLException e) {
    }
}

我的程序看起来像这样(它只是在测试表中插入一行):

Procedure j_update_hier_person_assign (p_array IN YOUR_OBJECT_ARRAY) is
  begin
     FOR i IN 1..p_array.count LOOP
       --dbms_output.put_line();
       insert into test (a) values (p_array(i).field_one);
     END LOOP;
  end;
于 2016-02-19T11:27:05.130 回答