一.入參為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>