使用mybatis的動態sql條件判斷時,需要使用
錯誤示例:
select * from table a where a.project_id=#{projectid} and a.id != #{id} and a.status=3 and a.id_card = #{code} or a.unit_code = #{code}
原因:
在
優化后的代碼:
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>
解釋:
優化后的代碼使用
? 版權聲明
文章版權歸作者所有,未經允許請勿轉載。
THE END