首页 > 解决方案 > 更新数据库时出错!表或视图不存在

问题描述

我有这个查询

<update id="updateRow">
    update
      ROW
    set
      DISPLAY_ID = #{displayId}
    <where>
      <include refid="rowCriteria" />
    </where>
</update>

 <sql id="rowCriteria">
  <if test="criteria.displayId} != null">
     DISPLAY_ID=#{criteria.displayId}
  </if>
  <if test="criteria.id != null">
    and ID=#{criteria.id}
  </if>
 </sql>

以及在哪里执行

 Preparing: update ROW set DISPLAY_ID = ? WHERE ID=? 

 Parameters: 72312(Long), 72991(Long)

我得到这个错误

该错误可能涉及defaultParameterMap

设置参数时出错

SQL:更新 ROW 集 DISPLAY_ID = ? 在哪里 ID =?

原因:java.sql.SQLSyntaxErrorException:ORA-00942:表或视图不存在

但是如果我从标准 ID 中删除“和”

 <sql id="rowCriteria">
  <if test="criteria.displayId} != null">
    DISPLAY_ID=#{criteria.displayId}
  </if>
  <if test="criteria.id != null">
    ID=#{criteria.id}
  </if>
 </sql>

查询运行正确并执行更新

我的问题是,如果我在标准中有and 键,为什么有时更新会正确运行,但有时会出现错误?

标签: jquerydatabasemybatis

解决方案


两件事情:

  • 删除}多余的。

  • 在每个条件前加上AND. < where > 标签将始终删除第一个标签。

    AND DISPLAY_ID=#{criteria.displayId} 和 ID=#{criteria.id}


推荐阅读