[英]How to insert an array value to postgreSQL with mybatis
My class我的 class
class PgFolder {
long id;
int[] pgFeature;
}
getter...
setter...
My table我的桌子
CREATE TABLE folder {
ID BIGSERIAL PRIMARY KEY,
feature INT[]
}
My xml我的 xml
<insert id="insertManyFolder" parameterType="java.util.List">
INSERT INTO
folder
(feature)
VALUES
<foreach collection="list" item="item" index="" separator=",">
(#{item.pgFeature})
</foreach>
</insert>
I want to insert a List<PgFolder>
list.我想插入一个List<PgFolder>
列表。 There are many PgFolder instance in the list.列表中有许多 PgFolder 实例。 When I run the program, I got an error:当我运行程序时,我得到一个错误:
org.apache.ibatis.exceptions.PersistenceException: org.apache.ibatis.exceptions.PersistenceException:
Error updating database.更新数据库时出错。 Cause: java.lang.IllegalStateException: Type handler was null on parameter mapping for property '__frch_item_0.pgFeature'.原因:java.lang.IllegalStateException:类型处理程序是 null 属性“__frch_item_0.pgFeature”的参数映射。 It was either not specified and/or could not be found for the javaType (java.util.List): jdbcType (null) combination.它没有指定和/或找不到 javaType (java.util.List): jdbcType (null) 组合。
Cause: java.lang.IllegalStateException: Type handler was null on parameter mapping for property '__frch_item_0.pgFeature'.原因:java.lang.IllegalStateException:类型处理程序是 null 属性“__frch_item_0.pgFeature”的参数映射。 It was either not specified and/or could not be found for the javaType (java.util.List): jdbcType (null) combination.**未指定和/或找不到 javaType (java.util.List): jdbcType (null) 组合。**
Why I got this error message?为什么我收到此错误消息? How to insert the array value in the right way?如何以正确的方式插入数组值? I can insert an array value if I use the sql in postgres client.如果我在 postgres 客户端中使用 sql,我可以插入一个数组值。 so the postgres of the version I installed can support the array value.所以我安装的版本的postgres可以支持数组值。
I'll explain both solutions ie mapping Integer[]
property and mapping int[]
property.我将解释这两种解决方案,即映射Integer[]
属性和映射int[]
属性。
Integer[]
映射Integer[]
class PgFolder {
long id;
Integer[] pgFeature;
// getter / setter
}
In this case, you can use MyBatis' built-in ArrayTypeHandler
.在这种情况下,您可以使用 MyBatis 内置的ArrayTypeHandler
。
Specifying typeHandler
in the parameter reference is sufficient.在参数引用中指定typeHandler
就足够了。
<foreach ...>
(#{item.pgFeature,typeHandler=org.apache.ibatis.type.ArrayTypeHandler})
</foreach>
int[]
映射int[]
As the built-in ArrayTypeHandler
may not work with primitive arrays, you may have to write a custom type handler.由于内置的ArrayTypeHandler
可能不适用于原始 arrays,因此您可能必须编写自定义类型处理程序。 eg例如
package pkg;
import java.sql.Array;
import java.sql.CallableStatement;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.Arrays;
import org.apache.ibatis.type.BaseTypeHandler;
import org.apache.ibatis.type.JdbcType;
public class PgIntArrayTypeHandler extends BaseTypeHandler<int[]>{
@Override
public void setNonNullParameter(PreparedStatement ps, int i, int[] parameter, JdbcType jdbcType)
throws SQLException {
ps.setObject(i, parameter);
}
@Override
public int[] getNullableResult(ResultSet rs, String columnName) throws SQLException {
return extractArray(rs.getArray(columnName));
}
@Override
public int[] getNullableResult(ResultSet rs, int columnIndex) throws SQLException {
return extractArray(rs.getArray(columnIndex));
}
@Override
public int[] getNullableResult(CallableStatement cs, int columnIndex) throws SQLException {
return extractArray(cs.getArray(columnIndex));
}
protected int[] extractArray(Array array) throws SQLException {
if (array == null) {
return null;
}
// pgjdbc returns Integer[] for the INT[] column
Integer[] wrapperArray = (Integer[]) array.getArray();
int[] result = Arrays.stream(wrapperArray).mapToInt(Integer::intValue).toArray();
array.free();
return result;
}
}
Then specify the type handler in the parameter reference.然后在参数引用中指定类型处理程序。
<foreach ...>
(#{item.pgFeature,typeHandler=pkg.PgIntArrayTypeHandler})
</foreach>
Tested with MyBatis 3.5.5 and pgjdbc 42.2.15.使用 MyBatis 3.5.5 和 pgjdbc 42.2.15 测试。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.