第一種方式:後面跟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>
第二種方式:後面跟<where>標籤,將第一個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>