简体   繁体   English

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

[英]How do you write arrays to an Oracle 10g XE db using iBatis?

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. 基本上我有一个使用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. 这适用于原始类型,如字符串,int等,但我的对象的一个​​属性是其他对象的数组。 I would like to be able to persist this and then later call the 'selectById' statement and retrieve the full object including the array. 我希望能够坚持这个,然后调用'selectById'语句并检索包括数组在内的完整对象。

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 is my array but its currently throwing an error. accountGrp是我的数组,但它当前抛出错误。 The statement works fine without this field. 没有这个字段,该声明工作正常。

The java is like so: 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!! 在此先感谢您的帮助!!

I've done with Mybatis3, should be similar in the old iBatis stuff. 我已经完成了Mybatis3,应该与旧的iBatis相似。 To get the JDBC stuff, read this thread . 要获取JDBC内容,请阅读此主题 It's a huge thread, but it is there. 这是一个巨大的线索,但它就在那里。 Look for "ArrayDescriptor". 寻找“ArrayDescriptor”。

Basicually, you need to write a TypeHandler. 基本上,您需要编写TypeHandler。 In the TypeHandler, call setArray. 在TypeHandler中,调用setArray。 Should be something like this in mybatis 3.x. 在mybatis 3.x中应该是这样的。 Your working with a List, just convert with the toArray method. 您使用List,只需使用toArray方法进行转换。 This is an example, where the parameter is a String[]. 这是一个示例,其中参数是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, 在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)

Then in the SQL map, make sure to reference the type handler. 然后在SQL映射中,确保引用类型处理程序。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 适用于Oracle 10G XE的JDBC驱动程序 - JDBC driver for Oracle 10G XE 如何在不直接使用System.setProperty()的情况下在Oracle 10g JVM中设置系统属性? - How do you set a System property in the Oracle 10g JVM without using System.setProperty() directly? 如何避免两个不同的线程从 DB 读取相同的行(Hibernate 和 Oracle 10g) - How to avoid two different threads read the same rows from DB (Hibernate and Oracle 10g) 如何从oracle_database 获取序列的nextvalue 并使用oracle 10g 在netbeans 的JOptionPane 中显示它? - how to get nextvalue of sequence from oracle_database and show it in JOptionPane in netbeans using oracle 10g? 无法连接 Tomcat 6 和 oracle 10g XE for jsp 数据库连接 - Can't connect Tomcat 6 and oracle 10g XE for jsp database connectivity 如何在Play框架中执行查询(使用Oracle 10g) - How do I execute queries in Play framework (with Oracle 10g) 如何使用JDBC为Oracle 10g设置字符编码 - How do I set character encoding for Oracle 10g with JDBC 使用Java swing与Oracle 10g创建JDBC连接 - creating JDBC connection with Oracle 10g using java swing 在Oracle 10g中使用Hibernate将数据保存到Clob中 - Saving data into Clob using Hibernate in Oracle 10g JdbcTemplate在Springboot应用中是null使用Oracle 10g - JdbcTemplate is null in Springboot application using Oracle 10g
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM