【问题标题】:Using MyBatis Update with foreach将 MyBatis 更新与 foreach 一起使用
【发布时间】:2015-06-15 08:15:16
【问题描述】:

我正在为 MyBatis 中的更新语句而苦苦挣扎。我想将多个字符串放在一行中:

UPDATE MY_FILTERS
SET
GROUPS =
  <foreach item="item" collection="selectedGroups" open="" separator="," close="">
    #{item}
  </foreach>,
TEMPLATES = 
  <foreach item="item" collection="selectedTemplates" open="" separator="," close="">
    #{item}
  </foreach>
where ID = #{id}

我尝试了以下替代方案:

open = "'" close = "'"

separator = "','"    

但仍然出现这样的异常:

org.springframework.jdbc.BadSqlGrammarException: 
### Error updating database. Cause: java.sql.SQLException: ORA-01747: invalid user.table.column, table.column, or column specification 
### The error may involve my.mybatis.PRPMapper.saveParamsToDB-Inline 
### The error occurred while setting parameters 
### SQL: UPDATE MY_FILTERS SET GROUPS = ? , ? , TEMPLATES = ? , ? where ID = ? 
### Cause: java.sql.SQLException: ORA-01747: invalid user.table.column, table.column, or column specification ; bad SQL grammar []; nested exception is java.sql.SQLException: ORA-01747: invalid user.table.column, table.column, or column specification javax.faces.event.AbortProcessingException: org.springframework.jdbc.BadSqlGrammarException: 

【问题讨论】:

    标签: sql oracle mybatis


    【解决方案1】:

    试试

    open = "'" close = "'"
    

    separator = ","  
    

    ${item}
    

    有了这些设置,mybatis 不会绑定变量,而是执行字符串替换。请注意,字符串替换容易受到 sql 注入的影响。如果您遇到更多问题,请发布由 mystatis 构建的更新后的 sql 查询

    【讨论】:

    • 感谢您的回复。我得到:org.springframework.jdbc.InvalidResultSetAccessException: ### Error updating database. Cause: java.sql.SQLException: Invalid column index ### The error may involve my.mybatis.PRPMapper.saveParamsToDB-Inline ### The error occurred while setting parameters ### SQL: UPDATE MY_FILTERS SET GROUPS = ' ? ',' ? ' , TEMPLATES = ' ? ' where ID = ? ### Cause: java.sql.SQLException: Invalid column index ; invalid ResultSet access for SQL []; nested exception is java.sql.SQLException: Invalid column index javax.faces.event.AbortProcessingException: ...
    • @Andrej:你说得对,我错过了单引号转义;我更新了回复。你也用过 ${item} 吗?
    • 谢谢,首先是我的错误,我没有使用$。最后它适用于open="'" separator="," close="'"
    猜你喜欢
    • 1970-01-01
    • 2015-04-24
    • 2014-05-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-08-25
    • 1970-01-01
    • 2017-04-15
    相关资源
    最近更新 更多