item循环体中的具体对象。支持属性的点路径访问,如item.age,item.info.details。具体说明:若collection属性为list或array,则item代表list或array里面的一个元素。若collection属性对应一个map,则item代表的是map中的value集合中的单个value该参数为必选。collection
foreach遍历的对象,作为入参时,List对象默认用list代替作为键,数组对象有array代替作为键,Map对象没有默认的键。也就是传入的集合(list,array,map)的名字,这个名字可以在foreach里面随便引用)当然在作为入参时可以使用@Param("params")来设置键,设置keyName后,list,array将会失效。 除了入参这种情况外,还有一种作为参数对象的某个字段的时候。举个例子:如果User有属性List ids。入参是User对象,那么这个collection = "ids"如果User有属性Ids ids;其中Ids是个对象,Ids有个属性List id;入参是User对象,那么collection = "ids.id"
如果传入参数类型为map,这个入参有注解@Param("params"),则map的所有的key集合可以写成params.keys,所有值集合可以写成params.values。这样foreach就可以对key集合或值集合进行迭代了。
上面只是举例,具体collection等于什么,就看你想对那个元素做循环。该参数为必选。
separator元素之间的分隔符,例如在in()的时候,separator=","会自动在元素中间用“,“隔开,避免手动输入逗号导致sql错误,如in(1,2,)这样。该参数可选。openforeach代码的开始符号,一般是(和close=")"合用。常用在in(),values()时。该参数可选。closeforeach代码的关闭符号,一般是)和open="("合用。常用在in(),values()时。该参数可选。index在list和数组中,index是元素的序号,在map中,index是元素的key,该参数可选。list批量插入:
<insert id="insertScoreDetailBatch"> INSERT INTO score_person VALUES <foreach collection="list" item="scoreDetail" separator=","> (DEFAULT, #{scoreDetail.stuId}, #{scoreDetail.insId}, #{scoreDetail.detailName}, DEFAULT, #{scoreDetail.operateScore}, #{scoreDetail.remarks}, DEFAULT, DEFAULT) </foreach> </insert>map更新:
<update id="alterStudentDormBatch" parameterType="Map"> <foreach collection="stuDormMap" index="stuId" item="newDorm" separator=";"> UPDATE student <set> stu_dorm=#{newDorm} </set> WHERE stu_id=#{stuId} </foreach> </update>
转载于:https://www.cnblogs.com/chenloveslife/p/9538557.html