关于#mybatis#的if标签

问题遇到的现象和发生背景

mybatis 增加一个if标签时就查询不到结果

增加type字段的if标签时
<select id="findInOutListByPage" resultType="com.ntvu.entity.InOut">
    select tio.*, tg.`goodName` as goodName
    from t_in_out tio
    inner join t_good tg on tg.`id` = tio.`goodId`
    <where>
        <if test="goodName != null and goodName != ''">
            and tg.goodName like concat('%',#{goodName},'%')
        if>
        <if test="employeeName != null and employeeName != ''">
            and tio.employeeName like concat('%',#{employeeName},'%')
        if>
        <if test="startDate!=null">
            =#{startDate}]]>
        if>
        <if test="endDate!=null">
            
            {endDate}]]>
        if>
        <if test="type != null">
            and tio.type = #{type}
        if>
    where>
    order by tio.id = #{id}
select>
运行结果

img

把type字段的if标签注释掉就可以查到结果了

img

我想要达到的结果

为什么会这样

order by tio.id = #{id} 排序为啥要这样写呢,还要传参数?

这不就是数据库没有符合其它条件并且 type = 0的数据呗,有什么问题吗?

把条件改成 这样也可以查询到结果

            <if test="type == 1 or type ==2">
                and tio.type = #{type}
            </if>

那就是你传了type值导致where 条件加了 and tio.type = #{type}

你多了一个条件 没找到符合条件的数据

type=0没查到数据,type=1.type=2查得到数据,这有啥问题

因为type=0没有数据

未找到符合条件的记录。

先单独下表 t_in_out 中没有 type = 0的记录?

你传过来的字段名也叫type吗?不叫的话映射不上啊

看下这篇博客,也许你就懂了,链接:mybatis if标签字符串判断