如何将一个list传入到mySql语句进行查询

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/xuanzhangran/article/details/85156987

如何将一个list传入到mySql语句进行查询

方法(一)
将list封装成一个Map传入进去

	//(1)将如下字符串分隔之后放入到list里面
	String cols = ”1,2,3,4,5,6”;
	List<String> yids = new ArrayList<String>();
	String[] as = cols.split(",");
	for(String s : as){
		yids.add(s);
	}
	//(2)将list放到Map里面,正常的传入到后台
	Map<String, Object> map2 = new HashMap<String, Object>();
	Long productId = 1111;
	map2.put("ids", yids);
	map2.put("productId", productId);
	specDataMapper.updateInvalidByProductIdAndCols1(map2);

mapper中sql语句如下

<update id="updateInvalidByProductIdAndCols1">
	    update tbl_mall_spec_data
	    set invalid = 1
	    where product_id = #{productId,jdbcType=BIGINT}
	     and cols1 not in 
	     //注意connection里面是ids item里面就是传过来的集合
	    <foreach collection="ids" item="item" index="index" open="(" separator="," close=")">
		   #{item}
	    </foreach>
  </update>

方法(二)
(1)将如下字符串分隔之后放入到list里面

    Long productId = 1111;
	String cols = ”1,2,3,4,5,6”;
	List<String> yids = new ArrayList<String>();
	String[] as = cols.split(",");
	for(String s : as){
		yids.add(s);
	}
	specDataMapper.updateInvalidByProductIdAndCols1(yids,productId);
	

(2)service注意 由于传进去的是两个参数 所以需要加上@Param 否者不会识别此参数

List<SysModel> getModelListByIds(@Param List<String> ids,@Param Long productId);

(3)mapper中sql语句如下

<update id="updateInvalidByProductIdAndCols1">
	    update tbl_mall_spec_data
	    set invalid = 1
	    where product_id = #{productId,jdbcType=BIGINT}
	     and cols1 not in 
	     //注意由于直接传入的是list所以connection里面是list   item里面就是传过来的集合
	    <foreach collection="list" item="item" index="index" open="(" separator="," close=")">
  			#{item}
  	    </foreach>
  </update>

猜你喜欢

转载自blog.csdn.net/xuanzhangran/article/details/85156987