if test=""的坑
原来接口
<select id="getSuperviseContentByType" resultType="java.util.Map">
select *
from supervise_accident_content sac
<where>
<choose>
<when test="accidentType !='0'">
and sac.accident_catalogy=#{accidentType}
</when>
<when test="accidentType ='0'">
and 1=1
</when>
<otherwise>
</otherwise>
</choose>
</where>
</select>
结果就是不管传什么值,传0,1,2最后打印的都是
select * from supervise_accident_content sac WHERE sac.accident_catalogy=? 0or1or2
该问题所在
不管如果传值,最后只走accidentType!='0’这一步
多方尝试
when test=“accidentType !=‘0’”>
and sac.accident_catalogy=#{accidentType}改为了
when test=“accidentType !=0”>
and sac.accident_catalogy=#{accidentType}仔细看看上面不同,0的单引号取消了
这里提醒一下accidentType的类型为String类型,所以我当时最开始写将0加上单引号
最后就好了。。。结论:
像if test这种+when test这种。
判断是否为某个值的时候,就算该值为String类型也不要加引号。
就为错的
候,就算该值为String类型也不要加引号。
就为错的
就为对的