mybatis-choose标签,when标签和otherwise标签,查询时选择一个其中某一个属性查询

<select id="getStudents" resultType="student" parameterType="map">
    select * from mybatis_test.student
    <where>
        <choose>
            <when test="id != null">
                id = #{id}
            </when>
            <when test="name != null">
                and name = #{name}
            </when>
            <otherwise>
                and tid = #{tid}
            </otherwise>
        </choose>
    </where>
</select>

解释:map对象中有三个属性:id,name,tid。

        如果id不为空则匹配id,下面的name和tid都不匹配,

        如果name不为空则匹配name,下面的id和tid都不匹配。

        如果id和name都为空则执行otherwise的tid匹配。

  1. choose标签,when标签,otherwise标签组合起来就相当于java中的switch语句。
  2. 当满足其中一个when标签中的条件时,将不再执行其他的when标签和otherwise标签
  3. 当所有的when标签中的条件均未满足时,执行othewise标签

如果想不一定要执行otherwise,可以在里面套<if>

<select id="query" parameterType="com.ICEvidenceDocumentTemplateQueryDTO" resultMap="ICEvidenceDocumentTemplateResult">
        <include refid="selectTemplate" />
        <where>
            <choose>
                <when test="id_ != null">ID_ = #{id_, jdbcType=INTEGER}</when>
                <when test="templateName != null and templateName != ''">
                    TEMPLATE_NAME=#{templateName, jdbcType=VARCHAR}
                </when>
                <otherwise>
                    <if test="templateNameDescription != null and templateNameDescription != ''"> and TEMPLATE_NAME like concat("%",#{templateNameDescription},"%")
                        or ID_ = #{templateNameDescription}
                    </if>
                </otherwise>
            </choose>
            and ISDELETED = '0'
        </where>
        order by
        ID_ asc
</select>

这样如果前面都为空,不符合,则最后的otherwise里面的if也为空时,则仅匹配外面ISDELETED = '0'的数据。

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值