mybatis动态sql的书写

时间:2022-10-11 05:11:13

God's blog

梦只在昨天,今天仍需努力

【免费公开课】Gulp前端自动化教程   走进VR开发世界——我们离开发一款VR大作还有多远?   CSDN发福利啦!C币、京东卡、现金任你选    <iframe width="300" height="250" align="center,center" id="cpro_u2392861_iframe" src="//pos.baidu.com/fcim?sz=300x250&amp;rdid=2392861&amp;dc=2&amp;di=u2392861&amp;dri=0&amp;dis=0&amp;dai=1&amp;ps=650x1340&amp;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&amp;dcb=BAIDU_SSP_define&amp;dtm=BAIDU_DUP_SETJSONADSLOT&amp;dvi=0.0&amp;dci=-1&amp;dpt=none&amp;tsr=0&amp;tpr=1464181549248&amp;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&amp;ari=1&amp;dbv=2&amp;drs=3&amp;pcs=1351x661&amp;pss=1351x4065&amp;cfv=11&amp;cpl=17&amp;chi=2&amp;cce=true&amp;cec=UTF-8&amp;tlm=1464152749&amp;ltu=http%3A%2F%2Fblog.csdn.net%2Fa600423444%2Farticle%2Fdetails%2F6658411&amp;ecd=1&amp;psr=1366x768&amp;par=1366x728&amp;pis=-1x-1&amp;ccd=24&amp;cja=true&amp;cmi=23&amp;col=zh-CN&amp;cdo=-1&amp;tcn=1464181549&amp;qn=f64a6847bdc36540&amp;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"></iframe>关闭

MyBatis动态SQL

标签: blogemailsqlnull优化 18211人阅读 评论(2) 收藏 举报 mybatis动态sql的书写分类: 作者同类文章X
    动态SQL
    MyBatis的动态SQL,解决了SQL字符串拼接的痛苦。

    1.if
    [html] view plain copy print?
    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>  


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


    2.choose,when,otherwise
    [html] view plain copy print?
    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 * FROM BLOG WHERE
    如果仅有第二个匹配,则会变成SELECT * FROM BLOG WHERE AND title LIKE somelike
    显然这样会查询失败。要解决这个问题,mybatis提供了解决方法。
    [html] view plain copy print?
    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>  


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


    3.set
    [html] view plain copy print?
    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>  


    同上的问题,优化后:
    [html] view plain copy print?
    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>