如何使用 iBatis 将数组写入 Oracle 10g XE 数据库?

发布于 2024-11-03 09:10:38 字数 716 浏览 6 评论 0原文

我一直在寻找这个高低的答案,但找不到答案。

基本上我有一个对象正在使用 iBatis 写入我的数据库。这适用于字符串、整数等基本类型,但我的对象的属性之一是其他对象的数组。我希望能够保留这一点,然后调用“selectById”语句并检索包括数组在内的完整对象。

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

Mapper.xml

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

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

java就像这样:

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

}

提前感谢您的帮助!

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

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

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

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

The java is like so:

  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!!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

我的痛♀有谁懂 2024-11-10 09:10:38

我用Mybatis3做过,应该和旧的iBatis的东西类似。要获取 JDBC 内容,请阅读

基本上,您需要编写一个 TypeHandler。在 TypeHandler 中,调用 setArray。 mybatis 3.x 中应该是这样的。您使用列表时,只需使用 toArray 方法进行转换。这是一个示例,其中参数是 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 中是这样的,

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

让你已经构建了一个类型,就像该线程中所说的那样。

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

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

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".

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

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.

i.e.

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

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文