java mybatis如何操作postgresql array數組類型

找瞭半天沒有找到postgresql中關於array數組類型的字段如何對應到java中的數據類型,後來找到瞭mybatis的TypeHandler,隻要實現一個自定義的TypeHandler就行瞭,如下,

我定義瞭幾個基礎數據類型的數組

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";
    @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;
        }
        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;
    }
}

然後在實體類裡面對應的字段配置這個轉換器就行,如果是配置文件需要在 xxmap.xml中配置對應屬性

@ColumnType(typeHandler = com.mvc.typehandler.ArrayTypeHandler.class)
private String[] cities; //所操作的城市code列表,為空表示所有城市
@ColumnType(typeHandler = com.mvc.typehandler.ArrayTypeHandler.class)
private String[] areaTypes; //操作的行業類型code列表,為空表示所有行業
@ColumnType(typeHandler = com.jmtool.mvc.typehandler.ArrayTypeHandler.class)
private Integer[] targetStatus; //操作的目標數據狀態

java mybatis操作 postgresql array數組類型備忘

postgrey sql數據庫字段類型varchar[],java使用String[]類型,自定義ArrayTypeHandler

ArrayTypeHandler.java

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";
    @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;
        }
        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;
    }
}

配置文件中映射配置,增加typeHandler屬性,填寫ArrayTypeHandler.java的全路徑。

<result column="source_type" jdbcType="ARRAY" property="sourceType" typeHandler="com.wang.common.ArrayTypeHandler" />

用到的SQL:

<insert id="insert" parameterType="com.wang.pojo.Qzf">
    insert into t_qz_qzf(id,source_type)
    values
    (#{id,jdbcType=VARCHAR},#{sourceType,jdbcType=ARRAY,typeHandler="com.wang.common.ArrayTypeHandler"})
</insert>

以上為個人經驗,希望能給大傢一個參考,也希望大傢多多支持WalkonNet。

推薦閱讀: