在使用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