Mybatis利用OGNL表達式處理動態(tài)sql的方法教程
本文介紹的是關于Mybatis中用OGNL表達式處理動態(tài)sql的相關內(nèi)容,分享出來供大家參考學習,下面來一起看看詳細的介紹:
常用的Mybatis動態(tài)sql標簽有6種:
1. if 語句 (簡單的條件判斷)
2. choose (when,otherwize) ,相當于Java 語言中的 switch ,與 jstl 中的choose 很類似.
3. trim (對包含的內(nèi)容加上 prefix,或者 suffix 等,前綴,后綴)
4. where (主要是用來簡化sql語句中where條件判斷的,能智能的處理 and or ,不必擔心多余導致語法錯誤)
5. set (主要用于更新時)
6. foreach (在實現(xiàn) mybatis in 語句查詢時特別有用)
(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
當Job參數(shù)有傳入時,就找出對應工作的人,否則就找出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,因為單純的寫where可能會導致 where And ... 和 where .....情況的發(fā)生,Set也是一樣的
當然 trim 標簽是萬能的
<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>
總結
以上就是這篇文章的全部內(nèi)容了,希望本文的內(nèi)容對大家的學習或者工作能帶來一定的幫助,如有疑問大家可以留言交流,謝謝大家對腳本之家的支持。
相關文章
基于request.getAttribute與request.getParameter的區(qū)別詳解
本篇文章小編為大家介紹,基于request.getAttribute與request.getParameter的區(qū)別詳解。需要的朋友參考下2013-04-04
SpringBoot+kaptcha實現(xiàn)驗證碼花式玩法詳解
這篇文章主要想和大家聊聊kaptcha的用法,畢竟這個已經(jīng)有16年歷史的玩意還在有人用,說明它的功能還是相當強大的,感興趣的小伙伴可以了解一下2022-05-05
SpringBoot如何注冊Servlet、Filter、Listener的幾種方式
在Servlet 3.0之前都是使用web.xml文件進行配置,這篇文章主要介紹了SpringBoot如何注冊Servlet、Filter、Listener的幾種方式,在Servlet 3.0之前都是使用web.xml文件進行配置,2018-10-10
mybatisplus實現(xiàn)自動創(chuàng)建/更新時間的項目實踐
Mybatis-Plus提供了自動填充功能,可以通過實現(xiàn)MetaObjectHandler接口來實現(xiàn)自動更新時間的功能,本文就來介紹一下mybatisplus實現(xiàn)自動創(chuàng)建/更新時間的項目實踐,感興趣的可以了解下2024-01-01
springboot中spring.profiles.include的妙用分享
這篇文章主要介紹了springboot中spring.profiles.include的妙用,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2022-08-08

