当前使用版本(必填,否则不予处理)
mybatis-plus: 3.5.3.1
该问题是如何引起的
保存对象时无法保存,对象属性中存在Map并已经自定义了TypeHandler转成json来保存,出现以下错误
重现步骤
xml:
<mapper namespace="com.device.tdengine.tsmapper.TdengineRequestMapper">
<insert id="insertBatch">
insert into
<foreach collection="datas" index="key" item="value">
device_request_${value.tag.deviceId} using super_device_request TAGS (#{value.tag.deviceId},#{value.tag.productId},#{value.tag.func}) VALUES
<foreach collection="value.datas" item="value1" separator=" ">
(#{value1.ts},#{value1.messageId},#{value1.arg,jdbcType=VARCHAR})
</foreach>
</foreach>
</insert>
</mapper>
pojo
保存的对象DeviceRequestDataTag
@Getter
@Setter
public class DeviceRequestDataTag {
DeviceRequestTag tag;
List<DeviceRequest> datas;
}
@Getter
@Setter
@TableName(value = "super_device_request",autoResultMap = true)
public class DeviceRequest implements Serializable {
String messageId;
Long deviceId;
Long productId;
String func;
@TableField(value = "arg", typeHandler = MapTypeHandler.class,jdbcType = JdbcType.VARCHAR)
Map<String,Object> arg;
Timestamp ts;
}
@MappedTypes({Map.class})
@MappedJdbcTypes(JdbcType.VARCHAR)
public class MapTypeHandler extends AbstractJsonTypeHandler<Map> {
Class<?> type;
public MapTypeHandler(Class<?> type) {
this.type = type;
}
@Override
protected Map parse(String json) {
if(StrUtil.isNotEmpty(json)){
return JSONUtil.toBean(json,Map.class);
}else{
return null;
}
}
@Override
protected String toJson(Map obj) {
return JSONUtil.toJsonStr(obj);
}
}
报错信息
··· org.mybatis.spring.MyBatisSystemException: nested exception is org.apache.ibatis.exceptions.PersistenceException:
Error updating database. Cause: java.lang.IllegalStateException: Type handler was null on parameter mapping for property '__frch_value1_1.arg'. It was either not specified and/or could not be found for the javaType (java.util.Map) : jdbcType (VARCHAR) combination.
The error may exist in file [D:\webproject\nacos-device\device-data\tdengine-component\target\classes\tsmapper\TdengineDeviceRequestMapper.xml]
The error may involve com.device.tdengine.tsmapper.TdengineRequestMapper.insertBatch
The error occurred while executing an update
Cause: java.lang.IllegalStateException: Type handler was null on parameter mapping for property '__frch_value1_1.arg'. It was either not specified and/or could not be found for the javaType (java.util.Map) : jdbcType (VARCHAR) combination.
at org.mybatis.spring.MyBatisExceptionTranslator.translateExceptionIfPossible(MyBatisExceptionTranslator.java:96)
at org.mybatis.spring.SqlSessionTemplate$SqlSessionInterceptor.invoke(SqlSessionTemplate.java:441)
at com.sun.proxy.$Proxy129.insert(Unknown Source)
at org.mybatis.spring.SqlSessionTemplate.insert(SqlSessionTemplate.java:272)
··· 你好,在保存对象时,MapTypeHandler 不会触发tojson 是哪里写错了吗?非常感谢🙏
Comment From: tom055
已解决,用BaseTypeHandler去实现