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>
mybatis choose用法
最新推荐文章于 2024-03-11 17:41:26 发布