1. 程式人生 > >MyBatis-動態SQL應用-mapper.xml

MyBatis-動態SQL應用-mapper.xml

MyBatis-動態SQL應用-mapper.xml

  • if語句
select * from user where 1 = 1
        <if test="username != null">
           and username=#{username}
        </if>
         
        <if test="username != null">
           and sex=#{sex}
        </if>

test 是對條件的驗證,可以理解為 if( username != null) 注意變數直接寫變數名,在引號裡直接處理,MyBatis特有的寫法 , 這裡為了關聯, 用到了 where 1 = 1 ,做恆等,and關聯 , 對越where 1 = 1 這樣的恆等,MyBatis 有自己的處理方式

  • where語句
select * from user
    <where>
        <if test="username != null">
           username=#{username}
        </if>
         
        <if test="username != null">
           and sex=#{sex}
        </if>
    </where>

where使用,代替了上面的恆等 1=1 ,而且,where標籤,會自動過濾掉 and/or 這樣的字首 , 比如這裡 , username一但值為空, 直接輸出的條件語句 where and sex = ? 這樣是錯誤的,但是MyBatis 有自己的處理方式 , 會過濾掉多餘的字首, 輸出條件語句 where sex = ?

  • choose(when,otherwise)語句
select * from user
      <where>
          <choose>
              <when test="id !='' and id != null">
                  id=#{id}
              </when>
              <when test="username !='' and username != null">
                  and username=#{username}
              </when>
              <otherwise>
                  and sex=#{sex}
              </otherwise>
          </choose>
      </where>

這裡choose / when / when / otherwise 可以理解為 choose 標籤內, 是可以選擇的條件, when / when/otherwise 做 if (條件)else if (條件)else  先去判斷第一個條件 , 如果滿足 , 選擇,不滿足判斷下一個,知道when結束,都沒有滿足條件,會直接進入othersies 

  • foreach 語句
select * from user
        <where>
            <foreach collection="ids" item="id" open="and id in (" close=") " separator=",">
                #{id}
            </foreach>
        </where>

這裡,ids 可以只array陣列,可以是list集合 item 是給ids在本迴圈裡的子單位 ,每一個子元素的別名 ,open 是迴圈開始標記 cloes 迴圈結束標記 ,separator 迴圈中 每次子元素之間的 間隔符 輸出的語句應該是這樣 select * from user where id in (?,?,?) 陣列/集合中有多少子元素迴圈多少次

  • sql 語句 include 嵌入
<sql id="mySQLIf">
    <if test="username != null and username != ''">
        AND username = #{username}
    </if>
    <if test="sex != null and sex != ''">
        AND sex = #{sex}
    </if>
</sql>
<sql id="mySQLList">
    username,sex,age
</sql>

可以是邏輯語句,子查詢,sql 查詢欄位等,上面編輯好了,接下來嵌入

select * from user <where> <include refid="mySQLIf"></include> </where>

輸出SQL select * from user where username = ? and sex = ?

  • set 語句
update user u
        <set>
            <if test="username != null and username != ''">
                u.username = #{username},
            </if>
            <if test="sex != null and sex != ''">
                u.sex = #{sex}
            </if>
        </set>
     
     <where>
         id=#{id}
     </where>

用於修改,修改條件 如果變數都不為空, 都生效, 輸出SQL update user u set username = ? ,sex = ? where id = ?

  • trim  語句
  • 案例一:
update user u
            <set>
                <if test="username != null and username != ''">
                    u.username = #{username},
                </if>
                <if test="sex != null and sex != ''">
                    u.sex = #{sex}
                </if>
            </set> 
         
         where id=#{id}
   

set語句的寫法,下面我用trim重寫它

update user u
      
            <trim prefix="set" suffixOverrides=",">
                <if test="username != null and username != ''">
                    u.username = #{username},
                </if>
                <if test="sex != null and sex != ''">
                    u.sex = #{sex},
                </if>
            </trim>
         
         where id=#{id}

效果是一樣的 trim 程式碼塊執行前加set 去掉最後一個 ,prefix 字首 suffixOverrides 字尾去掉(最後一個)

  • 案例二:
    select * from user
             <where>
                <if test="username != null">
                   username=#{username}
                </if>
                 
                <if test="username != null">
                   and sex=#{sex}
                </if>
            </where>  

    where if 連用的寫法 , 下面我用trim替換他

select * from user
        
        <trim prefix="where" prefixOverrides="and | or">
            <if test="username != null">
               and username=#{username}
            </if>
            <if test="sex != null">
               and sex=#{sex}
            </if>
        </trim>

字首 where 後面接trim程式碼塊內的 程式碼 ,去掉第一個 and 或者 or 這裡會去掉 and ,prefixOverrides 去掉最前面的 and 或者 or

今天就說這麼多,這就是我給大家分享的 MyBatis動態sql,* 可以忽略不看,這裡只是演示方便

 

隨筆記錄,方便自己學習

CHenyb 2018-11-16