MyBatis動態SQL報錯“badSql”,如何修改SQL語句使其正確執行?

MyBatis動態SQL報錯“badSql”,如何修改SQL語句使其正確執行?

mybatis動態sql報錯征解

在使用mybatis進行動態sql操作時,遇到報錯提示“badsql”,可能的原因是sql語句存在語法錯誤。

針對提供的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語句的語法正確,可以正常執行。

? 版權聲明
THE END
喜歡就支持一下吧
點贊7 分享