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

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

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

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


欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/zaji/5093095.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-11-17
下一篇 2022-11-16

发表评论

登录后才能评论

评论列表(0条)

保存