参考:foreach 实现 MyBatis 遍历集合与批量操作数据_点滴记录-CSDN博客_mybatis遍历list
SELECT * FROM t_employee WHERE id IN (1, 2, 3, ...)
/** 根据传入的 id 集合,查询出对应的员工信息,并使用集合保存信息 */ List<Employee> getEmpsByConditions(@Param("list") List<Integer> idList);
<!-- 注意返回的数据类型是集合中保存的数据类型 Employee-->
<select id="getEmpsByConditions" resultType="com.jas.mybatis.bean.Employee">
SELECT * FROM t_employee WHERE id IN
<!--
collection:指定要遍历的集合
item:取出当前集合中元素,赋给 item 中的值
separator:遍历出的多个元素之间用什么分隔符分隔开
open:遍历集合前用什么字符进行拼接
close:遍历集合后用什么字符进行拼接在 foreach 标签中还有一个属性 index,
遍历集合的时候 index 表示的是当前元素的索引,item 对应索引中的值
遍历 map 的时候 index 表示的是当前 map 中的 key,item 是 key 对应的 value
-->
<foreach collection="list" item="empId" separator="," open="(" close=")">
#{empId}
</foreach>
</select>
List<JdMenuVO> lazyList(Long parentId, Map<String, Object> param,long[] nodesId); 数组也行的<select id="lazyList" resultMap="jdMenuVOResultMap"> /* SELECT menu.*, ( SELECT CASE WHEN count( 1 ) > 0 THEN 1 ELSE 0 END FROM jd_menu WHERE parent_id = menu.id AND is_deleted = 1 ) AS "has_children" FROM jd_menu menu WHERE menu.is_deleted = 1*/ select t1.*,t2.dicname as p_name, ( SELECT CASE WHEN count( 1 ) > 0 THEN 1 ELSE 0 END FROM jd_menu WHERE parent_id = t1.id AND is_deleted = 1 ) AS "has_children" from jd_menu t1 left join jd_menu t2 on t2.id=t1.parent_id WHERE t1.is_deleted = 1 <if test="param1!=null"> and t1.parent_id = #{param1} </if> <if test="param2.dicName!=null and param2.dicName!=''"> and t1.dicName like concat(concat('%', #{param2.dicName}),'%') </if> <if test="param1!=0 and param3!=null"> and t1.id in <foreach collection="param3" item="empId" separator="," open="(" close=")"> #{empId} </foreach> </if> ORDER BY t1.sort </select>
List<Test> selectById(List<Integer> id);
select id="selectById" parameterType="java.util.List"
resultMap="BaseResultMap">
select * from test
where status = 'A'
and id in
<foreach collection="list" index="index" item="item" open="("
separator="," close=")">
#{item}
</foreach>
</select>
//判断该条数据是否属于当前选择路段
List<EtcPassRateBatchDTO> getByPlazaCodes(List<String> pcodes);
<select id="getByPlazaCodes" parameterType="java.util.List" resultType="org.springblade.indicator.dto.EtcPassRateBatchDTO">
select ROAD_NAME roadName from CONFIG_ROAD where id in(
select ROAD_ID from CONFIG_PLAZA where PLAZA_CODE in
<foreach collection="list" index="index" item="item" open="("
separator="," close=")">
#{item}
</foreach>
GROUP BY ROAD_ID
)
</select>
传入参数为list集合时 : collection = "list"
传入参数为array集合时 : collection = "array"
传入参数为其他集合时 : collection = "collection"
Set<String> getEmAccountsByUserIds(Set<String> userIdSet);
<select id="getEmAccountsByUserIds" parameterType="java.util.Set" resultType="String"> select em_account from portal_user where id in <foreach collection="collection" index="index" item="item" open="(" separator="," close=")"> #{item} </foreach> </select>