PostgreSQL表中字段由字符串改为数组

需求:PostgreSQL数据库中的一张表的某些字段,之前存的是字符串,由于业务需求变更,需要存储多条数据,字段类型要改为数组,并保留原来的数据。

具体实现:

  1. 修改表中字段类型:把 etl_flow_template 表中的 protocol_document_path_list字段和 protocol_document_file_name_list 字段修改为数组类型,并保留原来的值:
    alter table etl_flow_template alter column protocol_document_path_list type text[] using array[protocol_document_path_list]::text[];
    
    alter table etl_flow_template alter column protocol_document_file_name_list type text[] using array[protocol_document_file_name_list]::text[];

    修改后表中字段效果:

  2. 修改映射文件中数据类型:
    <resultMap id="BaseResultMap" type="com.hikvision.idatafusion.hdiwebsite.dto.template.TemplateView">
            <id column="id" jdbcType="BIGINT" property="id"/>
            <result column="template_name" jdbcType="VARCHAR" property="templateName"/>
            <result column="icon_path" jdbcType="VARCHAR" property="iconPath"/>
            <result column="template_path" jdbcType="VARCHAR" property="templatePath"/>
            <result column="template_file_name" jdbcType="VARCHAR" property="templateFileName"/>
            <result column="protocol_document_path_list" jdbcType="VARCHAR" property="protocolDocumentPathList"
                    typeHandler="com.hikvision.idatafusion.dhidata.commons.typeHandler.ArrayTypeHandlerPg"/>
            <result column="protocol_document_file_name_list" jdbcType="VARCHAR" property="protocolDocumentFileNameList"
                    typeHandler="com.hikvision.idatafusion.dhidata.commons.typeHandler.ArrayTypeHandlerPg"/>
    </resultMap>
    其中 ArrayTypeHandlerPg 是转换的工具类:
    public class ArrayTypeHandlerPg implements TypeHandler<List<?>> {
    
        @Override
        public void setParameter(PreparedStatement ps, int i, List<?> parameter, JdbcType jdbcType) throws SQLException {
            //用于pg数组
            if (parameter == null) {
                try {
                    ps.setNull(i, JdbcType.ARRAY.TYPE_CODE);
                } catch (SQLException e) {
                    throw new TypeException("Error setting null for parameter #" + i + " with JdbcType " + jdbcType + " . "
                            + "Try setting a different JdbcType for this parameter or a different jdbcTypeForNull configuration property. "
                            + "Cause: " + e, e);
                }
            } else {
                try {
                    ps.setArray(i, ps.getConnection().createArrayOf(jdbcType.name(), parameter.toArray()));
                } catch (Exception e) {
                    throw new TypeException("Error setting non null for parameter #" + i + " with JdbcType " + jdbcType
                            + " . "
                            + "Try setting a different JdbcType for this parameter or a different configuration property. "
                            + "Cause: " + e, e);
                }
            }
        }
    
        @Override
        public List<?> getResult(ResultSet rs, String columnName) throws SQLException {
            List<?> result;
            try {
                Array array = rs.getArray(columnName);
                result = array == null ? null : new ArrayList<>(Arrays.asList((Object[]) array.getArray()));
            } catch (Exception e) {
                throw new ResultMapException(
                        "Error attempting to get column '" + columnName + "' from result list.  Cause: " + e, e);
            }
            if (rs.wasNull()) {
                return null;
            } else {
                return result;
            }
        }
    
        @Override
        public List<?> getResult(ResultSet rs, int columnIndex) throws SQLException {
            List<?> result;
            try {
                Array array = rs.getArray(columnIndex);
                result = array == null ? null : new ArrayList<>(Arrays.asList((Object[]) array.getArray()));
            } catch (Exception e) {
                throw new ResultMapException(
                        "Error attempting to get column #" + columnIndex + " from result list.  Cause: " + e, e);
            }
            if (rs.wasNull()) {
                return null;
            } else {
                return result;
            }
        }
    
        @Override
        public List<?> getResult(CallableStatement cs, int columnIndex) throws SQLException {
            List<?> result;
            try {
                Array array = cs.getArray(columnIndex);
                result = array == null ? null : new ArrayList<>(Arrays.asList((Object[]) array.getArray()));
            } catch (Exception e) {
                throw new ResultMapException(
                        "Error attempting to get column #" + columnIndex + " from callable statement.  Cause: " + e, e);
            }
            if (cs.wasNull()) {
                return null;
            } else {
                return result;
            }
        }
    }
  3. 修改增删改查数据sql语句:
    增、查时,resultMap 引用上面的映射文件的 BaseResultMap 便可 resultMap="BaseResultMap",改时需要注意进行转换:
    <update id="updateProtocolData" parameterType="com.hikvision.idatafusion.hdiwebsite.model.EtlFlowTemplate">
            update etl_flow_template set
            <if test="protocolNames != null and protocolNames.size() > 0">
                protocol_document_file_name_list = #{protocolNames, jdbcType=VARCHAR, typeHandler=com.hikvision.idatafusion.dhidata.commons.typeHandler.ArrayTypeHandlerPg},
            </if>
            <if test="protocolPaths != null and protocolPaths.size() > 0">
                protocol_document_path_list = #{protocolPaths, jdbcType=VARCHAR, typeHandler=com.hikvision.idatafusion.dhidata.commons.typeHandler.ArrayTypeHandlerPg}
            </if>
            where id = #{templateId}
    </update>

     

http://www.niftyadmin.cn/n/5277562.html

相关文章

[产品性问题]由于低温(-30℃)导致串口数据错误

一、现象 串口数据在解析时一直失败&#xff0c;经查数据发现完全混乱&#xff0c;其他子设备无法识别。 二、排查 常温环境正常&#xff0c;低温环境出现异常。 低温下检测串口波特率频率&#xff0c;发现时序异常。 进一步检测芯片主频&#xff0c;发现主频异常。 三、…

在QT Creator下用CMake编译GEOS库

最近&#xff0c;想要在C下编一个可用GDAL模块的地图管理系统&#xff0c;找来找去&#xff0c;找到了GEOS。GEOS&#xff08;Geometry Engine-Open Source&#xff09;开源几何引擎 是一个用于计算几何的JTS库的 C/C实现&#xff0c;专注于地理信息系统 &#xff08;GIS&#…

redis 实现队列

一 使用场景 在一些场景&#xff0c;项目已发布了一段时间了&#xff0c;只是需要完善或优化一些功能要用到队列&#xff0c;但不想改动太大&#xff08;或者不想在安装第三方MQ组件框架&#xff09;的情况下可以用redis实现队列 。 二 redis实现队列 1 redis有序队列 使用red…

Android年份选择器(超简单-可直接复制使用)

效果图 思路 1、流程&#xff1a; 通过点击textview触发年份选择器dialog显示&#xff0c;选中年份后&#xff0c;更新到textview。 2、如何只显示年份&#xff1f; 隐藏月份和天数即可&#xff08;但仍需给一个初始化数据&#xff09;。 实现 1、直接新建一个工具类OnPickY…

C/C++与MySQL:多线程、大并发和异步操作的实践

C/C与MySQL&#xff1a;多线程、大并发和异步操作的实践 在前面的文章中&#xff0c;我们介绍了如何使用C/C调用MYSQL API进行基本的数据库操作。然而&#xff0c;在实际应用中&#xff0c;特别是面对大量用户请求和高并发场景时&#xff0c;单线程的数据库操作往往显得力不从…

java 非常好用的一个缓存(Google Guava的Cache)

基本每个web项目都少不了缓存&#xff0c;通常很时候都会选择redis作缓存&#xff0c;或者自己用map轻松实现&#xff0c;但上面这两种缓存有时满足不了需求&#xff0c;有时觉得redis作缓存有点重&#xff0c;而map手功实现又太轻或功能不足时&#xff08;缓存时过期实现&…

软件测试工程师,“我“从月10k到月30k进阶自动化测试之路...

目录&#xff1a;导读 前言一、Python编程入门到精通二、接口自动化项目实战三、Web自动化项目实战四、App自动化项目实战五、一线大厂简历六、测试开发DevOps体系七、常用自动化测试工具八、JMeter性能测试九、总结&#xff08;尾部小惊喜&#xff09; 前言 作为手工测试&…

Docker 网络模式 -day05

docker 启动时候还会有&#xff0c;名为docker0的虚拟网桥&#xff0c;注意网址为 127.0.0.1 [rootiZuf6hxabqikytnrumsi4gZ ~]# ifconfig docker0: flags4163<UP,BROADCAST,RUNNING,MULTICAST> mtu 1500inet 172.17.0.1 netmask 255.255.0.0 broadcast 172.17.255.2…