Mybatis的遍历查询

<!-- 数组遍历 -->
	<select id="queryUserByIDArray" resultType="User" parameterType="int[]">
		select * from user 
		<where>	
		<if test="array != null and array.length > 0">
		
			<foreach collection="array" open="and id in(" close=")" item="id" separator=",">
				#{id} 
			</foreach>
		</if>		
		</where>		 
	</select>

<where></where> 里面的东西一一分解

1 . <if test="array != null and array.length > 0">
这是对array数组进行判断
2.<foreach collection="array" open="and id in(" close=")" item="id" separator=",">
collection=“类型名称”,必须得是==“array“==才行
open和close其实就是对你where语句后面的分割 ,将其合并在一起就是and id in( xx , xx ),separator中是分隔符标志-----”,“
item 通俗理解为in(xx,xx)中的xx

	<!-- 集合遍历 -->
	<select id="queryUserByIDList" resultType="User" parameterType="list">
		select * from user 
		<where>	
		<!-- 数组里面需要用list代替测试类里面的参数ids -->	
		<if test="list != null and list.size > 0">
		
			<foreach collection="list" open="and id in(" close=")" item="o" separator=",">
				#{o} 
			</foreach>
		</if>		
		</where>		 
	</select>

上面表述的是集合的遍历,几乎跟数组没有什么区别,将 parameterType和collection中的数值改为list即可

	<!-- 
		对象数组遍历   
		传参的数组是Objet
		-->
	<select id="queryUserByIDObArray" resultType="User" parameterType="Object[]">
		select * from user 
		<where>	
		
		<if test="array != null and array.length > 0">
		
			<foreach collection="array" open="and id in(" close=")" item="user" separator=",">
				#{user.id} 
			</foreach>
		</if>		
		</where>		 
	</select>

将 parameterType和collection中的数值改为arry即可

  • 为什么要用Object[]
    答:因为输入的对象数组中有可能包含不同类型的数据
  • 5
    点赞
  • 9
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值