Mybatis處理select中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>

第二種方式:後面跟<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>

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章