日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

最常用的动态sql语句梳理Mybatis(转)

發(fā)布時間:2023/12/9 编程问答 24 豆豆
生活随笔 收集整理的這篇文章主要介紹了 最常用的动态sql语句梳理Mybatis(转) 小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.

?

? 公司項目中一直使用Mybatis作為持久層框架,自然,動態(tài)sql寫得也比較多了,最常見的莫過于在查詢語句中使用if標簽來動態(tài)地改變過濾條件了。Mybatis的強大特性之一便是它的動態(tài)sql,免除了拼接sql帶來的各種麻煩,在開發(fā)項目的過程中,常見的和不常見的你都有可能會用到,現(xiàn)在就來把這一塊總結(jié)一下。

  •   if
  •   choose(when,otherwise)
  •   trim(where,set)
  •   foreach

if

<select id="getCategory" parameterType="EshopShopCategory" resultMap="EshopCategory" >SELECT * from MALLT_SHOP_CATEGORY t WHERE (1=1)<if test="eshopShopCategory.shopCategoryId!=null">AND t.shop_category_id =#{eshopShopCategory.shopCategoryId} </if> <if test="eshopShopCategory.shopCategoryName!=null"> AND t.SHOP_CATEGORY_NAME like '%${eshopShopCategory.shopCategoryName}%' </if> <if test="eshopShopCategory.shopId==null"> AND t.shop_id=0 </if> ORDER BY SEQUENCE_NO </select>

  這通常用于多條件組合查詢。

<insert id="addProductCategory" parameterType="EshopShopCategory">insert into MALLT_SHOP_CATEGORY(<if test="shopCategoryName!=null and shopCategoryName!='' ">shop_category_name,</if><if test="shopId!=null and shopId!=''"> shop_id, </if> ADD_TIME) values(   <if test="shopCategoryName!=null and shopCategoryName!=''">   #{shopCategoryName,jdbcType=VARCHAR},   </if>   <if test="shopId!=null and shopId!=''">   #{shopId,jdbcType=NUMERIC},   </if>   current_timestamp ) </insert> 這適用于數(shù)據(jù)庫有默認值的時候可以不讓插入空值。 <update id="updateProductCategory" parameterType="EshopShopCategory" >update MALLT_SHOP_CATEGORY t set <if test="shopCategoryName!=null">t.shop_category_name=#{shopCategoryName,jdbcType=VARCHAR},</if><if test="updateUser!=null">t.update_user=#{updateUser,jdbcType=VARCHAR} ,</if>t.update_time=current_timestampwhere t.shop_category_id=#{shopCategoryId,jdbcType=NUMERIC} </update>

這條動態(tài)地修改語句用得非常多,是因為很多時候我們在做修改操作時并不確定到底要修改哪些字段(哪些屬性),可能有的需要保存原值不變,這時候就可以做動態(tài)的sql,你新建一個對象后將需要修改的字段附上新值,這樣不用修改的屬性在這個對象上表現(xiàn)地是null,調(diào)用這個動態(tài)的sql時便可以完成部分修改。

choose,when,otherwise

????? 適用場景:我們不想用到所有的條件語句,而只想從中擇其一二。針對這種情況,MyBatis 提供了 choose 元素,它有點像 Java 中的 switch 語句。(我感覺它有點像提供多種條件規(guī)則時,而這些規(guī)則又可以綜合寫在一起時)

<select id="findActiveBlogLike" resultType="Blog">SELECT * FROM BLOG WHERE state = ‘ACTIVE’<choose><when test="title != null">AND title like #{title}</when><when test="author != null and author.name != null">AND author_name like #{author.name}</when><otherwise> AND featured = 1 </otherwise> </choose> </select>

到目前為止,我還沒有用到過choose,以后多留意。

trim,where,set

為了避免當if動態(tài)條件都不成立時,或者第一個條件不成立第二個條件成立時出現(xiàn)諸如"select * from TableA where"或者"select * from TableA and where"病態(tài)sql,我們可以使用trim,where,set標簽來解決。

<select id="findActiveBlogLike" resultType="Blog">SELECT * FROM BLOG <where> <if test="state != null"> state = #{state} </if> <if test="title != null"> AND title like #{title} </if> <if test="author != null and author.name != null"> AND author_name like #{author.name} </if> </where> </select>

在實際應用中,我通常是不寫where標簽,而在where關鍵字之后加上1=1的條件。即不管有無動態(tài)條件,總可以得到完整的sql:select * from A where 1=1。。。

<update id="updateAuthorIfNecessary">update Author<set><if test="username != null">username=#{username},</if><if test="password != null">password=#{password},</if><if test="email != null">email=#{email},</if> <if test="bio != null">bio=#{bio}</if> </set> where id=#{id} </update>

foreach

foreach有時候在項目中會遇到,而且不止一次,用的時候是需要動點腦子的。通常用于篩選出在多個值組成的一個集合中或者排除多個值的場景,說白了,也就是我們之前寫sql時用到in、not in的時候:(集合是動態(tài)不確定的,需要從前臺傳值過來)

<select id="selectNumInOrder" resultType="String">select count(0) from eshop_order a left join eshop_order_item b on a.ORDER_ID = b.ORDER_IDwhere a.STATUS in ('1','2','3','5') <if test="list.size() > 0">and b.PHONE_NUM in <foreach item="numberList" collection="list" open="(" separator="," close=")"> #{numberList.num} </foreach> </if> </select> <select id="selectPostIn" resultType="domain.blog.Post">SELECT *FROM POST PWHERE ID in<foreach item="item" index="index" collection="list"open="(" separator="," close=")">#{item}</foreach> </select>

foreach 元素的功能是非常強大的,它允許你指定一個集合,聲明可以用在元素體內(nèi)的集合項和索引變量。它也允許你指定開閉匹配的字符串以及在迭代中間放置分隔符。這個元素是很智能的,因此它不會偶然地附加多余的分隔符。

注意 你可以將一個 List 實例或者數(shù)組作為參數(shù)對象傳給 MyBatis,當你這么做的時候,MyBatis 會自動將它包裝在一個 Map 中并以名稱為鍵。List 實例將會以“l(fā)ist”作為鍵,而數(shù)組實例的鍵將是“array”。

以上是結(jié)合http://mybatis.github.io/mybatis-3/zh/getting-started.html及自己開發(fā)中比較常用的總結(jié)出來的,今天給梳理一下,分享給大家!

行走在設計師的路上! http://www.cnblogs.com/yolanda-lee/p/4552514.html

總結(jié)

以上是生活随笔為你收集整理的最常用的动态sql语句梳理Mybatis(转)的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯,歡迎將生活随笔推薦給好友。