ai中如何做网站切图,邢台哪有学做网站的,公司建设网站需要多少钱,少儿编程加盟培宝未来在操作PostgreSQL开发场景中#xff0c;JSONB类型因高效的查询性能和结构化存储能力#xff0c;常被用于存储自定义复杂实体。但不少开发者会遇到「Failed to serialize ClauseExtractionResult to JSON」的序列化报错#xff0c;该问题直接导致数据入库、查询失败#xff…在操作PostgreSQL开发场景中JSONB类型因高效的查询性能和结构化存储能力常被用于存储自定义复杂实体。但不少开发者会遇到「Failed to serialize ClauseExtractionResult to JSON」的序列化报错该问题直接导致数据入库、查询失败是PostgreSQL开发中高频踩坑点。本文将从问题复现、根源分析入手重点讲解通过注解式TypeHandler结合PGobject解决该问题的实操方案。一、报错现象当执行插入/更新包含ClauseExtractionResult类型的JSONB字段时控制台抛出核心报错Failed to serialize ClauseExtractionResult to JSON Caused by: org.apache.ibatis.type.TypeException: Could not set parameters for mapping: ParameterMapping{propertyextractionResult, modeIN, javaTypeclass com.xxx.ClauseExtractionResult, jdbcTypeNULL, numericScalenull, resultMapIdnull, jdbcTypeNamenull, expressionnull}. Caused by: org.postgresql.util.PSQLException: Cant infer the SQL type to use for an instance of com.xxx.ClauseExtractionResult. Use setObject() with an explicit Types value to specify the type to use.二、问题根源分析PostgreSQL的JSONB类型是二进制存储的JSON格式。MyBatis默认的类型处理器仅支持基础数据类型与数据库字段的映射无法直接完成Java自定义实体ClauseExtractionResult到PostgreSQL JSONB类型的转换Java对象序列化后的JSON字符串无法直接匹配PG的JSONB二进制存储格式未指定自定义TypeHandler时MyBatis无法推断出Java对象对应的PG数据类型最终触发序列化失败。解决该问题的核心是通过自定义TypeHandler将Java对象序列化为JSON字符串后封装为PostgreSQL原生的PGobject并指定typejsonb完成类型适配。三、解决方案注解式注册TypeHandlerTypeHandler的注册方式有注解、sql.xml、全局xml三种其中注解方式灵活度最高仅作用于指定字段也是本文的核心方案关键是确保使用PGobject且明确指定typejsonb。步骤1自定义JSONB类型处理器创建自定义TypeHandler继承MyBatis的BaseTypeHandler重写数据入库/查询的类型转换逻辑核心是将Java对象序列化为JSON字符串并封装到PGobject中。importorg.apache.ibatis.type.BaseTypeHandler;importorg.apache.ibatis.type.JdbcType;importorg.postgresql.util.PGobject;importcom.fasterxml.jackson.databind.ObjectMapper;importcom.xxx.ClauseExtractionResult;importjava.sql.*;/** * PostgreSQL JSONB类型处理器适配ClauseExtractionResult实体 */publicclassClauseExtractionResultJsonbTypeHandlerextendsBaseTypeHandlerClauseExtractionResult{// 全局复用ObjectMapper避免重复创建导致性能损耗privatestaticfinalObjectMapperOBJECT_MAPPERnewObjectMapper();privatestaticfinalPGobjectPG_OBJECTnewPGobject();OverridepublicvoidsetNonNullParameter(PreparedStatementps,inti,ClauseExtractionResultparameter,JdbcTypejdbcType)throwsSQLException{try{// 1. 将Java对象序列化为JSON字符串StringjsonStrOBJECT_MAPPER.writeValueAsString(parameter);// 2. 关键指定PGobject类型为jsonb不可写为jsonPG_OBJECT.setType(jsonb);// 3. 为PGobject赋值JSON字符串PG_OBJECT.setValue(jsonStr);// 4. 将PGobject写入PreparedStatementps.setObject(i,PG_OBJECT);}catch(Exceptione){thrownewSQLException(Failed to serialize ClauseExtractionResult to JSONB,e);}}OverridepublicClauseExtractionResultgetNullableResult(ResultSetrs,StringcolumnName)throwsSQLException{returnparseJson(rs.getString(columnName));}OverridepublicClauseExtractionResultgetNullableResult(ResultSetrs,intcolumnIndex)throwsSQLException{returnparseJson(rs.getString(columnIndex));}OverridepublicClauseExtractionResultgetNullableResult(CallableStatementcs,intcolumnIndex)throwsSQLException{returnparseJson(cs.getString(columnIndex));}// 通用方法JSON字符串反序列化为ClauseExtractionResult对象privateClauseExtractionResultparseJson(StringjsonStr){if(jsonStrnull||jsonStr.isEmpty()){returnnull;}try{returnOBJECT_MAPPER.readValue(jsonStr,ClauseExtractionResult.class);}catch(Exceptione){thrownewRuntimeException(Failed to deserialize JSON to ClauseExtractionResult,e);}}}步骤2在实体类字段上注解指定TypeHandler在映射JSONB字段的ClauseExtractionResult属性上通过MyBatis的TypeHandler注解关联自定义处理器无需修改xml配置实现字段级别的类型适配。importcom.baomidou.mybatisplus.annotation.TableField;importorg.apache.ibatis.type.TypeHandler;importcom.xxx.handler.ClauseExtractionResultJsonbTypeHandler;publicclassContractClause{// 其他业务字段如id等.../** * 映射PostgreSQL的jsonb字段 */TableField(valueextraction_result,typeHandlerClauseExtractionResultJsonbTypeHandler.class)privateClauseExtractionResultextractionResult;// getter/setter方法...}步骤3验证结果重新执行包含ClauseExtractionResult字段的CRUD操作此时MyBatis会通过自定义TypeHandler完成Java对象到PG JSONB类型的转换「Failed to serialize ClauseExtractionResult to JSON」报错消失数据可正常入库和查询。