MybatisPlus PostgreSQL org.postgresql.util.PSQLException: ERROR: column "demo_json" is of type jsonb but expression is of type character varying
环境:
Group ID: com.baomidou Artifact ID: mybatis-plus Version: 3.5.11
Group ID: org.postgresql Artifact ID: postgresql Version: 42.7.5
错误内容:
### Error updating database. Cause: org.postgresql.util.PSQLException: ERROR: column "user_agent" is of type jsonb but expression is of type character varying
建议:You will need to rewrite or cast the expression.
位置:266
在使用MybatisPlus保存json字符串到表字段(字段类型为jsonb)中,出现了莫名其妙的问题。
解决方法很简单,就是在db连接字符串中加上stringtype=unspecified参数即可:
spring:
datasource:
url: jdbc:postgresql://localhost:5432/dbname?stringtype=unspecified
有关stringtype的作用是:
指定绑定通过 setString() 设置的 PreparedStatement 参数时使用的类型。如果 stringtype 设置为 VARCHAR(默认),
此类参数将作为 varchar 参数发送到服务器。如果 stringtype 被设置为 unspecified,参数将以无类型值的形式发送到服务器,
服务器将尝试推断出适当的类型。如果现有应用程序使用 setString() 来设置实际上是其他类型(如整数)的参数,
并且无法更改应用程序以使用适当的方法(如 setInt()),则此方法非常有用。
参考资料:
Java连接PostgreSQL数据库的jdbc连接参数的参考链接
postgresql json类型
postgresql json操作符
postgresql github仓库地址
ERROR: column is of type json but expression is of type character varying in Hibernate
PostgreSQL throws "column is of type jsonb but expression is of type bytea" with JPA and Hibernate
Resolving PostgreSQL JSON Type Mismatch Errors in JPA
Mybatis PostgreSql jsonb类型适配,在这里可以看到是已有年份的问题了哈
复制请注明出处,在世界中挣扎的灰太狼

浙公网安备 33010602011771号