数据库 表的字段存储数组 字符串转数组 PostgreSQL表中字段由字符串改为数组 - } else {

📅 2026-11-30 👤 拼多多助力营销技巧 📂 首码项目 👁️ 345 次阅读

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

具体实现:

修改表中字段类型:把 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[]; 修改后表中字段效果: 修改映射文件中数据类型:

typeHandler="com.hikvision.idatafusion.dhidata.commons.typeHandler.ArrayTypeHandlerPg"/>

typeHandler="com.hikvision.idatafusion.dhidata.commons.typeHandler.ArrayTypeHandlerPg"/>

其中 ArrayTypeHandlerPg 是转换的工具类: public class ArrayTypeHandlerPg implements TypeHandler> {@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;

}

}

} 修改增删改查数据sql语句: 增、查时,resultMap 引用上面的映射文件的 BaseResultMap 便可 resultMap="BaseResultMap",改时需要注意进行转换:

update etl_flow_template set

protocol_document_file_name_list = #{protocolNames, jdbcType=VARCHAR, typeHandler=com.hikvision.idatafusion.dhidata.commons.typeHandler.ArrayTypeHandlerPg},

protocol_document_path_list = #{protocolPaths, jdbcType=VARCHAR, typeHandler=com.hikvision.idatafusion.dhidata.commons.typeHandler.ArrayTypeHandlerPg}

where id = #{templateId}

精彩链接

标签: 拼多多助力网站平台 赚钱项目 拼多多助力网 首码项目 手赚APP
拼多多助力营销技巧