关于mybatis中传入一个List,字符串数组,或者Map集合作为查询条件的参数

2023-05-13,,

一.入参为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>

关于mybatis中传入一个List,字符串数组,或者Map集合作为查询条件的参数的相关教程结束。

《关于mybatis中传入一个List,字符串数组,或者Map集合作为查询条件的参数.doc》

下载本文的Word格式文档,以方便收藏与打印。