当前使用版本(必填,否则不予处理)
<dependency>
<groupId>com.baomidou</groupId>
<artifactId>mybatis-plus-boot-starter</artifactId>
<version>3.5.2</version>
</dependency>
该问题是如何引起的?(确定最新版也有问题再提!!!)
我通过LambdaUpdateWrapper向公司项目的一个表的json类型的字段进行修改时,报了类型不一致的错误。在对应的类中添加了处理,,但依旧报错,debug也进不到handler方法中。
重现步骤(如果有就写完整)
- 修改数据库表对应的Java对象,关于json数据类型的Java数据类型
- 编写对应的handler处理器,对改字段单独处理 Java属性修改如下:
@TableField(jdbcType = JdbcType.OTHER, typeHandler = JsonArrayTypeHandler.class)
private Object measureValue;
JsonArrayTypeHandler内容:
@MappedTypes({Object.class})
public class JsonArrayTypeHandler extends BaseTypeHandler<Object> {
//引入PGSQL提供的工具类PGobject
private static final PGobject jsonObject = new PGobject();
@Override
public void setNonNullParameter(PreparedStatement ps, int i, Object parameter, JdbcType jdbcType) throws SQLException {
jsonObject.setType("json");
jsonObject.setValue(parameter.toString());
ps.setObject(i, jsonObject);
}
@Override
public Object getNullableResult(ResultSet rs, String s) throws SQLException {
return rs.getString(s);
}
@Override
public Object getNullableResult(ResultSet rs, int columnIndex) throws SQLException {
return rs.getString(columnIndex);
}
@Override
public Object getNullableResult(CallableStatement cs, int columnIndex) throws SQLException {
return cs.getString(columnIndex);
}
}
- 使用对应的LambdaUpdateWrapper对数据进行修改
LambdaUpdateWrapper<Detection> regularWorkLambdaUpdateWrapper = new LambdaUpdateWrapper<>();
regularWorkLambdaUpdateWrapper.eq(Detection::getId, regularWorkUploadedOfflineReq.getId());
regularWorkLambdaUpdateWrapper.set(Detection::getMeasureValue, regularWorkUploadedOfflineReq.getMeasureValue());
update(regularWorkLambdaUpdateWrapper);
执行就报错了,并且debug断点也走不到handler处理器里面。
- 通过创建的对应的实体类,调用updateById就可以执行对应的handler对json数据进行处理
报错信息
Error updating database. Cause: org.postgresql.util.PSQLException: ERROR: column \"measure_value\" is of type json but expression is of type character varying\n 建议:You will need to rewrite or cast the expression.
Comment From: caishenao
使用updateById方法对json类型的数据进行更新就可以,但是通过LambdaUpdateWrapper就会报错,但是数据库对应类上的属性上已经添加了对应的注解和数据处理方法,为什么执行update的时候,不会调用定期的数据处理器呢?
Comment From: miemieYaho
set(Detection::getMeasureValue, regularWorkUploadedOfflineReq.getMeasureValue(),"typehandler=xxx.xx.xx.xx");
Comment From: caishenao
谢谢@miemieYaho,疑惑解决了
Comment From: zhaoyunxing92
set(Detection::getMeasureValue, regularWorkUploadedOfflineReq.getMeasureValue(),"typehandler=xxx.xx.xx.xx");
set(Detection::getMeasureValue, regularWorkUploadedOfflineReq.getMeasureValue(),"typehandler=xxx.xx.xx.xx");
set(Detection::getMeasureValue, regularWorkUploadedOfflineReq.getMeasureValue(),"typehandler=xxx.xx.xx.xx");
这个底层是不是可以修改判断下如果列上有 typehander 就自动添加上