好程序員Java分享Mybatis必會的動態SQL

好程序員Java分享Mybatis必會的動態SQL,前言:
Mybatis可謂是java開發者必須會的一項技能。MyBatis 的強大特性之一即是它的動態 SQL。若是你有使用 JDBC 或其它相似框架的經驗,你就能體會到根據不一樣條件拼接 SQL 語句的痛苦。例如拼接時要確保不能忘記添加必要的空格,還要注意去掉列表最後一個列名的逗號。利用動態 SQL 這一特性能夠完全擺脫這種痛苦。
Mybatis動態sql
mybatis 動態SQL,經過 if, choose, when, otherwise, trim, where, set, foreach等標籤,可組合成很是靈活的SQL語句,從而在提升 SQL 語句的準確性的同時,也大大提升了開發人員的效率。本文主要介紹這幾個動態SQL.
具體示例
if標籤 if就是用來對輸入映射的字段進行判斷 通常是非空判斷 null 和""。
1.<!-- 案例1:動態sql之if -->  
2.<select id="selectUsersIf" parameterType="user" resultType="user">  
3.    select * from users where 1=1   
4.    <if test="uname!=null and uname!=''"> and uname like "%"#{uname}"%" </if>   
5.    <if test="sex!=null and sex!=''">and sex = #{sex} </if>   
6.</select>  
動態SQL <where /> 至關於 where關鍵字 <where />能夠自動處理第一個前and 或者or。 當條件都沒有的時候 where也不會加上 。
1.<!-- 案例2:動態sql之where 能夠自動處理第一個前and 或者or。當條件都沒有的時候  where也不會加上  -->  
2.<select id="selectUsersWhere" parameterType="user" resultType="user">  
3.    select * from users   
4.    <where>  
5.    <if test="uname!=null and uname!=''"> and uname like "%"#{uname}"%" </if>   
6.    <if test="sex!=null and sex!=''">and sex = #{sex} </if>   
7.    </where> 
choose—when--when--otherwise when能夠多個 otherwise只能有一個 相似於 switch case。
需求:輸入用戶id 按照用戶id進行精確查找 其餘條件不看 沒有輸入 id 用戶名模糊查找 都沒有的話 查詢id=1的用戶
1.<!-- 案例3:動態sql之choose—when when otherwise -->  
2.  
3.<select id="selectUsersChoose" parameterType="user" resultType="user">  
4.    select * from users   
5.    <where>  
6.            <choose>  
7.                <when test="uid!=null"> uid=#{uid}</when>  
8.                <when test="uname!=null and uname!=''"> uname like "%"#{uname}"%"</when>  
9.                <otherwise>uid=1</otherwise>  
10.              
11.            </choose>   
12.    </where>  
13.</select>
動態sql之set 代替set關鍵字 set標籤能夠幫助咱們去掉最後一個逗號
1.<update id="updateSet" parameterType="user">  
2.    
3.    update users    
4.      
5.  <set>  
6.    <if test="uname!=null and uname!=''"> uname =#{uname},</if>  
7.    <if test="upwd!=null and upwd!=''"> upwd =#{upwd},</if>  
8.    <if test="sex!=null and sex!=''"> sex =#{sex},</if>  
9.    <if test="birthday!=null"> birthday =#{birthday},</if>  
10.  </set>  
11.    where  uid=#{uid}  
12.  </update>  
Trim,trim代替where
1.<!-- 案例5:動態sql之trim  代替where      -->  
2.    <select id="selectUsersTrimWhere" parameterType="user" resultType="user">  
3.    select * from users   
4.    <!--   
5.    prefix:指添加前綴修飾  
6.    suffix:添加後綴修飾  
7.    prefixOverrides:去掉前綴修飾  
8.    suffixOverrides:去掉後綴修飾  
9.     -->  
10.<trim prefix="where"  prefixOverrides="and|or" >  
11.<if test="uname!=null and uname!=''"> and uname like "%"#{uname}"%" </if>   
12.<if test="sex!=null and sex!=''">and sex = #{sex} </if>   
13.</trim>  
14.</select>  
Trim代替set:
1.      <!-- 案例6:動態sql之trim  代替set    -->  
2.      <update id="updateTrimSet" parameterType="user">  
3.  
4.update users    
5.      
6.  <trim prefix="set" suffixOverrides="," suffix="where  uid=#{uid}" >  
7.    <if test="uname!=null and uname!=''"> uname =#{uname},</if>  
8.    <if test="upwd!=null and upwd!=''"> upwd =#{upwd},</if>  
9.    <if test="sex!=null and sex!=''"> sex =#{sex},</if>  
10.    <if test="birthday!=null"> birthday =#{birthday},</if>  
11.  </trim>  
12.      
Foreach來遍歷集合
1.<!--案例7:動態sql之foreach   遍歷數組  -->  
2.<select id="selectUsersForeachArray"  resultType="user">  
3.    select * from users  where uid  in    
4.    <!--   
5.        collection:要遍歷的集合  
6.        item:當前正在遍歷的對象的變量名  
7.        open:開始遍歷  
8.        close:結束便利  
9.        index:下標  
10.        separator:分割  
11.     -->  
12.    <foreach collection="array" item="item" open="(" close=")" index="index" separator=",">  
13.        #{item}  
14.    </foreach>  
15.  
16.</select>  java

總結
熟練掌握以上Mysql的動態SQL,咱們能夠更駕輕就熟的完成個人功能,寫更少的代碼,實現更多的功能。程序員

相關文章
相關標籤/搜索