如何使用iBatis将数组写入Oracle 10g XE数据库? [英] How do you write arrays to an Oracle 10g XE db using iBatis?

查看:137
本文介绍了如何使用iBatis将数组写入Oracle 10g XE数据库?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经找到了这个高低的答案,但无法得到答案。

I have looked for the answer to this high and low but cannot get the answer.

基本上我有一个对象,我正在使用iBatis写入我的数据库。这适用于原始类型,如字符串,int等,但我的对象的一个​​属性是其他对象的数组。我希望能够坚持这个,然后调用'selectById'语句并检索完整的对象,包括数组。

Basically I have an object I am writing to my db using iBatis. This works fine with primitive types like strings, int's etc but one of the attributes of my object is an array of other objects. I would like to be able to persist this and then later call the 'selectById' statement and retrieve the full object including the array.

这是我到目前为止的代码:

Here is the code I have so far:

Mapper.xml

Mapper.xml

  <insert id="insertTrade" parameterClass="TradeObject">
insert into TESTTABLE (
  ORDERID,
  MAXPX,
  COMMISSION,
  ACCOUNTGRP )
values (
  #orderID#, #maxPx#, #commission#, #accountGrp#
)

accountGrp是我的数组,但它目前正在抛出错误。如果没有这个字段,该语句就可以正常工作。

accountGrp is my array but its currently throwing an error. The statement works fine without this field.

java是这样的:

  public static void insertTrade (Trade obj) throws SQLException {
  logger.debug("inserting trade. Order Id: " + obj.toString());
sqlMapper.insert("insertTrade", obj);

}

感谢您提供任何帮助提前!!

Thanks for any help in advance!!

推荐答案

我已经完成了Mybatis3,应该与旧的iBatis相似。要获得JDBC的东西,请阅读这个帖子。这是一个巨大的线索,但它就在那里。寻找ArrayDescriptor。

I've done with Mybatis3, should be similar in the old iBatis stuff. To get the JDBC stuff, read this thread. It's a huge thread, but it is there. Look for "ArrayDescriptor".

基本上,你需要编写一个TypeHandler。在TypeHandler中,调用setArray。在mybatis 3.x中应该是这样的。您使用List,只需使用toArray方法进行转换。这是一个示例,其中参数是String []。

Basicually, you need to write a TypeHandler. In the TypeHandler, call setArray. Should be something like this in mybatis 3.x. Your working with a List, just convert with the toArray method. This is an example, where the parameter is a String[].

import oracle.sql.ARRAY;
import oracle.sql.ArrayDescriptor;   
.....
public void setParameter(PreparedStatement ps, int i, Object parameter, JdbcType jdbcType) throws SQLException
{
 //null check?

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

在ibatis中可能是这样的,

and maybe something like this in ibatis,

public void setParameter(ParameterSetter setter, Object parameter) throws SQLException
{
    ArrayDescriptor desc = ArrayDescriptor.createDescriptor("STRARRAY", setter.getPreparedStatement().getConnection());
    ARRAY oracleArray = new ARRAY(desc, setter.getPreparedStatement().getConnection(), parameter);
    setter.setArray(oracleArray);
}

让你自己构建一个类型,就像在那个帖子中所说的那样。

Make your you've built a type, like it says in that thread.

ie

CREATE OR REPLACE TYPE STRARRAY AS TABLE OF VARCHAR2 (255)

然后在SQL地图中,确保引用类型处理程序。

Then in the SQL map, make sure to reference the type handler.

这篇关于如何使用iBatis将数组写入Oracle 10g XE数据库?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆