使用mybatis过程中 mapper.xml中的if判断
需求:查询状态为2时,查询出状态为2,4,5的数据:
当传入的参数为String类型的数字时,错误示范:
<if test="state!=null and state!='' and state!='2'">
AND a.catastate = #{state}
</if>
<if test="state!=null and state!='' and '2'==state">
AND a.catastate in ('2','4','5')
</if>
mapper.xml直接将2识别为数字,实际上state为String类型,所以需要做一个toString()
正确示范
<if test="state!=null and state!='' and state.toString()!='2'.toString()">
AND a.catastate = #{state}
</if>
<if test="state!=null and state!='' and '2'.toString()==state.toString()">
AND a.catastate in ('2','4','5')
</if>
如此才能在查询状态为2是查出状态为2,4,5的数据