org.springframework.jdbc.BadSqlGrammarException: ### Error querying database. Cause: org.postgresql.util.PSQLException: ERROR: operator does not exist: bigint = character varying

2023-02-18,,,,

1、报错信息

org.springframework.jdbc.BadSqlGrammarException:
### Error querying database. Cause: org.postgresql.util.PSQLException: ERROR: operator does not exist: bigint = character varying
建议:No operator matches the given name and argument types. You might need to add explicit type casts.
位置:334
### The error may exist in org/springblade/develop/mapper/ModelPrototypeMapper.java (best guess)
### The error may involve defaultParameterMap
### The error occurred while setting parameters
### SQL: SELECT id, model_id, jdbc_name, jdbc_type, property_name, property_type, property_entity, comment, is_list, is_form, is_row, component_type, dict_code, is_required, is_query, query_type, create_user, create_dept, create_time, update_user, update_time, status, is_deleted FROM blade_model_prototype WHERE is_deleted = 0 AND (model_id = ?)
### Cause: org.postgresql.util.PSQLException: ERROR: operator does not exist: bigint = character varying
建议:No operator matches the given name and argument types. You might need to add explicit type casts.
位置:334
; bad SQL grammar []; nested exception is org.postgresql.util.PSQLException: ERROR: operator does not exist: bigint = character varying
建议:No operator matches the given name and argument types. You might need to add explicit type casts.
位置:334
at org.springframework.jdbc.support.SQLStateSQLExceptionTranslator.doTranslate(SQLStateSQLExceptionTranslator.java:101)

2、问题分析

org.springframework.jdbc.BadSqlGrammarException:
### Error querying database. Cause: org.postgresql.util.PSQLException: ERROR: operator does not exist: bigint = character varying

场景:在一次开发过程中,使用 Mybatis-plus 自动生成的代码时,生成完成后调试过程中出现的报错信息。

此问题出现的原因:部分数据库在执行 sql 过程中,不支持部分字段类型的自动转换

使用过程中实际报错的场景:使用的数据库是 postgresql 数据库,报错字段是 modelId (数据库中是 model_id), 在数据库中是 int8 类型的。

而在controller 层接收参数 modelId 是 String 类型的,所以在执行 新增的操作时,出现了数据库层面的报错。

实际就是 modelId 字段类型与数据库不一致错误。

3、解决方案

我解决问题的处理,就是在 controller 层,将参数 id 改为 Long 类型,报错就解决了。

实际解决问题的过程,要排查 controller 中的参数类型,并排查中间操作的是否有类型转换的操作,以及 实体类 entity 中的类型与数据库是否一致。

org.springframework.jdbc.BadSqlGrammarException: ### Error querying database. Cause: org.postgresql.util.PSQLException: ERROR: operator does not exist: bigint = character varying的相关教程结束。

《org.springframework.jdbc.BadSqlGrammarException: ### Error querying database. Cause: org.postgresql.util.PSQLException: ERROR: operator does not exist: bigint = character varying.doc》

下载本文的Word格式文档,以方便收藏与打印。