1)sql where 条件
select id="find" parameterType="User" resultType="User">
	select id,name, age,address from user_c where 1=1
	<if test="id!=null">
		and id=#{id}
	</if>
	<if test="name!=null">
		and name like "%"#{name}"%"
	</if>
	<if test="age!=null">
		and age=#{age}
	</if>
	<if test="address!=null">
		and address=#{address}
	</if>
</select>

 

2)<where>…</where> 会自动将sql中的 and 去掉,就无需写 where 1=1

 

select <include refid="cols"/> from user_c

<where>
	<if test="name!=null">
	 and name like "%"#{name}"%"
	</if>
	
	<if test="age!=null">
	 and age=#{age}
	</if>
	
	<if test="address!=null">
	 and address like #{address}
	</if>
</where>

 3)<set>…</set> 标签
会将update sql中的 set 最后一个条件多余的逗号去掉

<update id="update" parameterType="User">
	update user_c
	<set>
	<if test="name!=null">
	and name like "%"#{name}"%“,
	</if>
	
	<if test="age!=null">
	age=#{age},
	</if>
	
	<if test="address!=null">
	address=#{address},
	</if>
	</set>
	
	where id=#{id}
</update>

 

posted on 2015-03-15 21:25  张释文  阅读(498)  评论(0编辑  收藏  举报