關於Mybatis中傳入一個List,字符串數組,或者Map集合作為查詢條件的參數


一.入參為List的寫法

<select id="queryParamList" resultType="map" parameterType="java.util.List">
          select id from static
          where id in 
          <foreach collection="list" index="index" item="item" open="(" separator="," close=")">  
            #{item}  
        </foreach>
  </select>

其中<foreach>這個標簽是用來循環傳入的集合的,collection="list"這個參數中有list,map兩種,還有就是自定義的參數,item="item"這個參數可以自定義,

用來循環集合里面的值,這個參數的取名要和下面#()這個里面的取名一致。

parameterType="java.util.List"這個傳入的參數類型不能簡寫成List(其中只有基本數據類型可以簡寫)。

當然,如果用in來查詢的,可以用一個string來寫,如上圖列子:將id手動拼接成一個string傳入。參照sql語句的規則。

 

二.入參為Map的寫法

<selectid="findTeacherByPage"resultMap="supervisorResultMap" parameterType="java.util.Map">
              select * from teacher  where name= #{name} limit #{start},#{limit}  
</select>

注:map中的key值就是name,start,limit。

 

三.入參為String數組的寫法

<sql id="condition_sql">     
        <if test=" paymentTypes != null and paymentTypes.size() > 0">
            AND payment_type in
            <foreach collection="paymentTypes" index="index" item="item" open="(" separator="," close=")">
                #{item}       
            </foreach> 
        </if>
</sql>
mapper的接口:  List<Dept> getDeptsByCompanyIds(@Param("companyIds") String[] companyIds);

<select id="getDeptsByCompanyIds" resultMap="Dept">
      select * from t_dept where COMPANY_ID in
      <foreach collection="companyIds" item="id" open="(" close=")" separator=",">
              #{id}
      </foreach>
</select>

 


免責聲明!

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



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