Mybatis利用OGNL表達(dá)式處理動態(tài)sql的方法教程
本文介紹的是關(guān)于Mybatis中用OGNL表達(dá)式處理動態(tài)sql的相關(guān)內(nèi)容,分享出來供大家參考學(xué)習(xí),下面來一起看看詳細(xì)的介紹:
常用的Mybatis動態(tài)sql標(biāo)簽有6種:
1. if 語句 (簡單的條件判斷)
2. choose (when,otherwize) ,相當(dāng)于Java 語言中的 switch ,與 jstl 中的choose 很類似.
3. trim (對包含的內(nèi)容加上 prefix,或者 suffix 等,前綴,后綴)
4. where (主要是用來簡化sql語句中where條件判斷的,能智能的處理 and or ,不必?fù)?dān)心多余導(dǎo)致語法錯(cuò)誤)
5. set (主要用于更新時(shí))
6. foreach (在實(shí)現(xiàn) mybatis in 語句查詢時(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í),就找出對應(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)閱渭兊膶憌here可能會導(dǎo)致 where And ... 和 where .....情況的發(fā)生,Set也是一樣的
當(dāng)然 trim 標(biāo)簽是萬能的
<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é)
以上就是這篇文章的全部內(nèi)容了,希望本文的內(nèi)容對大家的學(xué)習(xí)或者工作能帶來一定的幫助,如有疑問大家可以留言交流,謝謝大家對腳本之家的支持。
相關(guān)文章
Java創(chuàng)建型設(shè)計(jì)模式之建造者模式詳解
建造者模式是Java中一種創(chuàng)建型設(shè)計(jì)模式,它的主要目的是將一個(gè)復(fù)雜對象的構(gòu)建過程分解為多個(gè)簡單對象的構(gòu)建過程,本文將詳細(xì)介紹Java中的建造者模式,包括它的定義、結(jié)構(gòu)、實(shí)現(xiàn)方法以及應(yīng)用場景等方面,希望對大家有所幫助2023-05-05mybatis實(shí)現(xiàn)表與對象的關(guān)聯(lián)關(guān)系_動力節(jié)點(diǎn)Java學(xué)院整理
這篇文章主要介紹了mybatis實(shí)現(xiàn)表與對象的關(guān)聯(lián)關(guān)系_動力節(jié)點(diǎn)Java學(xué)院整理,需要的朋友可以參考下2017-09-09springmvc使用JSR-303進(jìn)行數(shù)據(jù)校驗(yàn)實(shí)例
本篇文章主要介紹了詳解springmvc使用JSR-303進(jìn)行數(shù)據(jù)校驗(yàn),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下。2017-02-02利用idea快速搭建一個(gè)spring-cloud(圖文)
本文主要介紹了idea快速搭建一個(gè)spring-cloud,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2022-07-07SpringBoot依賴和代碼分開打包的實(shí)現(xiàn)步驟
本文主要介紹了SpringBoot依賴和代碼分開打包的實(shí)現(xiàn)步驟,,這種方法將依賴和代碼分開打包,一般更新只有代碼修改,Pom文件是不會經(jīng)常改動的,感興趣的可以了解一下2023-10-10使用Java實(shí)現(xiàn)構(gòu)建jenkins的多個(gè)job并返回構(gòu)建結(jié)果示例
這篇文章主要介紹了使用Java實(shí)現(xiàn)構(gòu)建jenkins的多個(gè)job并返回構(gòu)建結(jié)果示例,具有很好的參考價(jià)值,希望對大家有所幫助。一起跟隨小編過來看看吧2020-05-05