使用mybatis plus 做持久层 不能直接映射JSONObject到postsql的jsonb
报错:
Cause: java.lang.IllegalStateException: Type handler was null on parameter mapping for property '字段名'. It was either not specified and/or could not be found for the javaType (com.alibaba.fastjson.JSONObject) : jdbcType (null) combination.
at org.mybatis.spring.MyBatisExceptionTranslator.translateExceptionIfPossible(MyBatisExceptionTranslator.java:96)
at org.mybatis.spring.SqlSessionTemplate$SqlSessionInterceptor.invoke(SqlSessionTemplate.java:441)
at com.sun.proxy.$Proxy82.insert(Unknown Source)
at org.mybatis.spring.SqlSessionTemplate.insert(SqlSessionTemplate.java:272)
...
解决办法 直接在字段名上加上 typeHandler=FastjsonTypeHandler.class
@TableField(typeHandler = FastjsonTypeHandler.class)
//如
@TableField(value = "body", typeHandler = FastjsonTypeHandler.class)
private JSONObject body;
mybatis的话可以自定义FastjsonTypeHandler
毕竟FastjsonTypeHandler也是mybatis plus自定义的 继承了mybatis的BaseTypeHandler 实现了TypeHandler
然后又出现错误
column "xxx" is of type jsonb but expression is of type character varying
建议:You will need to rewrite or cast the expression.
在配置文件数据库url地址后加上"&stringtype=unspecified"即可