如何使用MyBatis将Java对象列表传递给Oracle存储过程?

时间:2012-10-04 02:56:14

标签: oracle stored-procedures mybatis

我一直在谷歌搜索一段时间,似乎无法找到任何真正的答案。

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

在pacakge中声明:

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;

Oracle程序:

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>

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

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

2 个答案:

答案 0 :(得分:8)

我无法判断您是否已经这样做,但您需要定义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}
)

答案 1 :(得分:0)

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;