最近同事使用mybatis-plus处理blob类型数据保存进mysql数据库时报错了,提示如下:

java.lang.IllegalStateException: Type handler was null on parameter mapping for property 'embedding'. It was either not specified and/or could not be found for the javaType (java.sql.Blob) : jdbcType (null) combination.

我之前解决过这个问题,翻了一下git提交记录找到了,最佳实例如下:

一、新增处理blob类型的handler

@MappedJdbcTypes(JdbcType.BLOB)
@MappedTypes(Blob.class)
public class CustomBlobTypeHandler extends BaseTypeHandler<Blob> {

    @Override
    public void setNonNullParameter(PreparedStatement ps, int i,
                                    Blob parameter, JdbcType jdbcType) throws SQLException {
        InputStream is = parameter.getBinaryStream();
        try {
            ps.setBinaryStream(i, is, is.available());
        } catch (IOException e) {
            throw new SQLException(e);
        }
    }

    @Override
    public Blob getNullableResult(ResultSet rs, String columnName)
            throws SQLException {
        return rs.getBlob(columnName);
    }

    @Override
    public Blob getNullableResult(ResultSet rs, int columnIndex)
            throws SQLException {
        return rs.getBlob(columnIndex);
    }

    @Override
    public Blob getNullableResult(CallableStatement cs, int columnIndex)
            throws SQLException {
        return cs.getBlob(columnIndex);
    }

}

二、配置handler

mybatis-plus:
  global-config:
    db-config:
      id-type: assign_id
# 对新增的handler进行配置
  type-handlers-package: *.mybatis.handlers

再加一个测试用例测试,问题解决。

Logo

魔乐社区(Modelers.cn) 是一个中立、公益的人工智能社区,提供人工智能工具、模型、数据的托管、展示与应用协同服务,为人工智能开发及爱好者搭建开放的学习交流平台。社区通过理事会方式运作,由全产业链共同建设、共同运营、共同享有,推动国产AI生态繁荣发展。

更多推荐