最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

Mybatis利用OGNL表達(dá)式處理動(dòng)態(tài)sql的方法教程

 更新時(shí)間:2017年06月15日 09:15:04   作者:枯木生花  
這篇文章主要給大家介紹了關(guān)于Mybatis利用OGNL表達(dá)式處理動(dòng)態(tài)sql的方法教程的相關(guān)資料,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面跟著小編一起來(lái)學(xué)習(xí)學(xué)習(xí)吧。

本文介紹的是關(guān)于Mybatis中用OGNL表達(dá)式處理動(dòng)態(tài)sql的相關(guān)內(nèi)容,分享出來(lái)供大家參考學(xué)習(xí),下面來(lái)一起看看詳細(xì)的介紹:

常用的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 很類似.

      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ǔ)句查詢時(shí)特別有用)

(1) if

模糊查詢

<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="http://static.blog.csdn.net/images/save_snippets.png"></a></div></pre> 

總結(jié)

以上就是這篇文章的全部?jī)?nèi)容了,希望本文的內(nèi)容對(duì)大家的學(xué)習(xí)或者工作能帶來(lái)一定的幫助,如有疑問(wèn)大家可以留言交流,謝謝大家對(duì)腳本之家的支持。

相關(guān)文章

最新評(píng)論

赤壁市| 玉田县| 满洲里市| 呼玛县| 商河县| 建始县| 彩票| 鞍山市| 德安县| 宜宾县| 昌都县| 普格县| 苍溪县| 静宁县| 尚志市| 安陆市| 德化县| 闵行区| 木里| 邵阳市| 大悟县| 建阳市| 南和县| 镇坪县| 台北县| 青龙| 苗栗市| 博湖县| 礼泉县| 临洮县| 达州市| 鹤岗市| 盐边县| 金湖县| 营口市| 佛冈县| 黄梅县| 扎赉特旗| 城口县| 左权县| 靖安县|