关于mybaits中如何循环map集合

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

模拟请求 参数为map集合

@RequestMapping(value="/hh", method = RequestMethod.POST)
	public void hh() {
		Map<String,Object> outerMap=new HashMap<String,Object>();
		Map<String,Object> insideMap=new HashMap<String,Object>();
		insideMap.put("hh", "ye");
		insideMap.put("cc", "yy");
		insideMap.put("ih", "uu");
		
		outerMap.put("hei", insideMap);
		service.hh(outerMap);

xml中

<insert id="hh">
  	insert into a (
  		<foreach collection="hei" item="value" index="key" separator=",">
  			${key}
  		</foreach>
  	)values(
  		<foreach collection="hei" item="value" index="key" separator=",">
  			#{value}
  		</foreach>
  	)
  </insert>

控制台打印的sql

有上面的流程可知

            传入到xml文件中的集合outerMap,键值为hei对应着另一个map集合insideMap,所以在<foreach>标签中collection为hei对应的insideMap集合,index为insideMap的key,item为insideMap的value;#{key},#{value}即可取到pd这个map集合中的键值。至于${key},由于我写的是个插入语句,key值作为字段,value作为值,#{key}取出来的为字符串,而oracle数据库表明字段不能为字符串,用${key}取出即可;

猜你喜欢

转载自blog.csdn.net/panhaigang123/article/details/81624463