看到这个代码的时候我在想如果第一个if语句成立,那么where后面不就直接跟and了吗
所以上网查了一下,发现是这么处理的
第一种方式:后面跟1=1,使条件绝对成立:
<select id="selectStudent">
select * from student where 1=1
<if test=" id !=null and id !='' ">
and id = #{id}
</if>
<if test=" name !=null and name !='' ">
and name = #{name}
</if>
</select>
第二种方式:后面跟标签,将第一个and过滤(不会过滤第二个):
<select id="selectStudent">
select * from student where
<where>
<if test=" id !=null and id !='' ">
and id = #{id}
</if>
<if test=" name !=null and name !='' ">
and name = #{name}
</if>
</where>
</select>