..。有没有一种简单的方法可以在iBatis的帮助下将Java int[]插入到PostgreSql中?(旧的,而不是新的MyBatis)
我不确定是否需要自定义类型处理程序,但我很难找到一个可以说明所发生的事情的代码示例。
提前谢谢。
ps:
由于最初的发布,我能够从DB读取数组并填充域对象中的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]; -错误发生在project/persistance/sql_xml/Article.xml中。
-应用参数映射时出错。
-检查update文章-InlineParameterMap。
-检查'crap‘属性的参数映射。
- java.lang.NullPointerException;嵌套异常为com.ibatis.common.jdbc.exception.NestedSQLException:
-错误发生在project/persistance/sql_xml/Article.xml中。
-应用参数映射时出错。
-检查update文章-InlineParameterMap。
-检查'crap‘属性的参数映射。
-原因: 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/persistance/sql_xml/Article.xml中。
-应用参数映射时出错。
-检查update文章-InlineParameterMap。
-检查'crap‘属性的参数映射。
-原因: java.lang.ClassCastException: java.util.ArrayList;嵌套异常为com.ibatis.common.jdbc.exception.NestedSQLException:
-错误发生在project/persistance/sql_xml/Article.xml中。
-应用参数映射时出错。
-检查update文章-InlineParameterMap。
-检查'crap‘属性的参数映射。
-原因: java.lang.ClassCastException: java.util.ArrayList
..。不过,我今天已经吃完了。谢谢
发布于 2011-01-18 03:50:45
根据Jeremy的回答所引用的页面,不久前我已经编写了我自己的处理程序(使用了一些hack)。如果你觉得它有用:
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");
}
}发布于 2011-01-15 03:34:08
这看起来很有希望:
http://beerholder.blogspot.com/2007/10/mapping-postgresql-arrays-with-ibatis-i.html
发布于 2011-01-20 00:34:59
..。终于明白了。下面是它是如何从一开始的:
..。第一个: reading the int[]
..。第二:第二,在搜索和跌跌撞撞的过程中,发现了一个implementation of the java.sql.Array interface (尽管只有JDK1.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邮件列表时,我得到了以下结果:
这几个问题...
http://code.google.com/p/mybatis/source/detail?r=3467
对于我们在MyBatis 3中处理原始数组所做的更改。
在JDBC中对数组类型的支持完全是一团糟,将其与原始数组混合增加了另一层混乱:)
https://stackoverflow.com/questions/4694959
复制相似问题