baomidou / mybatis-plus

An powerful enhanced toolkit of MyBatis for simplify development
https://baomidou.com
Apache License 2.0
16.3k stars 4.3k forks source link

[错误报告]: updateWrapper无法正确处理pg 的 jsonb字段映射 #6468

Closed wzyxdwll closed 1 week ago

wzyxdwll commented 1 week ago

确认

当前程序版本

3.5.5

问题描述

详情见:https://blog.csdn.net/xiaosi_xiaosi/article/details/138540152

  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 {

private static final PGobject jsonObject = new PGobject();

@Override
public void setNonNullParameter(PreparedStatement ps, int i, JSONObject parameter, JdbcType jdbcType) throws SQLException {
    jsonObject.setType("json");
    jsonObject.setValue(parameter.toString());
    ps.setObject(i, jsonObject);
}

@Override
public JSONObject getNullableResult(ResultSet rs, String columnName) throws SQLException {
    String sqlJson = rs.getString(columnName);
    if (null != sqlJson) {
        return JSONObject.parseObject(sqlJson);
    }
    return null;
}

@Override
public JSONObject getNullableResult(ResultSet rs, int columnIndex) throws SQLException {
    String sqlJson = rs.getString(columnIndex);
    if (null != sqlJson) {
        return JSONObject.parseObject(sqlJson);
    }
    return null;
}

@Override
public JSONObject getNullableResult(CallableStatement cs, int columnIndex) throws SQLException {
    String sqlJson = cs.getString(columnIndex);
    if (null != sqlJson) {
        return JSONObject.parseObject(sqlJson);
    }
    return null;
}

}


5. update Wrapper

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


在JSONTypeHandler的setNonNullParameter方法添加断点,发现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);
}

### 详细堆栈日志

```bash
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
miemieYaho commented 1 week ago

注入成全局