在foreach中使用MyBatis Update

时间:2015-06-15 08:15:17

标签: sql oracle mybatis

我在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: 

1 个答案:

答案 0 :(得分:0)

尝试

open = "'" close = "'"

separator = ","  

${item}

使用这些设置,mybatis不会绑定变量,但会执行字符串替换。请注意字符串替换容易受到sql注入的影响。如果您遇到更多问题,请发布mystatis

构建的更新的sql查询