网站开发 · 2024年11月14日

MyBatis动态SQL报错“badSql”,如何修改SQL语句使其正确执行?

MyBatis动态SQL报错“badSql”,如何修改SQL语句使其正确执行?插图

mybatis动态sql报错征解

在使用mybatis进行动态sql操作时,遇到”badsql”,可能的原因是存在语法错误。

针对提供的sql语句:

select * from table a 
<where>
 a.project_id=#{projectid}
 and a.id != #{id}
 and a.status=3 
<choose>
    <when test="type == idcard"> 
        and a.id_card = #{code}
    </when>
<when test="type == unitcode">and a.unit_code = #{code}</when>
    <otherwise>  
    </otherwise>  
</choose>
</where>

登录后复制

修改后,正确的sql语句应该如下:

select * from table a
<where>
 a.project_id=#{projectId}
 and a.id != #{id}
 and a.status=3 
<choose>
    <when test="type == 'idCard'"> 
        and a.id_card = #{code}
    </when>
    <when test="type == 'unitCode'"> 
        and a.unit_code = #{code}
    </when>
    <otherwise>  
    </otherwise>  
</choose>
</where>

登录后复制

修改后的部分包括:

  • 将test=’type == idcard’修改为test=”type == ‘idcard'”,添加了引号。
  • 将test=’type == unitcode’修改为test=”type == ‘unitcode'”,添加了引号。
  • 添加了 语句块。

修改后,sql语句的语法正确,可以正常执行。

以上就是MyBatis动态SQL报错“badSql”,如何修改SQL语句使其正确执行?的详细内容,更多请关注米云网其它相关文章!