2011-01-14 20 views
2

...有沒有簡單的方法在iBatis的幫助下將Java int []插入到PostgreSql中? (舊的,而不是新的MyBatis)在iBatis的幫助下將int []插入到PostgreSql中

不知道我是否需要自定義類型處理程序,但是我很難找到代碼示例來說明發生了什麼。

在此先感謝。

PS:

因爲原來的張貼,我能夠讀取數據庫的陣列和填充INT []域中的對象。但不能寫入到數據庫但:-(

所以在域模型有:

int[] crap = null; 

與getter和setter,cusom財產的處理程序是這樣的:

public class ArrayTypeHandler implements TypeHandlerCallback { 
public void setParameter(ParameterSetter setter, Object parameter) throws SQLException { 

    if(parameter == null){ 
     setter.setNull(Types.ARRAY); 
    } else { 
     setter.setArray((Array) Arrays.asList(parameter)); 
    } 

} 

public Object getResult(ResultGetter getter) throws SQLException { 
    Array array = getter.getResultSet().getArray(getter.getColumnName()); 
    if(!getter.getResultSet().wasNull()){ 
     return array.getArray(); 
    } else { return null; } 

} 

public Object valueOf(String string) { 
    throw new UnsupportedOperationException("Not supported yet."); 
} 

}

sqlMapConfig.xml:

<typeHandler javaType="java.sql.Array" jdbcType="ARRAY" callback="project.persistance.sqlmapdao.ArrayTypeHandler" /> 

當試圖更新我得到以下錯誤:

org.springframework.web.util.NestedServletException: Request processing failed; nested exception is org.springframework.jdbc.UncategorizedSQLException: SqlMapClient operation; uncategorized SQLException for SQL []; SQL state [null]; error code [0]; 

---項目/持久性/ sql_xml/Article.xml發生錯誤。
---應用參數映射時發生錯誤。
---檢查updateArticle-InlineParameterMap。
---檢查'廢話'屬性的參數映射。
---原因:java.lang.NullPointerException;嵌套的異常是com.ibatis.common.jdbc.exception.NestedSQLException:
---該錯誤發生在project/persistance/sql_xml/Article.xml中。
---應用參數映射時發生錯誤。
---檢查updateArticle-InlineParameterMap。
---檢查'廢話'屬性的參數映射。
---原因:java.lang.NullPointerException

...有什麼提示我缺少什麼? 感謝

===

...我的工作方式到ClassCastExceptiong :-)

嘗試設置屬性格式:

public void setParameter(ParameterSetter setter, Object parameter) throws SQLException { 
    int[] c = (int[]) parameter; 

    setter.setArray((java.sql.Array) c ); 
} 

...以及隨之而來的異常:

org.springframework.web.util.NestedServletException: Request processing failed; nested exception is org.springframework.jdbc.UncategorizedSQLException: SqlMapClient operation; uncategorized SQLException for SQL []; SQL state [null]; error code [0]; 

---發生錯誤在project/persistanc E/sql_xml/Article.xml。
---應用參數映射時發生錯誤。
---檢查updateArticle-InlineParameterMap。
---檢查'廢話'屬性的參數映射。
---原因:java.lang.ClassCastException:java.util.ArrayList;嵌套的異常是com.ibatis.common.jdbc.exception.NestedSQLException:
---該錯誤發生在project/persistance/sql_xml/Article.xml中。
---應用參數映射時發生錯誤。
---檢查updateArticle-InlineParameterMap。
---檢查'廢話'屬性的參數映射。
---原因:java.lang.ClassCastException:java.util.ArrayList

......我今天過了。 感謝

+2

你想插入`一行INT []`的一個整體的其他層或多行? – Nishant 2011-01-14 19:24:51

+0

一行,col類型是整數[] – vector 2011-01-14 19:33:00

回答

1

基於由Jeremy的回答中引用的頁面上,我已經編碼我自己的處理程序(有一些黑客)前一陣子。如果你覺得它有用:

public class ArrayIntsTypeHandlerCallback implements TypeHandlerCallback { 

    /** 
     * to write an integer array in db. Object should be Integer[] 
     */  
     public void setParameter(ParameterSetter setter, Object parameter) throws SQLException { 
      Connection con = setter.getPreparedStatement().getConnection(); 
      // hack: if using poolable connection from dbcp must get inside true connection! 
      if(con instanceof org.apache.commons.dbcp.PoolableConnection) { 
       con =  ((org.apache.commons.dbcp.PoolableConnection)con).getInnermostDelegate(); 
      } 
      Array array = con.createArrayOf("integer", (Object[])parameter); 
      setter.setArray(array); 
     } 

     /** 
     * read integer array from db. returns Integer[] 
     */ 
     public Object getResult(ResultGetter getter) throws SQLException { 
     Array array = getter.getArray(); 
     if (!getter.getResultSet().wasNull()) { 
      return array.getArray(); 
     } else { 
      return null; 
     } 
     } 

     public Object valueOf(String s) { 
     throw new UnsupportedOperationException("Not implemented"); 
     } 


    } 
1

......終於搞定了。這是它從一開始就如何去:

...第一: reading the int[]

...第二:第二,而搜索和絆腳石周圍發現了一個implementation of the java.sql.Array interface(JDK 1.6只雖然)和張貼在mailing list from 2005

最終落實在TypeHandlerCallbac iterface中的setParameter方法:

public void setParameter(ParameterSetter setter, Object parameter) throws SQLException { 
    setter.setArray(this.convertIntegerToPgSqlArray((int[]) parameter)); 
} 

... 

    private java.sql.Array convertIntegerToPgSqlArray(final int[] p) { 
    if (p == null || p.length < 1) { 
     return null; 
    } 
    Array a = new Array() { 

     public String getBaseTypeName() { 
      return "int4"; 
     } 

     public int getBaseType() { 
      return 0; 
     } 

     public Object getArray() { 
      return null; 
     } 

     public Object getArray(Map<String, Class<?>> map) { 
      return null; 
     } 

     public Object getArray(long index, int count) { 
      return null; 
     } 

     public Object getArray(long index, int count, Map<String, Class<?>> map) { 
      return null; 
     } 

     public ResultSet getResultSet() { 
      return null; 
     } 

     public ResultSet getResultSet(Map<String, Class<?>> map) { 
      return null; 
     } 

     public ResultSet getResultSet(long index, int count) { 
      return null; 
     } 

     public ResultSet getResultSet(long index, int count, 
      Map<String, Class<?>> map) { 
      return null; 
     } 

     public String toString() { 
      String fp = "{"; 
      if (p.length == 0) { 
      } else { 
       for (int i = 0; i < p.length - 1; i++) { 
        fp += p[i] + ","; 
       } 
       fp += p[p.length - 1]; 
      } 
      fp += "}"; 
      return fp; 
     } 
    }; 
     return a; 
} 

在最後感謝大家,希望這一段時間節省別人:-)

PS:正如最後僅供參考,當我張貼的問題MyBatis的郵件列表,這裏就是我回來:

Several problems with this...

  1. Arrays.asList()不適用於基元數組。在這裏看到:

    http://code.google.com/p/mybatis/source/detail?r=3467

    對於我們的MyBatis 3做處理原數組的變化。

  2. 然後,您可能無法將java.util.List轉換爲java.sql.Array。 要生成java.sql.Array,您需要使用JDBC驅動程序的一些實用程序,或者切換到JDK6並使用 Connection.createArrayOf(...)方法。

支持JDBC中的數組類型是一團糟,與 基本數組混合它增加了混亂:)