mybatis複雜查詢collection、association等標籤的實戰指南

mybatis foreach中的collection的幾重用法

foreach的主要用在構建in條件中,它可以在SQL語句中進行迭代一個集合。foreach元素的屬性主要有 item,index,collection,open,separator,close。

  • item表示集合中每一個元素進行迭代時的別名
  • index指 定一個名字,用於表示在迭代過程中,每次迭代到的位置
  •  open表示該語句以什麼開始
  • separator表示在每次進行迭代之間以什麼符號作爲分隔符
  •  close表示以什麼結束。

在使用foreach的時候最關鍵的也是最容易出錯的就是collection屬性,該屬性是必須指定的,但是在不同情況 下,該屬性的值是不一樣的,主要有一下3種情況:

  1. 如果傳入的是但參數且參數類型是一個List的時候,collection屬性值爲list
  2. 如果傳入的是但參數且參數類型是array數組的時候,collection的屬性值爲array
  3. 如果傳入的參數的多個的時候,我們可以將所有的參數封裝成一個Map,當然單個參數也可
  4. 如果傳入的是一個Bean對象的時候,我們可以通過bean.屬性的方式獲取參數值

一、參數List情況

public List<Area> findUserListByIdList(List<String> idList) {  
       return baseMapper.selectUserListByIdList(idList);  
}  

對應mapper

<select id="selectUserListByIdList" parameterType="java.util.ArrayList" resultType="User">  
    select * from user 
    <where>  
         in (  
        <foreach  collection="list"  item="item" index="index" separator=","> 
            #{item} 
        </foreach>  
        )  
    </where>  
</select>

二、單參數Array數組情況

public List<User> findUserListByIdArray(int[] ids) {  
      return baseMapper.selectUserListByIdArray(ids);  
}

對應mapper

<select id="selectUserListByIdArray" parameterType="java.util.HashList" resultType="User">  
    select * from user u 
    <where>  
        u.id in (  
        <foreach collection="array" item="item" index="index" separator=","> 
            #{item} 
        </foreach>  
        )  
    </where>  
</select> 

三、多參數Map情況

mapper層:

List<SdCrmCustomer> selectCustomerListWhereIdNotInSysAnnouncement(@Param("paramMap") Map<String, Object> paramMap);

xml:

 <!-- 查詢超過五分鐘半個小時之內未跟進的並且半個小時之內未發過消息的客戶列表 (not in)  -->
    <select id="selectCustomerListWhereIdNotInSysAnnouncement" resultType="org.jeecg.modules.crm.customer.entity.SdCrmCustomer" parameterType="Map">
        SELECT
              id, user_id, customer_no, we_chat, qq, phone,
              opportunity_gains_mode, belong_status, is_follow, latest_belong_time
        FROM
              sd_crm_customer
        WHERE
              deleted = 0
        AND (
        belong_status = #{paramMap.belongStatus}
        AND is_follow = #{paramMap.isFollow}
        AND opportunity_gains_mode IN
        <foreach collection="paramMap.opportunityGainsModeList" index="index" item="mode" open="(" separator="," close=")">
            #{mode}
        </foreach>
        AND opportunity_gains_time &gt; #{paramMap.opportunityGainsTimeStart}
        AND opportunity_gains_time &lt; #{paramMap.opportunityGainsTimeEnd} )
        AND id NOT IN ( SELECT bus_id FROM sys_announcement WHERE create_time &gt; #{paramMap.beforeTime})
    </select>

可以看到Map的參數方式,可以通過Map.key的取值方式獲取參數值,key主要是駝峯就是駝峯,以你自己定義的爲準就好了

四、Bean對象情況

List<SdSchoolBatchQueryResultVO> selectBatchPageList(Page<SdSchoolBatchQueryResultVO> page, @Param("queryDTO") SdSchoolBatchQueryDTO queryDTO);

對應mapper

<select id="selectBatchPageList" resultType="org.jeecg.modules.admin.vo.SdSchoolBatchQueryResultVO">
        SELECT
            id, name, batch_no, major_category_id, start_enroll_date, progress_status, batch_class_type_count, batch_class_section_count,
            is_exist_conflict_class_section, first_class_open_date, last_class_open_date, status, create_by, create_time, update_by, update_time
        FROM
            sd_school_batch
        <where>
            <if test="queryDTO.id!=null and queryDTO.id!='' ">
                AND id = #{queryDTO.id}
            </if>
            <if test="queryDTO.name!=null and queryDTO.name!='' ">
                AND `name` = #{queryDTO.name}
            </if>
            <if test="queryDTO.startEnrollDateBegin!=null ">
                AND start_enroll_date &gt; #{queryDTO.startEnrollDateBegin}
            </if>
            <if test="queryDTO.startEnrollDateEnd!=null ">
                AND start_enroll_date &lt; #{queryDTO.startEnrollDateEnd}
            </if>
            <if test="queryDTO.minBatchClassSectionCount != null ">
                AND batch_class_section_count &gt; #{queryDTO.minBatchClassSectionCount}
            </if>
            <if test="queryDTO.minBatchClassSectionCount != null ">
                AND batch_class_section_count &lt; #{queryDTO.maxBatchClassSectionCount}
            </if>
            <if test="queryDTO.isExistConflictClassSection!=null ">
                AND is_exist_conflict_class_section = #{queryDTO.isExistConflictClassSection}
            </if>
            <if test="queryDTO.progressStatus!=null ">
                AND progress_status = #{queryDTO.progressStatus}
            </if>
            <if test="queryDTO.status!=null">
                AND status = #{queryDTO.status}
            </if>
            <if test="queryDTO.majorCategoryChildrenIdList!=null ">
                AND major_category_id IN
                <foreach collection="queryDTO.majorCategoryChildrenIdList" item="item" index="index" open="(" separator="," close=")">
                    #{item}
                </foreach>
            </if>
            AND deleted = 0
        </where>
        ORDER BY
        <choose>
            <when test="queryDTO.column!=null and queryDTO.order!=null ">
                ${queryDTO.column} ${queryDTO.order}
            </when>
            <otherwise>
                create_time DESC
            </otherwise>
        </choose>
    </select>

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章