MyBatis 中if test判断数字
在实现搜索功能时碰到的错误,很多时候我们会在数据库中使用一个字段来作为状态保存,如:0=男,1=女;0=禁止,1=启用等。
在这里插入图片描述
无论我选择停用、还是启用,都将整个表格的数据显示出来,没有起到筛选的作用。通过排除,找到了导致问题的代码:
<select id="queryAllByLimit" resultMap="SystemMenuMap">
select
id, pid, title, icon, href, sort, status
from system_menu
<where>
<if test="systemMenu.status != null and systemMenu.status != '' ">
and status = #{systemMenu.status}
</if>
</where>
</select>
改为:
<select id="queryAllByLimit" resultMap="SystemMenuMap">
select
id, pid, title, icon, href, sort, status
from system_menu
<where>
<if test="systemMenu.status != null">
and status = #{systemMenu.status}
</if>
</where>
</select>
原因:
<if test="systemMenu.status != null and systemMenu.status != '' ">
只对判断String类型的字符串时起作用,由于status是Integer类型,判断条件不成立,没起到作用。如:当传入的值是1时,’1’会被解析成字符。