【问题标题】:what is the best way to handle reserved key words of PostgreSQL when using Mybatis使用Mybatis时处理PostgreSQL保留关键字的最佳方法是什么
【发布时间】:2021-07-04 06:50:04
【问题描述】:

当我在 PostgreSQL 13 中创建 namepassword 等列时。PostgreSQL 可能将其视为保留关键字。所以 MyBatis SQL 会这样:

INSERT INTO apple_server_notification_record (created_time, updated_time, notification_type, auto_renew_product_id, auto_renew_status, auto_renew_status_change_date, auto_renew_status_change_date_ms, auto_renew_status_change_date_pst, environment, `password`, bid, bvrs) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)

它会在 SQL 中自动添加 ``。但是在 PostgreSQL 13 中执行 sql 时显示此错误:

org.springframework.jdbc.BadSqlGrammarException: 
### Error updating database.  Cause: org.postgresql.util.PSQLException: ERROR: syntax error at or near "`"
  Position: 249
### The error may exist in class path resource [mybatis/mapper/dolphin/AppleServerNotificationRecordMapper.xml]
### The error may involve com.dolphin.soa.post.dao.AppleServerNotificationRecordMapper.insertSelective-Inline
### The error occurred while setting parameters
### SQL: INSERT INTO apple_server_notification_record (created_time, updated_time, notification_type, auto_renew_product_id, auto_renew_status, auto_renew_status_change_date, auto_renew_status_change_date_ms, auto_renew_status_change_date_pst, environment, `password`, bid, bvrs) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)
### Cause: org.postgresql.util.PSQLException: ERROR: syntax error at or near "`"
  Position: 249
; bad SQL grammar []; nested exception is org.postgresql.util.PSQLException: ERROR: syntax error at or near "`"
  Position: 249

我可以通过将列 password 更改为 apple_auth_password 来解决它,我知道它会解决问题。但是在我的代码中,我必须在接收来自苹果的请求中显式转换映射。我的实体定义如下:

private String password;

有什么好的建议可以解决这个问题吗?遇到这个问题你会怎么做?

【问题讨论】:

  • 您在此处显示反引号。您看到的这些真的是单引号吗?
  • 反引号对于在 Postgres 中引用标识符是没有用的。它们原生于 MySQL 和 MariaDB。对于 Postgres(和标准 SQL),必须使用双引号来引用标识符。

标签: java postgresql spring-mybatis quoted-identifier


【解决方案1】:

您可以更改 Mybatis-generator 的自动生成器配置代码配置:

  <property name="autoDelimitKeywords" value="true"/>
    <!--
    the beginningDelimiter and endingDelimiter using " by default
    when using PostgreSQL using "
    when using MySQL, we should change to `
     -->
    <property name="beginningDelimiter" value="&quot;"/>
    <property name="endingDelimiter" value="&quot;"/>

【讨论】:

    猜你喜欢
    • 2015-03-28
    • 1970-01-01
    • 2010-09-06
    • 2021-11-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多