Mysql動態查詢


if條件查詢

格式: <if test=”條件判斷”> 添加到sql的語句 </if>

where標簽

簡化SQL語句中WHERE條件判斷

智能處理and和or

如果使用幾個if條件標簽,如果第一個條件不成立,那么該sql語句就不成立了.

把所有的if條件語句加入到where標簽內,則會根據if語句成立是否添加where條件,若標簽返回的內容是以and或者or開頭的,會自動剔除.

案例:

<!-- 分類后根據關鍵字進行搜索 -->

   <select id="getUserListByClassify" resultMap="userList">

         SELECT user.*,role.roleName

         FROM smbms_user user

         INNER JOIN smbms_role role ON user.userRole=role.id

         <where>

            <if test="userName != null">

              AND   userName like CONCAT('%',#{userName},'%')

           </if>

           <if test="userRole != null">

              AND userRole=#{userRole}

           </if>

         </where>

</select>

trim標簽

屬性:

prefix:前綴 如果有返回值,在標簽前加的語句

suffix:后綴 如果有返回值,在標簽后加的語句

prefixOverrides:對於trim包含內容的首部進行指定內容的忽略

sufixOverrides:對於trim包含內容的尾部進行指定內容的忽略

可以去除多余的關鍵字(自定義)   where只能去除多余的and/or

<!-- 分類后根據關鍵字進行搜索 -->

   <select id="getUserListByClassify" resultMap="userList">

         SELECT user.*,role.roleName

         FROM smbms_user user

         INNER JOIN smbms_role role ON user.userRole=role.id

         <trim prefix="where" prefixOverrides="and | or">

           <if test="userName != null">

              userName like CONCAT('%',#{userName},'%')

           </if>

           <if test="userRole != null">

              and userRole=#{userRole}

           </if>

         </trim>

</select>

if+set進行修改(在實際開發中很少使用)

如何只對有值得參數進行修改,而不必每次都要獲取整個對象.

案例:

<!-- 更新用戶屬性 -->

      <update id="update" parameterType="User">

         UPDATE smbms_user

         <set>

           <if test="userCode != null">userCode=#{userCode},</if>

            <if test="userName!= null">userName=#{userName},</if>

           <if test="userPassword!=          null">userPassword=#{userPassword},</if>

           <if test="gender!= null">gender=#{gender},</if>

           <if test="birthday!= null">birthday=#{birthday},</if>

           <if test="phone!= null">phone=#{phone},</if>

           <if test="address!= null">address=#{address},</if>

           <if test="modifyDate!= null">modifyDate=#{modifyDate}</if>

         </set>

         where id=#{id}

</update>

if+trim進行修改

添加條件判斷修改

<!-- 更新用戶屬性 -->

      <update id="update" parameterType="User">

         UPDATE smbms_user

         <trim prefix="set" prefixOverrides="," resultMap="userList">

           <if test="userCode != null">userCode=#{userCode},</if>

            <if test="userCode != null">userName=#{userName},</if>

           <if test="userCode !=          null">userPassword=#{userPassword},</if>

           <if test="userCode != null">gender=#{gender},</if>

           <if test="userCode != null">birthday=#{birthday},</if>

           <if test="userCode != null">phone=#{phone},</if>

           <if test="userCode != null">address=#{address},</if>

           <if test="userCode != null">modifyDate=#{modifyDate}</if>

         </trim>

         where id=#{id}

</update>

使用foreach進行復雜查詢

迭代一個集合,通常用於in條件

屬性:

item:表示集合中每一個元素進行迭代時候的別名

index:指定一個名稱,表示迭代過程中,每次迭代到的位置.(可省略...)

open:表示語句以什么開始 例:open=”(”

separator:表示每次進行迭代之間以什么作為分隔符.

close:表示該語句以什么結束.

collection:最關鍵並最容易出錯的屬性,該屬性必須指定,不同情況下,該屬性的值也不一樣,主要分三種:

  1. 若傳入的參數為單參數且參數類型是一個List的時候,collection屬性值為list.
  2. 若入參為單參數且參數類型為數組的時候,collection屬性值為array
  3. 若傳入的參數為多參數,就需要把他們封裝為一個Map處理,屬性值為Map的key.

案例:使用foreach實現 全選 刪除

<!-- 刪除用戶 -->

   <delete id="delete" parameterType="map">

      DELETE FROM smbms_user WHERE id IN

<!-- 傳入的參數為map集合時 collection=為map的key -->

      <foreach collection="id" item="userId" open="(" separator="," close=")">

        #{userId}

      </foreach>

      <!-- 傳入的參數為數組時候  collection="array" -->

      <foreach collection="array" item="userId" open="(" separator="," close=")">

        #{userId}

      </foreach>

      <!-- 傳入的參數為List時候  collection="list" -->

      <foreach collection="list" item="userId" open="(" separator="," close=")">

        #{userId}

      </foreach>

   </delete>

 

 

choose(when,otherwise)

類似於java的switch --case語句,跟JSTL的choose使用方式一樣.

when元素:當test屬性中條件滿足的時候.就會輸出when元素的內容.跟java中switch一樣,依次向下執行,當有一個when中的test條件成立后,執行該語句立馬跳出choose,所以choose中只有一個條件會輸出.

 

MyBatis分頁

獲取數據的頁數

<!-- 獲取數據的總記錄數 -->

   <select id="getAllPages" resultType="Integer">

       SELECT COUNT(`id`) AS pages FROM smbms_user

       <trim prefix="where" prefixOverrides="and | or">

            <if test="userRole != null">

            userRole=#{userRole};

            </if>

       </trim>

</select>

 注:if或when標簽是針對JAVABEAN或者MAP的,進行判斷的參數要么使用注解進行封裝,或傳入對象,或添加進map在進行傳參數.

分頁

<!-- 查詢所有用戶 並進行分頁 -->

   <select id="getAllUser" resultType="User">

         SELECT user.*,role.roleName

         FROM smbms_user user

         INNER JOIN smbms_role role ON user.userRole=role.id

         ORDER BY creationDate DESC LIMIT #{from},#{pageSize}

   </select>

獲取最后一條插入數據的id(自增主鍵)

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM