首先自定义一个数组处理器
package com.ctsy.l3a1.handler; import org.apache.ibatis.type.BaseTypeHandler; import org.apache.ibatis.type.JdbcType; import org.apache.ibatis.type.TypeException; import java.sql.*; /** * @do 数组类型处理器 */ public class ArrayTypeHandler extends BaseTypeHandler<Object[]> { private static final String TYPE_NAME_VARCHAR = "varchar"; private static final String TYPE_NAME_INTEGER = "integer"; private static final String TYPE_NAME_BOOLEAN = "boolean"; private static final String TYPE_NAME_NUMERIC = "numeric"; private static final String TYPE_NAME_LONG = "int8"; @Override public void setNonNullParameter(PreparedStatement ps, int i, Object[] parameter, JdbcType jdbcType) throws SQLException { String typeName = null; if (parameter instanceof Integer[]) { typeName = TYPE_NAME_INTEGER; } else if (parameter instanceof String[]) { typeName = TYPE_NAME_VARCHAR; } else if (parameter instanceof Boolean[]) { typeName = TYPE_NAME_BOOLEAN; } else if (parameter instanceof Double[]) { typeName = TYPE_NAME_NUMERIC; } else if (parameter instanceof Long[]) { typeName = TYPE_NAME_LONG; } if (typeName == null) { throw new TypeException("ArrayTypeHandler parameter typeName error, your type is " + parameter.getClass().getName()); } // 这3行是关键的代码,创建Array,然后ps.setArray(i, array)就可以了 Connection conn = ps.getConnection(); Array array = conn.createArrayOf(typeName, parameter); ps.setArray(i, array); } @Override public Object[] getNullableResult(ResultSet resultSet, String s) throws SQLException { return getArray(resultSet.getArray(s)); } @Override public Object[] getNullableResult(ResultSet resultSet, int i) throws SQLException { return getArray(resultSet.getArray(i)); } @Override public Object[] getNullableResult(CallableStatement callableStatement, int i) throws SQLException { return getArray(callableStatement.getArray(i)); } private Object[] getArray(Array array) { if (array == null) { return null; } try { return (Object[]) array.getArray(); } catch (Exception e) { } return null; } }
然后在实体类上加上注解@TableName(autoResultMap = true)
在实体类的数组字段上加上注解@TableField(typeHandler = ArrayTypeHandler.class, jdbcType = JdbcType.ARRAY)
这里注意ArrayTypeHandler的引用路径,不要引用了mybatis plus中自带的
最后去实体对应的mapper.xml文件中,对BaseResultMap中的数组字段设置<result column="数据库中的字段名" property="实体字段名" jdbcType="ARRAY" typeHandler="com.xxx.xxx.handler.ArrayTypeHandler"/>
以上弄完后,就可以检查结果了
感觉查阅,如果有帮助,记得收藏或者做笔记,避免下次遇到又找不着北~
标签:getArray,postgreSQL,NAME,parameter,typeName,plus,mybatis,TYPE,String From: https://www.cnblogs.com/ganggang7878/p/17163292.html