mybaits下面标签choose 标签

choose (when, otherwise)
有时候我们并不想应用所有的条件,而只是想从多个选项中选择一个。MyBatis提供了choose 元素,按顺序判断when中的条件出否成立,如果有一个成立,则choose结束。当choose中所有when的条件都不满则时,则执行 otherwise中的sql。类似于Java 的switch 语句,choose为switch,when为case,otherwise则为default。
if是与(and)的关系,而choose是或(or)的关系。

<!-- 查询学生list,like姓名、或=性别、或=生日、或=班级,使用choose -->   
<select id="getStudentListChooseEntity" parameterType="StudentEntity" resultMap="studentResultMap">   
    SELECT * from STUDENT_TBL ST    
    <where>   
        <choose>   
            <when test="studentName!=null and studentName!='' ">   
                    ST.STUDENT_NAME LIKE CONCAT(CONCAT('%', #{studentName}),'%')    
            </when>   
            <when test="studentSex!= null and studentSex!= '' ">   
                    AND ST.STUDENT_SEX = #{studentSex}    
            </when>   
            <when test="studentBirthday!=null">   
                AND ST.STUDENT_BIRTHDAY = #{studentBirthday}    
            </when>   
            <when test="classEntity!=null and classEntity.classID !=null and classEntity.classID!='' ">   
                AND ST.CLASS_ID = #{classEntity.classID}    
            </when>   
            <otherwise>   
                    
            </otherwise>   
        </choose>   
    </where>   
</select> 

在在选择的情况之下 然后对sql语句进行添加或者 选用。在进行逻辑判断的时候比较方便。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值