MySql的动态语句foreach,当传入参数为数组或者集合时需要通过foreach标签进行遍历,其主要是在in条件中,可以在SQL语句中迭代一个集合;

<foreach collection="dto.orderStatusList" item="item" index="index" open="(" close=")" separator=",">
	#{item}
</foreach>
 
  • collection:要做foreach的对象,作为入参时,List<?>对象默认用list代替作为键,数组对象有array代替作为键,Map对象用map代替作为键,该参数必选;

  • item:循环体中的具体对象,支持属性的点路径访问,该参数必选;

  • index:在list和数组中,index是元素的序号,在map中,index是元素的key,该参数可选;

  • open:foreach代码的开始符号,一般是(和close=")"合用,该参数可选;

  • close:foreach代码的关闭符号,一般是)和open="("合用,该参数可选;

  • separator:元素之间的分隔符,例如在in()的时候,separator=","会自动在元素中间用“,“隔开,避免手动输入逗号导致sql错误,该参数可选;

如果传入的是单参数且参数类型是一个List的时候,collection属性值为list

* Mapper中传入 * @param orderStatusList * @return public List<OrderInfoVO> findList(List<String> orderStatusList);
<select id="findList" resultType="OrderInfoVO">  
    select * from order_info where 1=1 and t.order_status in
    <foreach collection="list" item="item" index="index" open="(" close=")" separator=",">
       #{item}
    </foreach>
</select>  
 

如果传入的是单参数且参数类型是一个array数组的时候,collection的属性值为array

* 传入数组 public List<OrderInfoVO> findList(int[] orderId);
<select id="findList" resultType="OrderInfoVO">  
   select * from order_info where 1=1 and t.id in 
    <foreach collection="array" index="index" item="item" open="(" separator="," close=")">  
        #{item}  
    </foreach>  
</select> 
 

如果传入的参数是多个的时候,我们就需要把它们封装成一个Map了,当然单参数也可以封装成map,map的key就是参数名,因此collection属性值就是传入的List或array对象在自己封装的map里面的key

* 传入Map public List<OrderInfoVO> findList(Map<String, Object> orderInfo);
<select id="findList" resultType="OrderInfoVO">  
    select * from order_info where 1=1 and orderInfo like "%"#{orderInfo}"%" and t.orderServiceType in 
    <foreach collection="orderId" index="index" item="item" open="(" separator="," close=")">  
        #{item}  
    </foreach>  
</select> 
                    MyBatis中mapper.xml中foreach的使用Author:kak&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;MySql的动态语句foreach,当传入参数为数组或者集合时需要通过foreach标签进行遍历,其主要是在in条件中,可以在SQL语句中迭代一个集合;综述&lt;foreach collection="dto.orderStatusList" item="item" index="index" open="("
foreach的主要用在构建in条件,它可以在SQL语句进行迭代一个集合。
foreach元素的属性主要有 item,index,collection,open,separator,close。
item :表示集合每一个元素进行迭代时的别名
index :指定一个名字,用于表示在迭代过程,每次迭代到的位置
open :表...
				
<foreach item="item" index="index" collection="supplyIdAry" open="(" separator="," close=")"> #{item} </foreach> <foreach collection="xxx" item="item" index=...
foreach元素的属性主要有 collection,item,index,separator,open,close。 collection:表示集合,数据源 item :表示集合的每一个元素 index :用于表示在迭代过程,每次迭代到的位置 separator :表示在迭代时数据以什么符号作为分隔符 open :表示该语句以什么开始 close :表示以什么结束 DataTable tblInf = (DataTable)HttpContext.Current.Session["user_inf"]; DataRow row = tblInf.Rows[0]; INSERT INTO table_name (column1, column2, column3, ...) VALUES (value1, value2, value3, ...), (value1, value2, value3, ...), (value1, value2, value3, ...), 其,table_name 是表的名称,column1, column2, column3 是要插入的字段名称,value1, value2, value3 是要插入的数据。 如果您使用 Mybatis 来进行 SQL 的操作,可以在 mapper.xml 使用以下语句进行批量插入: <insert id="batchInsert" parameterType="java.util.List"> insert into table_name (column1, column2, column3, ...) values <foreach collection="list" item="item" index="index" separator=","> (#{item.value1}, #{item.value2}, #{item.value3}, ...) </foreach> </insert> 其,list 是要插入的数据列表,item 是对列表每个元素的引用,value1, value2, value3 分别是要插入的数据。