• java开发中postgresql数组字段类型处理


    在实际开发中遇到postgresql中定义的数组字段,下面解决两个问题,如何定义数组字段的默认值为空格数组,以及如何再java实体类中直接使用数组对象接受数据或把数据存入数据库。

    1.在postgresql中定义数组对象及默认值

    以字符串你数组为例:
    比如一个字段用于存储多张图片的url,可以使用一下sql定义

    pictures _varchar NOT NUll default ARRAY[]::character varying[]
    
    • 1

    2.实体类存入数组到数据库并接受数据库的数组数据
    直接定义数组字段并无法实现数据存入和接受。还需要一个typeHandler。这个typeHandler是自定的,而且整型数组和字符串数组不一样,下面以字符串数组为例。

    typeHandler类如下:

    import org.apache.ibatis.type.BaseTypeHandler;
    import org.apache.ibatis.type.JdbcType;
    
    import java.sql.*;
    import java.util.List;
    
    public class StringListArrayTypeHandler extends BaseTypeHandler<List<String>> {
    
        @Override
        public void setNonNullParameter(PreparedStatement preparedStatement, int i, List<String> strings, JdbcType jdbcType) throws SQLException {
            if (strings != null) {
                Array array = preparedStatement.getConnection().createArrayOf(JdbcType.VARCHAR.name(), strings.toArray(new String[0]));
                preparedStatement.setArray(i, array);
            }
        }
    
        @Override
        public List<String> getNullableResult(ResultSet resultSet, String s) throws SQLException {
            Array array = resultSet.getArray(s);
            if (array == null) {
                return null;
            }
            String[] result = (String[]) array.getArray();
            array.free();
            return List.of(result);
        }
    
        @Override
        public List<String> getNullableResult(ResultSet resultSet, int i) throws SQLException {
            Array array = resultSet.getArray(i);
            if (array == null) {
                return null;
            }
            String[] result = (String[]) array.getArray();
            array.free();
            return List.of(result);
        }
    
        @Override
        public List<String> getNullableResult(CallableStatement callableStatement, int i) throws SQLException {
            Array array = callableStatement.getArray(i);
            if (array == null) {
                return null;
            }
            String[] result = (String[]) array.getArray();
            array.free();
            return List.of(result);
        }
    }
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50

    typeHandler一般放在项目的config目录下。

    实体类的定义如下:

    @TableName(autoResultMap = true)
    public class Entity implements Serializable {
    
        private static final long serialVersionUID = 1L;
    
        @TableId(type = IdType.AUTO)
        private Long id;
        
        private String code;
    
        private String name;
    
        /**
         * 图片
         */
        @TableField(typeHandler = StringListArrayTypeHandler.class)
        private List<String> pictures;
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18

    注意注解@TableName(autoResultMap = true)也要加上。

    附加整型数组的typeHandler:

    import org.apache.ibatis.type.BaseTypeHandler;
    import org.apache.ibatis.type.JdbcType;
    
    import java.sql.*;
    import java.util.Collections;
    import java.util.List;
    
    public class IntegerListArrayTypeHandler 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[0]));
                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 List.of(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 List.of(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 List.of(result);
        }
    }
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
  • 相关阅读:
    Splunk的转发器扮演什么角色?
    Chapter4:Traing Model
    第十三章 Python操作数据库
    Codeanalysis(tca)后端二次开发环境搭建
    IDEA文件UTF-8格式控制台输出中文乱码
    如何实现晶圆载具ACSII码条码数据与TI玻璃管RFID标签16进制数据匹配
    leetcode 925. 长按键入
    涂鸦Wi-Fi&BLE SoC开发幻彩灯带(2)----环境搭建与测试
    我最佩服的一位学生!他是哈工大在读NLP博士积累28W粉丝
    [推荐系统] 1. 深度学习与推荐系统
  • 原文地址:https://blog.csdn.net/qq_43745578/article/details/126658663