1. 程式人生 > >**mybatis處理SQL查詢中的where後面and常用的2種方法**

**mybatis處理SQL查詢中的where後面and常用的2種方法**

<!-- 1:後面跟1=1 決對成立-->
	<select id="queryPersonByidAndNo">
		select * from person where 1=1
		<if test=" id !=null and id !='' ">
			 and id = #{id}
		</if>
		<if test=" id !=null and id !='' ">
			and  no = #{no}
		</if>
	</select>
	<!-- 2 :後面跟where標籤,會將第一個and過濾(不會過濾第二個)-->
	<select id="queryPersonByidAndNo">
		select * from person where
		<where>
			<if test=" id !=null and id !='' ">
			 and id = #{id}
			</if>
			<if test=" id !=null and id !='' ">
				and  no = #{no}
			</if>
		</where>
	</select>