当前使用版本(必填,否则不予处理)
3.3.1
该问题是如何引起的?(确定最新版也有问题再提!!!)
我使用的数据库是PostgreSQL 10.5,其支持jsonb类型的字段。这个类型是可以进行关联查询和设立索引的。 我尝试使用JacksonTypeHandler之后,提示我插入的类型是varchar。后来我查看了其中的代码,其中假设了总是使用String类型来处理。这样的话就,使用json的意义就只变成了方便序列化和反序列化对象和字段之间的映射。 事实上Mysql在5.7开始也增加了对json的支持。还是希望可以以一种优雅的方式来解决这种问题。
重现步骤(如果有就写完整)
报错信息
2020-05-06 18:37:25.368 [http-nio-9998-exec-1] ERROR c.y.f.m.s.s.service.GlobalDefaultExceptionHandler - org.springframework.jdbc.BadSqlGrammarException:
Error updating database. Cause: org.postgresql.util.PSQLException: 错误: 字段 "config" 的类型为 jsonb, 但表达式的类型为 character varying
建议:你需要重写或转换表达式 位置:96
The error may exist in com/ym/foundation/management/sensor/status/dao/ModelStatusConfigDao.java (best guess)
The error may involve com.ym.foundation.management.sensor.status.dao.ModelStatusConfigDao.insert-Inline
The error occurred while setting parameters
SQL: INSERT INTO sensor_status_model_config ( record_id, sensor_model, config ) VALUES ( ?, ?, ? )
Cause: org.postgresql.util.PSQLException: 错误: 字段 "config" 的类型为 jsonb, 但表达式的类型为 character varying
建议:你需要重写或转换表达式 位置:96 ; bad SQL grammar []; nested exception is org.postgresql.util.PSQLException: 错误: 字段 "config" 的类型为 jsonb, 但表达式的类型为 character varying 建议:你需要重写或转换表达式 位置:96 2020-05-06 18:37:25.369 [http-nio-9998-exec-1] WARN o.s.w.s.m.m.a.ExceptionHandlerExceptionResolver - Resolved exception caused by Handler execution: org.springframework.jdbc.BadSqlGrammarException:
Error updating database. Cause: org.postgresql.util.PSQLException: 错误: 字段 "config" 的类型为 jsonb, 但表达式的类型为 character varying
建议:你需要重写或转换表达式 位置:96
The error may exist in com/ym/foundation/management/sensor/status/dao/ModelStatusConfigDao.java (best guess)
The error may involve com.ym.foundation.management.sensor.status.dao.ModelStatusConfigDao.insert-Inline
The error occurred while setting parameters
SQL: INSERT INTO sensor_status_model_config ( record_id, sensor_model, config ) VALUES ( ?, ?, ? )
Cause: org.postgresql.util.PSQLException: 错误: 字段 "config" 的类型为 jsonb, 但表达式的类型为 character varying
建议:你需要重写或转换表达式 位置:96 ; bad SQL grammar []; nested exception is org.postgresql.util.PSQLException: 错误: 字段 "config" 的类型为 jsonb, 但表达式的类型为 character varying 建议:你需要重写或转换表达式 位置:96
Comment From: miemieYaho
不支持定制,这是全数据库通用的
Comment From: linghuanxu
我并不是说要对PostgreSQL进行定制,而是让json这种数据结构的支持更加准确一些,这样人们在关系型数据库中对json这种数据结构的看法就会产生变化。事实上,从各大数据库的新版本来看,json成为关系型数据库的标准数据类型已经很近了。
Comment From: miemieYaho
你先去百度了解一下jdbc怎么使用pg的json再说
Comment From: linghuanxu
事实上这个TypeHandler我已经实现了,只是class实例引入的方向很恶心,让总线看看有没有更优雅的实现方式罢了。
Comment From: linghuanxu
顺便说,autoResultMap很好用
Comment From: miemieYaho
通用的需要jdbc直接进行支持
Comment From: linghuanxu
我明白数据库适配是个不符合Mybatis plus定位的工作。而我的实现也用到了PG自己的库,mybatis的jdbc枚举里没有json。但,我想,如果只实现一两个有兴趣实现的数据库适配,更多的是把适配的接口流出来让使用者自己实现就可以了。 我刚才有细看了下AbstractJsonTypeHandler和它的子类,明天我试试自己写AbstractJsonTypeHandler的子类能不能跑通吧,能正常工作的话。我的问题就很好的解决了。
Comment From: hong4cong
我明白数据库适配是个不符合Mybatis plus定位的工作。而我的实现也用到了PG自己的库,mybatis的jdbc枚举里没有json。但,我想,如果只实现一两个有兴趣实现的数据库适配,更多的是把适配的接口流出来让使用者自己实现就可以了。 我刚才有细看了下AbstractJsonTypeHandler和它的子类,明天我试试自己写AbstractJsonTypeHandler的子类能不能跑通吧,能正常工作的话。我的问题就很好的解决了。
@linghuanxu 解决了吗?
Comment From: aboutZZ
可在SQL语句中强转类型为json或jsonb, 我想这个应该是能实现的,看作者愿不愿意改了
例如:
INSERT INTO room_categories (name, code, metadata) VALUES (?, ?, ? ::jsonb )