溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊(cè)×
其他方式登錄
點(diǎn)擊 登錄注冊(cè) 即表示同意《億速云用戶(hù)服務(wù)條款》

Mybatis中的動(dòng)態(tài)sql怎么利用OGNL表達(dá)式進(jìn)行處理

發(fā)布時(shí)間:2020-11-24 15:03:39 來(lái)源:億速云 閱讀:228 作者:Leah 欄目:編程語(yǔ)言

Mybatis中的動(dòng)態(tài)sql怎么利用OGNL表達(dá)式進(jìn)行處理?相信很多沒(méi)有經(jīng)驗(yàn)的人對(duì)此束手無(wú)策,為此本文總結(jié)了問(wèn)題出現(xiàn)的原因和解決方法,通過(guò)這篇文章希望你能解決這個(gè)問(wèn)題。

常用的Mybatis動(dòng)態(tài)sql標(biāo)簽有6種:

      1. if 語(yǔ)句 (簡(jiǎn)單的條件判斷)

      2. choose (when,otherwize) ,相當(dāng)于Java 語(yǔ)言中的 switch ,與 jstl 中的choose 很類(lèi)似.

      3. trim (對(duì)包含的內(nèi)容加上 prefix,或者 suffix 等,前綴,后綴)

      4. where (主要是用來(lái)簡(jiǎn)化sql語(yǔ)句中where條件判斷的,能智能的處理 and or ,不必?fù)?dān)心多余導(dǎo)致語(yǔ)法錯(cuò)誤)

      5. set (主要用于更新時(shí))

      6. foreach (在實(shí)現(xiàn) mybatis in 語(yǔ)句查詢(xún)時(shí)特別有用)

(1) if

模糊查詢(xún)

<select id="select1" resultType="BaseresultMap"> 
 SELECT * FROM User WHERE Age = ‘18' 
 <if test="name != null"> 
 AND name like #{name} 
 </if> 
</select> 

年齡18且可以模糊搜索姓名

(2)choose,when,otherwize

當(dāng)Job參數(shù)有傳入時(shí),就找出對(duì)應(yīng)工作的人,否則就找出Job為none的人,而不是所有人

<select id="select2" resultType="BaseresultMap"> 
 SELECT * FROM User WHERE Age = ‘18' 
 
 <choose> 
 <when test="Job != null"> 
 AND Job =#{Job} 
 </when> 
 <otherwise> 
 AND Job="none" 
 
 </otherwise> 
 </choose> 
</select> 

(3)foreach

 <select id="select5" resultType="BaseresultBase"> 
 select * from User where id in 
 <foreach collection="list" index="index" item="item" open="(" separator="," close=")"> 
  #{item} 
 </foreach> 
 </select> 
 
public List<User> select5(List<Integer> ids); 

(4) where set trim

where,set

為什么要用where,因?yàn)閱渭兊膶?xiě)where可能會(huì)導(dǎo)致 where And ... 和 where .....情況的發(fā)生,Set也是一樣的

當(dāng)然 trim 標(biāo)簽是萬(wàn)能的

<select id="select3" resultType="BaseresultMap"> 
 SELECT * FROM User 
<where> 
 
 <if test="Age != null"> 
 Age = #{Age} 
 </if> 
 <if test="Job != null"> 
 AND Job like #{Job} 
 </if> 
 
<where> 
</select> 
 
<update id="update1"> 
 update User 
 <set> 
 <if test="username != null">username=#{username},</if> 
 <if test="password != null">password=#{password},</if> 
 <if test="Age != null">Age =#{Age}</if> 
 </set> 
 where id=#{id} 
</update> 
<pre code_snippet_id="2048504" snippet_file_name="blog_20161214_2_7439616" class="prettyprint lang-xml" name="code"><pre code_snippet_id="2048504" snippet_file_name="blog_20161214_2_7439616" name="code" class="html"><pre code_snippet_id="2048504" snippet_file_name="blog_20161214_2_7439616"></pre> 
<pre></pre> 
<pre></pre> 
<p></p> 
<pre></pre> 
<pre></pre> 
<pre></pre> 
<pre code_snippet_id="2048504" snippet_file_name="blog_20161214_3_3393435" name="code" class="html"></pre><pre code_snippet_id="2048504" snippet_file_name="blog_20161214_3_3393435"></pre> 
<pre></pre> 
<pre></pre> 
<pre></pre> 
<pre></pre> 
<pre></pre> 
 
</pre><div class="save_code tracking-ad" data-mod="popu_249"><a href="javascript:;" rel="external nofollow" ><img src="https://cache.yisu.com/upload/information/20200623/121/113452.png"></a></div></pre> 

看完上述內(nèi)容,你們掌握Mybatis中的動(dòng)態(tài)sql怎么利用OGNL表達(dá)式進(jìn)行處理的方法了嗎?如果還想學(xué)到更多技能或想了解更多相關(guān)內(nèi)容,歡迎關(guān)注億速云行業(yè)資訊頻道,感謝各位的閱讀!

向AI問(wèn)一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI