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