mybatis choose用法

choose标签是按顺序判断其内部when标签中的test条件出否成立,如果有一个成立,则 choose 结束。当 choose 中所有 when 的条件都不满则时,则执行 otherwise 中的sql。类似于Java 的 switch 语句,choose 为 switch,when 为 case,otherwise 则为 default。

例如下面例子,同样把所有可以限制的条件都写上,方面使用。choose会从上到下选择一个when标签的test为true的sql执行。安全考虑,我们使用where将choose包起来,放置关键字多于错误。
<!-- choose 判断 -与java中 if else if相同用法 -->
<select id="selectVillageDynamicsCount" parameterType="Map" resultType="int">
	select count(1) from  User a where a.del_flag = 0
	<if test="maps.circle_home_id == null || maps.circle_home_id == '' ">
		<choose>
			<when test="maps.dynamic_type == 1 ">
				and (a.type ='1'||a.type='2'||a.type='3')
			</when>
			<when test="maps.dynamic_type == 2 ">
				and a.type = '3'
			</when>
			<otherwise>
				and (a.type ='1'||a.type='2'||a.type='3')
			</otherwise>
		</choose>
	</if>
	<if test="maps.circle_home_id != null">
		and a.circle_home_id = #{maps.circle_home_id}
	</if>
	
	<if test="maps.home_id != null">
		and a.home_id in(#{maps.home_id})
	</if>
	<if test="maps.company_id != null">
		and a.company_id = #{maps.company_id}
	</if>
</select>

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值