新建表的时候在int4后加上[]中括号就行

-- 创建数组
SELECT ARRAY[1, 2, 3, 4, 5];

-- 访问数组元素(从1开始)
SELECT ARRAY[1, 2, 3, 4, 5][1]; -- 返回 1

-- 数组长度
SELECT array_length(ARRAY[1, 2, 3, 4, 5], 1); -- 返回 5

-- 数组连接
SELECT ARRAY[1, 2, 3] || ARRAY[4, 5]; -- 返回 {1,2,3,4,5}

-- 数组包含元素
SELECT 3 = ANY(ARRAY[1, 2, 3, 4, 5]); -- 返回 true

-- 数组包含所有元素
SELECT ARRAY[1, 2] <@ ARRAY[1, 2, 3, 4, 5]; -- 返回 true

-- 数组包含任意元素
SELECT ARRAY[1, 6] && ARRAY[1, 2, 3, 4, 5]; -- 返回 true

-- 数组追加元素
SELECT array_append(ARRAY[1, 2, 3], 4); -- 返回 {1,2,3,4}

-- 数组前置元素
SELECT array_prepend(0, ARRAY[1, 2, 3]); -- 返回 {0,1,2,3}

-- 数组移除元素
SELECT array_remove(ARRAY[1, 2, 3, 2], 2); -- 返回 {1,3}

-- 数组替换元素
SELECT array_replace(ARRAY[1, 2, 3, 2], 2, 4); -- 返回 {1,4,3,4}

-- 将数组展开为行
SELECT unnest(ARRAY[1, 2, 3, 4, 5]); -- 返回每个元素作为一行

-- 数组聚合
SELECT array_agg(column_name) FROM table_name; -- 将列值聚合为数组

第一步新建IntArrayHandler

import org.apache.ibatis.type.BaseTypeHandler;
import org.apache.ibatis.type.JdbcType;
import org.apache.ibatis.type.MappedTypes;

import java.sql.*;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

@MappedTypes(List.class)
public class IntArrayHandler extends BaseTypeHandler<List<Integer>> {

    @Override
    public void setNonNullParameter(PreparedStatement preparedStatement, int i, List<Integer> integers, JdbcType jdbcType) throws SQLException {
        if (integers != null) {
            Array array = preparedStatement.getConnection().createArrayOf(JdbcType.INTEGER.name(), integers.toArray(new Integer[integers.size()]));
            preparedStatement.setArray(i, array);
        }
    }

    @Override
    public List<Integer> getNullableResult(ResultSet resultSet, String s) throws SQLException {
        Array array = resultSet.getArray(s);
        if (array == null) {
            return null;
        }
        Integer[] result = (Integer[]) array.getArray();
        array.free();
        return new ArrayList<>(Arrays.asList(result));
    }

    @Override
    public List<Integer> getNullableResult(ResultSet resultSet, int i) throws SQLException {
        Array array = resultSet.getArray(i);
        if (array == null) {
            return null;
        }
        Integer[] result = (Integer[]) array.getArray();
        array.free();
        return new ArrayList<>(Arrays.asList(result));
    }

    @Override
    public List<Integer> getNullableResult(CallableStatement callableStatement, int i) throws SQLException {
        Array array = callableStatement.getArray(i);
        if (array == null) {
            return null;
        }
        Integer[] result = (Integer[]) array.getArray();
        array.free();
        return new ArrayList<>(Arrays.asList(result));
    }
}

第二步在entity属性上设置typeHandler就可以

@TableField(value ="tags", typeHandler = IntArrayHandler.class)
    private List<Integer> tags;

如果调用insert报错是因为jdbc的版本bug,升级jdbc就行。

org.springframework.web.util.NestedServletException: Handler dispatch failed; nested exception is java.lang.NoSuchMethodError: java.nio.ByteBuffer.position(I)Ljava/nio/ByteBuffer;

可以升级postgresql jdbc版本
请添加图片描述

点赞(0) 打赏

评论列表 共有 0 条评论

暂无评论

微信公众账号

微信扫一扫加关注

发表
评论
返回
顶部