当前位置: 首页 > news >正文

Mybatis plus update PG json 类型 报错解决

Mybatis plus update PG json 类型 报错解决

  • 1. 定义的PG数据库对象
  • 2. 自定义 JSON Handler
  • 3. update Wrapper
  • 4. update 报错信息
    • 4.1 No hstore extension installed.
    • 4.2 Error setting non null for parameter #1 with JdbcType null . Try setting a different JdbcType for this parameter or a different configuration property.
  • 5. 解决方案

1. 定义的PG数据库对象

@TableName("t_demo")
public class DemoPO {@TableField(value = "content", typeHandler = JSONTypeHandler.class)private JSONObject content;}

2. 自定义 JSON Handler

package com.xxx.handler;import com.alibaba.fastjson2.JSONObject;
import org.apache.ibatis.type.BaseTypeHandler;
import org.apache.ibatis.type.JdbcType;
import org.apache.ibatis.type.MappedTypes;
import org.postgresql.util.PGobject;import java.sql.CallableStatement;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;@MappedTypes(JSONObject.class)
public class JSONTypeHandler extends BaseTypeHandler<JSONObject> {private static final PGobject jsonObject = new PGobject();@Overridepublic void setNonNullParameter(PreparedStatement ps, int i, JSONObject parameter, JdbcType jdbcType) throws SQLException {jsonObject.setType("json");jsonObject.setValue(parameter.toString());ps.setObject(i, jsonObject);}@Overridepublic JSONObject getNullableResult(ResultSet rs, String columnName) throws SQLException {String sqlJson = rs.getString(columnName);if (null != sqlJson) {return JSONObject.parseObject(sqlJson);}return null;}@Overridepublic JSONObject getNullableResult(ResultSet rs, int columnIndex) throws SQLException {String sqlJson = rs.getString(columnIndex);if (null != sqlJson) {return JSONObject.parseObject(sqlJson);}return null;}@Overridepublic JSONObject getNullableResult(CallableStatement cs, int columnIndex) throws SQLException {String sqlJson = cs.getString(columnIndex);if (null != sqlJson) {return JSONObject.parseObject(sqlJson);}return null;}
}

3. update Wrapper

public boolean update(String id, JSONObject content) {LambdaUpdateWrapper<DemoPO> lambdaUpdate = new LambdaUpdateWrapper<>();lambdaUpdate.eq(DemoPO::getId, id)lambdaUpdate.set(DemoPO::getContent, content);return update(lambdaUpdate);
}

4. update 报错信息

Caused by: org.apache.ibatis.type.TypeException: Error setting non null for parameter #1 with JdbcType null . Try setting a different JdbcType for this parameter or a different configuration property. Cause: org.apache.ibatis.type.TypeException: Error setting non null for parameter #1 with JdbcType null . Try setting a different JdbcType for this parameter or a different configuration property. Cause: org.postgresql.util.PSQLException: No hstore extension installed.at org.apache.ibatis.type.BaseTypeHandler.setParameter(BaseTypeHandler.java:76)at com.baomidou.mybatisplus.core.MybatisParameterHandler.setParameters(MybatisParameterHandler.java:296)... 209 common frames omitted
Caused by: org.apache.ibatis.type.TypeException: Error setting non null for parameter #1 with JdbcType null . Try setting a different JdbcType for this parameter or a different configuration property. Cause: org.postgresql.util.PSQLException: No hstore extension installed.at org.apache.ibatis.type.BaseTypeHandler.setParameter(BaseTypeHandler.java:76)at org.apache.ibatis.type.UnknownTypeHandler.setNonNullParameter(UnknownTypeHandler.java:71)at org.apache.ibatis.type.BaseTypeHandler.setParameter(BaseTypeHandler.java:74)... 210 common frames omitted
Caused by: org.postgresql.util.PSQLException: No hstore extension installed.at org.postgresql.jdbc.PgPreparedStatement.setMap(PgPreparedStatement.java:559)at org.postgresql.jdbc.PgPreparedStatement.setObject(PgPreparedStatement.java:1063)at com.zaxxer.hikari.pool.HikariProxyPreparedStatement.setObject(HikariProxyPreparedStatement.java)at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method)at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:77)at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)at java.base/java.lang.reflect.Method.invoke(Method.java:568)at org.apache.ibatis.logging.jdbc.PreparedStatementLogger.invoke(PreparedStatementLogger.java:67)at jdk.proxy3/jdk.proxy3.$Proxy121.setObject(Unknown Source)at org.apache.ibatis.type.ObjectTypeHandler.setNonNullParameter(ObjectTypeHandler.java:31)at org.apache.ibatis.type.BaseTypeHandler.setParameter(BaseTypeHandler.java:74)... 212 common frames omitted

4.1 No hstore extension installed.

hstore 是 PG 扩展的一种数据结构,但实际上我们 PG 数据库定义的为 json 类型,所以请忽略这个报错。

4.2 Error setting non null for parameter #1 with JdbcType null . Try setting a different JdbcType for this parameter or a different configuration property.

关注一下这个报错,其实错误指向也不是很明确,但大体可以 GET 到类型转换出问题了。

so 我们在JSONTypeHandlersetNonNullParameter方法添加断点,发现insert都会走到这个方法,但是update却不会走到这边。

    public void setNonNullParameter(PreparedStatement ps, int i, JSONObject parameter, JdbcType jdbcType) throws SQLException {jsonObject.setType("json"); // 这一行添加断点,检查 update 是否经过这边jsonObject.setValue(parameter.toString());ps.setObject(i, jsonObject);}

5. 解决方案

update 的时候手动添加typeHandler

lambdaUpdate.set(DemoPO::getContent, content);
==>
lambdaUpdate.set(DemoPO::getContent, content, "typeHandler=com.xxx.handler.JSONTypeHandler");
http://www.lryc.cn/news/343175.html

相关文章:

  • 精通 Docker:简化开发、部署与安全保障
  • KIMI的API使用:重点是他的API在使用的适合可以实时调用tool(外部联网等)
  • Android内核之Binder读写通信:binder_ioctl_write_read用法实例(七十)
  • 【C语言/数据结构】经典链表OJ习题~第二期——链中寻环
  • MySQL日志机制【undo log、redo log、binlog 】
  • SSL通信、证书认证原理和失败原因
  • 【MsSQL】数据库基础 库的基本操作
  • AI编码工具-通义灵码功能实测
  • 直接显示二进制图片
  • 微软exchange邮箱发送
  • AI绘画Stable Diffusion SDXL 超赞!高质量万能大模型,写实人像、时尚设计、建筑设计、电影制作—筑梦工业XLV4.0
  • 数字人捕捉、建模与合成
  • Yarn:下一代JavaScript包管理器的安装与实战指南
  • JVM进程缓存 Caffeine
  • c++ 线程交叉场景试验
  • Cell:如何升华你的单细胞数据——PCF空间单细胞蛋白组联合scRNA-seq解析骨髓微环境
  • vue强制刷新组件
  • 分享5款对工作学习有帮助的效率软件
  • redis秒杀(PHP版本)
  • 图形用户界面(GUI)在AI去衣技术中的作用与重要性
  • 如何阅读:一个已被证实的低投入高回报的学习方法的笔记
  • pycharm 安装“通义灵码“并测试
  • React 之 useMemo Hook (九)
  • 短视频矩阵系统源码saas开发--可视化剪辑、矩阵托管、多功能合一开发
  • 百度大模型文心一言api 请求错误码 一览表
  • Unity调用智谱API(简单操作 文本实时翻译)
  • Android 开机启动扫描SD卡apk流程源码分析
  • 如何恢复回收站中被删除的文件?3个恢复策略,实测有用!
  • Unity---版本控制软件
  • 基于大模型的idea提炼:围绕论文和引用提炼idea之ResearchAgent