<!-- 查询物品的id -->
<select id="checkItemsId" parameterType="pd" resultType="java.lang.Integer">
SELECT
i.itemsid
FROM pq_goods_items i
<where>
<!--方式一使用choose的方式查询-->
<!-- <choose>
<when test="parentId !=0 ">parentTypeId=#{parentId}</when>
<when test="parentId==0">parentTypeId is null</when>
</choose> -->
<!--方式二使用if的方式查询-->
<if test="color!=null">
i.personone=#{personone}
AND i.persontwo=#{persontwo}
AND i.color=#{color}
</if>
<if test="color==null">
i.personone=#{personone}
AND i.persontwo=#{persontwo}
AND i.color is null
</if>
</where>
</select>
需要注意的是 使用了where标签以后,sql中不在使用where字段来限制条件
如果判断条件有多个 中间用 and 表示并列
<if test="color!=null and personone!=null">
本文介绍了一个使用MyBatis实现的动态SQL查询案例,通过`if`条件判断来灵活构建查询语句,适用于根据不同参数进行商品ID的查询场景。
8万+

被折叠的 条评论
为什么被折叠?



