码迷,mamicode.com
首页 > 数据库 > 详细

mybatis_动态sql 查询、更新

时间:2015-03-15 22:50:05      阅读:189      评论:0      收藏:0      [点我收藏+]

标签:

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>

 

mybatis_动态sql 查询、更新

标签:

原文地址:http://www.cnblogs.com/zhangshiwen/p/4340464.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!