mybatis动态sql的书写

width="300" height="250" align="center,center" id="cpro_u2392861_iframe" src="//pos.baidu.com/fcim?sz=300x250&rdid=2392861&dc=2&di=u2392861&dri=0&dis=0&dai=1&ps=650x1340&coa=at%3D3%26rsi0%3D300%26rsi1%3D250%26pat%3D17%26tn%3DbaiduCustNativeAD_xuanfu%26rss1%3D%2523FFFFFF%26conBW%3D1%26adp%3D1%26ptt%3D0%26titFF%3D%2525E5%2525BE%2525AE%2525E8%2525BD%2525AF%2525E9%25259B%252585%2525E9%2525BB%252591%26titFS%3D14%26rss2%3D%2523000000%26titSU%3D0&dcb=BAIDU_SSP_define&dtm=BAIDU_DUP_SETJSONADSLOT&dvi=0.0&dci=-1&dpt=none&tsr=0&tpr=1464181549248&ti=MyBatis%E5%8A%A8%E6%80%81SQL%20-%20God's%20blog%20-%20%E5%8D%9A%E5%AE%A2%E9%A2%91%E9%81%93%20-%20CSDN.NET&ari=1&dbv=2&drs=3&pcs=1351x661&pss=1351x4065&cfv=11&cpl=17&chi=2&cce=true&cec=UTF-8&tlm=1464152749&ltu=http%3A%2F%2Fblog.csdn.net%2Fa600423444%2Farticle%2Fdetails%2F6658411&ecd=1&psr=1366x768&par=1366x728&pis=-1x-1&ccd=24&cja=true&cmi=23&col=zh-CN&cdo=-1&tcn=1464181549&qn=f64a6847bdc36540&tt=1464181549230.22.137.147" frameborder="0" marginwidth="0" marginheight="0" scrolling="no" vspace="0" hspace="0" style="margin: 0px; border: 0px currentColor; border-image: none; vertical-align: bottom;" allowtransparency="true">
关闭

MyBatis动态SQL

标签: blogemailsqlnull优化
18211人阅读 评论(2) 收藏 举报
分类:
动态SQL
MyBatis的动态SQL,解决了SQL字符串拼接的痛苦。

1.if
  1. <select id="findActiveBlogWithTitleLike"  
  2.     parameterType="Blog" resultType="Blog">  
  3.     SELECT * FROM BLOG  
  4.     WHERE state = 'ACTIVE'  
  5.     <if test="title != null">  
  6.         AND title like #{title}  
  7.     </if>  
  8. </select>  
<select id="findActiveBlogWithTitleLike"
	parameterType="Blog" resultType="Blog">
	SELECT * FROM BLOG
	WHERE state = 'ACTIVE'
	<if test="title != null">
		AND title like #{title}
	</if>
</select>


这条一句会提供一个可选的文本查找功能。如果没有传递title,那么所有激活的博客都会被返回。
如果传递了title,那么就会查找相近的title。


2.choose,when,otherwise
  1. <select id="findActiveBlogLike"  
  2.     parameterType="BLOG" resultType="BLOG">  
  3.     SELECT * FROM BLOG  
  4.     WHERE  
  5.     <choose>  
  6.         <when test="title != null">  
  7.             AND title like #{title}  
  8.         </when>  
  9.         <when test="author != null and author.name != null">  
  10.             AND title like #{author.name}  
  11.         </when>  
  12.         <otherwise>  
  13.             AND featured = 1  
  14.         </otherwise>  
  15.     </choose>  
  16. </select>  
<select id="findActiveBlogLike"
	parameterType="BLOG" resultType="BLOG">
	SELECT * FROM BLOG
	WHERE
	<choose>
		<when test="title != null">
			AND title like #{title}
		</when>
		<when test="author != null and author.name != null">
			AND title like #{author.name}
		</when>
		<otherwise>
			AND featured = 1
		</otherwise>
	</choose>
</select>


注:如果上述条件都没有匹配,则会变成SELECT * FROM BLOG WHERE
如果仅有第二个匹配,则会变成SELECT * FROM BLOG WHERE AND title LIKE somelike
显然这样会查询失败。要解决这个问题,mybatis提供了解决方法。
  1. <select id="findActiveBlogLike"  
  2.     parameterType="BLOG" resultType="BLOG">  
  3.     SELECT * FROM BLOG  
  4.     WHERE  
  5.     <trim prefix="WHERE" prefixOverrides="AND |OR ">  
  6.         <choose>  
  7.             <when test="title != null">  
  8.                 AND title like #{title}  
  9.             </when>  
  10.             <when test="author != null and author.name != null">  
  11.                 AND title like #{author.name}  
  12.             </when>  
  13.             <otherwise>  
  14.                 AND featured = 1  
  15.             </otherwise>  
  16.         </choose>  
  17.     </trim>  
  18. </select>  
<select id="findActiveBlogLike"
	parameterType="BLOG" resultType="BLOG">
	SELECT * FROM BLOG
	WHERE
	<trim prefix="WHERE" prefixOverrides="AND |OR ">
		<choose>
			<when test="title != null">
				AND title like #{title}
			</when>
			<when test="author != null and author.name != null">
				AND title like #{author.name}
			</when>
			<otherwise>
				AND featured = 1
			</otherwise>
		</choose>
	</trim>
</select>


overrides属性采用管道文本分隔符来覆盖,这里的空白是重要的。它的结果就是移除在InnerText中overrides中指定的内容。


3.set
  1. <update id="updateAuthorIfNecessary"  
  2.     parameterType="Author">  
  3.     update Author  
  4.     <set>  
  5.         <if test="username != null">username=#{username},</if>  
  6.         <if test="password != null">password=#{password},</if>  
  7.         <if test="email != null">email=#{email}</if>  
  8.     </set>  
  9.     where id=#{id}  
  10. </update>  
<update id="updateAuthorIfNecessary"
	parameterType="Author">
	update Author
	<set>
		<if test="username != null">username=#{username},</if>
		<if test="password != null">password=#{password},</if>
		<if test="email != null">email=#{email}</if>
	</set>
	where id=#{id}
</update>


同上的问题,优化后:
  1. <update id="updateAuthorIfNecessary"  
  2.     parameterType="Author">  
  3.     update Author  
  4.     <trim prefix="where" prefixOverrides=",">   
  5.     <set>  
  6.         <if test="username != null">username=#{username},</if>  
  7.         <if test="password != null">password=#{password},</if>  
  8.         <if test="email != null">email=#{email}</if>  
  9.     </set>  
  10.     where id=#{id}  
  11.     </trim>  
  12. </update>  
<update id="updateAuthorIfNecessary"
	parameterType="Author">
	update Author
	<trim prefix="where" prefixOverrides=","> 
	<set>
		<if test="username != null">username=#{username},</if>
		<if test="password != null">password=#{password},</if>
		<if test="email != null">email=#{email}</if>
	</set>
	where id=#{id}
	</trim>
</update>


0
0
 
 
我的同类文章
http://blog.csdn.net 更多文章
猜你在找
width="728" height="90" align="center,center" id="iframeu1607657_0" src="//pos.baidu.com/fcim?sz=728x90&rdid=1607657&dc=2&di=u1607657&dri=0&dis=0&dai=2&ps=3485x287&coa=at%3D3%26rsi0%3D728%26rsi1%3D90%26pat%3D6%26tn%3DbaiduCustNativeAD%26rss1%3D%2523FFFFFF%26conBW%3D1%26adp%3D1%26ptt%3D0%26titFF%3D%2525E5%2525BE%2525AE%2525E8%2525BD%2525AF%2525E9%25259B%252585%2525E9%2525BB%252591%26titFS%3D%26rss2%3D%2523000000%26titSU%3D0%26ptbg%3D90%26piw%3D0%26pih%3D0%26ptp%3D0&dcb=BAIDU_SSP_define&dtm=BAIDU_DUP_SETJSONADSLOT&dvi=0.0&dci=-1&dpt=none&tsr=0&tpr=1464181549248&ti=MyBatis%E5%8A%A8%E6%80%81SQL%20-%20God's%20blog%20-%20%E5%8D%9A%E5%AE%A2%E9%A2%91%E9%81%93%20-%20CSDN.NET&ari=1&dbv=2&drs=3&pcs=1351x661&pss=1351x4065&cfv=11&cpl=17&chi=2&cce=true&cec=UTF-8&tlm=1464152749&ltu=http%3A%2F%2Fblog.csdn.net%2Fa600423444%2Farticle%2Fdetails%2F6658411&ecd=1&psr=1366x768&par=1366x728&pis=-1x-1&ccd=24&cja=true&cmi=23&col=zh-CN&cdo=-1&tcn=1464181549&qn=5bf5b01977c87ed8&tt=1464181549230.25.178.180" frameborder="0" marginwidth="0" marginheight="0" scrolling="no" vspace="0" hspace="0" style="margin: 0px; border: 0px currentColor; border-image: none; vertical-align: bottom;" allowtransparency="true">
查看评论
1楼 Wilson_Peng 2013-08-17 17:35发表 [回复]
所有的if判断都是判断非空,能否判断是否等于一个特定字符串呢?
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
  • 个人资料
    • 访问:685167次
    • 积分:9391
    • 等级:
    • 排名:第1166名
    • 原创:242篇
    • 转载:11篇
    • 译文:0篇
    • 评论:160条
  • 文章分类
  • 最新评论

提问

您的问题将会被发布在“技术问答”频道 ×
该问题已存在,请勿重复提问
src="http://ask.csdn.net/upload.html">
插入图片
| | | | | |
  
 
 
0 0 0:0
推荐标签:
我要悬赏
取消 发布
可能存在类似的问题:
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值