**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>

猜你喜欢

转载自blog.csdn.net/qq_37296212/article/details/83178450