MyBatis choose(when, otherwise)标签

MyBatis choose(when, otherwise)标签

        有时候我们并不想应用所有的条件,而只是想从多个选项中选择一个。而使用if标签时,只要test中的表达式为 true,就会执行 if 标签中的条件。MyBatis 提供了 choose 元素。if标签是与(and)的关系,而 choose 是或(or)的关系。

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

        下面是我写的一个例子:

<choose>
 <when test="BEGINTIME != null and BEGINTIME != '' and ENDTIME != null and ENDTIME != ''">
 	AND time BETWEEN #{BEGINTIME, jdbcType=VARCHAR} AND #{ENDTIME, jdbcType=VARCHAR}
 </when>
 <when test="BEGINTIME != null and BEGINTIME != ''">
 	<![CDATA[ AND time  > #{BEGINTIME, jdbcType=TIMESTAMP} ]]>
 </when>
 <when test="ENDTIME != null and ENDTIME != ''">
 	<![CDATA[ AND time  < #{ENDTIME, jdbcType=TIMESTAMP} ]]>
 </when>
 <otherwise></otherwise>
</choose>

猜你喜欢

转载自injavawetrust.iteye.com/blog/2293798
今日推荐