Mybatis foreach 批量操作

来源:互联网 发布:away3d教程 源码 编辑:程序博客网 时间:2024/05/05 11:49

foreach属性

属性描述item循环体中的具体对象。支持属性的点路径访问,如item.age,item.info.details。
具体说明:在list和数组中是其中的对象,在map中是value。
该参数为必选。collection要做foreach的对象,作为入参时,List<?>对象默认用list代替作为键,数组对象有array代替作为键,Map对象用map代替作为键。
当然在作为入参时可以使用@Param("keyName")来设置键,设置keyName后,list,array,map将会失效。 除了入参这种情况外,还有一种作为参数对象的某个字段的时候。举个例子:
如果User有属性List ids。入参是User对象,那么这个collection = "ids"
如果User有属性Ids ids;其中Ids是个对象,Ids有个属性List id;入参是User对象,那么collection = "ids.id"
上面只是举例,具体collection等于什么,就看你想对那个元素做循环。
该参数为必选。separator元素之间的分隔符,例如在in()的时候,separator=","会自动在元素中间用“,“隔开,避免手动输入逗号导致sql错误,如in(1,2,)这样。该参数可选。openforeach代码的开始符号,一般是(和close=")"合用。常用在in(),values()时。该参数可选。closeforeach代码的关闭符号,一般是)和open="("合用。常用在in(),values()时。该参数可选。index在list和数组中,index是元素的序号,在map中,index是元素的key,该参数可选。


[html] view plain copy
 print?在CODE上查看代码片派生到我的代码片
  1. <select id="countByUserList" resultType="_int" parameterType="list">    
  2. select count(*) from users    
  3.   <where>    
  4.     id in    
  5.     <foreach item="item" collection="list" separator="," open="(" close=")" index="">    
  6.       #{item.id, jdbcType=NUMERIC}    
  7.     </foreach>    
  8.   </where>    
  9. </select>   

::select count(*) from users WHERE id in ( ? , ? ) 

[html] view plain copy
 print?在CODE上查看代码片派生到我的代码片
  1. <insert id="addList">  
  2.           
  3.         INSERT INTO DELIVER  
  4.             (  
  5.                 <include refid="selectAllColumnsSql"/>  
  6.              )  
  7.            
  8.           <foreach collection="deliverList" item="item" separator="UNION ALL">  
  9.                 SELECT   
  10.                      #{item.id, jdbcType=NUMERIC},  
  11.                      #{item.name, jdbcType=VARCHAR}  
  12.                 FROM DUAL  
  13.           </foreach>  
  14.     </insert>  

::insert into deliver select ?,? from dual union all select ?,? from dual

[html] view plain copy
 print?在CODE上查看代码片派生到我的代码片
  1. <insert id="ins_string_string">    
  2.         insert into string_string (key, value) values    
  3.         <foreach item="item" index="key" collection="map"    
  4.             open="" separator="," close="">(#{key}, #{item})</foreach>    
  5.     </insert>   

::insert into string_string (key, value) values (?, ?) , (?, ?)  -- mysql

[html] view plain copy
 print?在CODE上查看代码片派生到我的代码片
  1. <select id="sel_key_cols" resultType="int">    
  2.         select count(*) from key_cols where    
  3.         <foreach item="item" index="key" collection="map"    
  4.             open="" separator="AND" close="">${key} = #{item}</foreach>    
  5.     </select>   

::select count(*) from key_cols where col_a = ? AND col_b = ?  (一定要注意到$和#的区别,$的参数直接输出,#的参数会被替换为?,然后传入参数值执行。)

foreach属性

属性描述item循环体中的具体对象。支持属性的点路径访问,如item.age,item.info.details。
具体说明:在list和数组中是其中的对象,在map中是value。
该参数为必选。collection要做foreach的对象,作为入参时,List<?>对象默认用list代替作为键,数组对象有array代替作为键,Map对象用map代替作为键。
当然在作为入参时可以使用@Param("keyName")来设置键,设置keyName后,list,array,map将会失效。 除了入参这种情况外,还有一种作为参数对象的某个字段的时候。举个例子:
如果User有属性List ids。入参是User对象,那么这个collection = "ids"
如果User有属性Ids ids;其中Ids是个对象,Ids有个属性List id;入参是User对象,那么collection = "ids.id"
上面只是举例,具体collection等于什么,就看你想对那个元素做循环。
该参数为必选。separator元素之间的分隔符,例如在in()的时候,separator=","会自动在元素中间用“,“隔开,避免手动输入逗号导致sql错误,如in(1,2,)这样。该参数可选。openforeach代码的开始符号,一般是(和close=")"合用。常用在in(),values()时。该参数可选。closeforeach代码的关闭符号,一般是)和open="("合用。常用在in(),values()时。该参数可选。index在list和数组中,index是元素的序号,在map中,index是元素的key,该参数可选。


[html] view plain copy
 print?在CODE上查看代码片派生到我的代码片
  1. <select id="countByUserList" resultType="_int" parameterType="list">    
  2. select count(*) from users    
  3.   <where>    
  4.     id in    
  5.     <foreach item="item" collection="list" separator="," open="(" close=")" index="">    
  6.       #{item.id, jdbcType=NUMERIC}    
  7.     </foreach>    
  8.   </where>    
  9. </select>   

::select count(*) from users WHERE id in ( ? , ? ) 

[html] view plain copy
 print?在CODE上查看代码片派生到我的代码片
  1. <insert id="addList">  
  2.           
  3.         INSERT INTO DELIVER  
  4.             (  
  5.                 <include refid="selectAllColumnsSql"/>  
  6.              )  
  7.            
  8.           <foreach collection="deliverList" item="item" separator="UNION ALL">  
  9.                 SELECT   
  10.                      #{item.id, jdbcType=NUMERIC},  
  11.                      #{item.name, jdbcType=VARCHAR}  
  12.                 FROM DUAL  
  13.           </foreach>  
  14.     </insert>  

::insert into deliver select ?,? from dual union all select ?,? from dual

[html] view plain copy
 print?在CODE上查看代码片派生到我的代码片
  1. <insert id="ins_string_string">    
  2.         insert into string_string (key, value) values    
  3.         <foreach item="item" index="key" collection="map"    
  4.             open="" separator="," close="">(#{key}, #{item})</foreach>    
  5.     </insert>   

::insert into string_string (key, value) values (?, ?) , (?, ?)  -- mysql

[html] view plain copy
 print?在CODE上查看代码片派生到我的代码片
  1. <select id="sel_key_cols" resultType="int">    
  2.         select count(*) from key_cols where    
  3.         <foreach item="item" index="key" collection="map"    
  4.             open="" separator="AND" close="">${key} = #{item}</foreach>    
  5.     </select>   

::select count(*) from key_cols where col_a = ? AND col_b = ?  (一定要注意到$和#的区别,$的参数直接输出,#的参数会被替换为?,然后传入参数值执行。)
0 0
原创粉丝点击