使用Set和List传参在MyBatis的foreach标签中遍历的写法是一样的

本文详细介绍了在MyBatis中如何使用List和Set作为参数进行SQL查询的foreach写法,包括mapper接口定义及XML配置示例,适用于需要处理集合参数的场景。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

1.1、List参数在mybatis的foreach写法如下代码:

<select id="getObjByProductId" parameterType="java.util.List" resultType="String">
		SELECT
			GROUP_CONCAT(
				CONCAT_WS(
					'-',
					Province,
					City,
					Area
				) SEPARATOR ','
			) AS zone
		FROM
			table
		where
			ID IN 
			<foreach collection="productids" item="id" index="index" open="(" close=")" separator=",">
			  #{id}
			</foreach>
	</select>

1.2、对应的mapper(Dao)

public String getObjByProductId(@Param("productids") List<String> productids);

2.1、Set参数在mybatis的foreach写法也一样

<select id="getUniqueObjByState" resultType="String" parameterType="java.util.Set">
		<if test="set != null">
			SELECT
				City 
			FROM
				table WHERE ID IN 
				<foreach collection="set" index="index" open="(" close=")" separator="," item="setObj">
					#{setObj}				
				</foreach>
		</if>
	</select>

2.2、对应的mapper(Dao)

public List<String> getUniqueObjByState(@Param("set") Set<String> set);

注:parameterType不同,foreach循环的写法一样。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值