mybatis foreach循环使用的两种传参方式
方式一:传参ids是用逗号隔开
Mapper.java
List<> selectByIds(@Param("ids") String ids);
Mapper.xml
<select id="selectByIds" parameterType="String" resultType="String">
select * from table a where a.id in
<foreach item="item" index="index" collection="ids.split(',')"
open="(" separator="," close=")"> #{item}
</foreach>
</select>
方式二:传参ids是用List<String> 集合
Mapper.java
List<> selectByIds(@Param("ids") List<String> ids);
Mapper.xml
<select id="selectByIds" parameterType="list" resultMap="BaseResultMap">
select * from table a where a.id in
<foreach item="item" index="index" collection="ids"
open="(" separator="," close=")"> #{item}
</foreach>
</select>

浙公网安备 33010602011771号