mybatis中foreach循环的使用

mybatis中foreach循环的使用

xml中构建的sql语句

<select   id="selectUsers" parameterType="java.util.List"  resultType="User">

	 select  *  from d_user where id in  
			<foreach   collection = "Ids"  item = "id" index="index"  open = "(" separator= "," close=")" >
					#{id}
			</foreach>
			
</select>

实际执行的sql语句

select  *  from d_user where id in ('30dc0a58044740edb1014be2a0da33e2','89418cc55902404484a9c04c02971b86')

参数意义

  • collection = “Ids” 是接收到的集合或者数组。
  • item = “id” 是在循环过程中的ids被循环到的某一个id
  • index=“index” 是当前元素在集合中索引值。
  • open = “(” 起始符号用为"("。
  • separator= “,” 被循环的所有元素用 “,” 隔开。
  • close=")" 结束符号用为")"。

关于传值
* foreachcollection 接收值时不使用 #{ xxx } 的形式的

  • 直接 parameterType=“java.util.List”, 根据 list 名获取。
  • 通过对象中的list类型的属性传值,根据 list类型的属性名获取。
  • 通过 parameterType=" java.util.Map",根据 list 名获取。

用法技巧示例

分割 separator=“union” 将多对象从dual查询出并合并,实现批量插入数据。

 <insert id="insert" parameterType="java.util.List">
         insert into d_school (
         	id,
			name,
			leavel,
			label_id,
			school_time_start,
			school_time_end,
			subordinate_province,
			subordinate_province_name,
			subordinate_city,
			subordinate_city_name,
			subordinate_region,
			subordinate_region_name,
			
        )
        <foreach collection="list" item="item" index="index" separator="union">
   			select
				#{item.id},
				#{item.name},
				#{item.leavel},
				#{item.labelId},
				#{item.schoolTimeStart},
				#{item.schoolTimeEnd},
				#{item.subordinateProvince},
				#{item.subordinateProvinceName},
				#{item.subordinateCity},
				#{item.subordinateCityName},
				#{item.subordinateRegion},
				#{item.subordinateRegionName},
				
    		from dual
    	</foreach>
    </insert>


分割 separator=";" 将需要修改的多个对象循环组件update语句,实现批量修改数据。

 <update id="update" parameterType="java.util.List">
		<foreach collection="list" item="item" index="index" open="" close="" separator=";">
			update d_school
					   <set>
					          <if test="item.name != null and item.name != ''">
							       name = #{item.name},
							  </if>
					          <if test="item.leavel != null and item.leavel != ''">
							       leavel = #{item.leavel},
							  </if>
					          <if test="item.labelId != null and item.labelId != ''">
							       label_id = #{item.labelId},
							  </if>
							  <if test="item.schoolTimeStart != null and item.schoolTimeStart != ''">
							       school_time_start = #{item.schoolTimeStart},
							  </if>
					          <if test="item.schoolTimeEnd != null and item.schoolTimeEnd != ''">
							       school_time_end = #{item.schoolTimeEnd},
							  </if>
					          <if test="item.subordinateProvince != null and item.subordinateProvince != ''">
							       subordinate_province = #{item.subordinateProvince},
							  </if>
					          <if test="item.subordinateProvinceName != null and item.subordinateProvinceName != ''">
							       subordinate_province_name = #{item.subordinateProvinceName},
							  </if>
					          <if test="item.subordinateCity != null and item.subordinateCity != ''">
							       subordinate_city = #{item.subordinateCity},
							  </if>
					          <if test="item.subordinateCityName != null and item.subordinateCityName != ''">
							       subordinate_city_name = #{item.subordinateCityName},
							  </if>
					          <if test="item.subordinateRegion != null and item.subordinateRegion != ''">
							       subordinate_region = #{item.subordinateRegion},
							  </if>
					          <if test="item.subordinateRegionName != null and item.subordinateRegionName != ''">
							       subordinate_region_name = #{item.subordinateRegionName},
							  </if>
					 </set>
			<where>
		        	<if test="item.id != null and item.id != ''">
		         		   id = #{item.id}
		      		</if>
		    </where>
		</foreach>
	</update>
  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
### 回答1: Mybatisforeach循环可以用于遍历一个List或者数组,也可以用于遍历一个Map。如果要遍历一个Map,需要在foreach标签指定collection属性为Map的keySet()方法,同时指定item属性为Map的key值,index属性为Map的value值。例如: ``` <select id="selectByMap" parameterType="map" resultType="User"> SELECT * FROM user WHERE id IN <foreach collection="ids" item="id" index="index" open="(" separator="," close=")"> #{id} </foreach> </select> ``` 其,ids是一个Map类型的参数,key为String类型,value为Integer类型。在foreach标签,指定了collection属性为ids.keySet(),item属性为key,index属性为value,这样就可以遍历Map的key值了。 ### 回答2: mybatis是一个Java持久化框架,它提供了一个非常方便的功能,即foreach循环map。Mybatisforeach循环map功能非常重要,因为它能够简化我们的SQL语句,提高我们的开发效率。 在mybatis,我们可以使用foreach循环一个map,map是一种非常常见的数据结构,它是由一组键值对组成的。我们可以用foreach循环map的键或值,也可以同时循环map的键和值。 使用mybatisforeach循环map,需要我们定义一个map的标签,然后使用foreach标签来循环map的键和值,我们还可以使用index变量来获取当前循环的索引。 除了循环map的键和值,我们还可以在foreach使用if语句来过滤map的数据,这样我们可以更精确地获取我们所需的数据。 使用mybatisforeach循环map要注意一些细节,比如说循环map的键和值时要使用entry标签,而不能使用key或value标签。此外,我们还需要通过指定collection属性来指定我们要循环的map。 总之,mybatisforeach循环map功能非常强大,它可以让开发者更高效地编写SQL语句,提高开发效率。使用foreach循环map需要注意一些细节,但只要掌握了相关的知识,就可以轻松地完成各种复杂的开发任务。 ### 回答3: MyBatis是一款面向Java语言的持久层框架,它提供了许多方便的实现数据库操作的方式。在MyBatisforeach语句是一种非常强大的循环语句,可以用来遍历集合或数组,并将其转换成SQL语句的一部分。在MyBatis,可以使用foreach循环遍历一个Map对象。 在使用foreach循环遍历Map时,需要在SQL语句使用#{}占位符,并在foreach标签指定Map的key和value变量名。具体实现方式如下: 使用Map进行数据查询: ``` <select id="getUserListByMap" parameterType="java.util.Map" resultType="com.example.User"> SELECT * FROM user WHERE <foreach collection="param.keySet()" index="key" item="item" open="" close="" separator="AND"> ${key} = #{item} </foreach> </select> ``` 通过上面这段代码可以看到,在SQL语句使用了#{}占位符,并使用foreach标签来遍历Map对象。其,collection属性用于指定要遍历的Map对象,item属性表示Map的value值,index属性表示Map的key值。 在以上SQL语句,遍历Map对象得到的结果类似于: SELECT * FROM user WHERE age = 18 AND name = 'Bob' AND sex = 'male'; 这条语句是根据输入的Map对象的键值对动态生成的,可以根据不同的需求进行调整。 总之,使用foreach循环遍历Map对象是MyBatis一种十分常见的操作方式,能够大大提升查询的效率,并且极大地降低了程序开发的难度。需要注意的是,在使用foreach循环时,需要格外注意SQL语句的正确性,并避免出现语法错误。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值