MyBatis——案例——查询-单条件查询-动态条件查询
单条件查询-动态条件查询(choose(when,otherwise))
  
从多个条件中选择一个
choose(when,otherwise) 选择,类似于java中的Switch语句(when 相当于 case otherwise 相当于 default)
<!-- 单条件查询-动态查询 -->
    <select id="selectByConditionSingle" resultMap="brandResultMap">
        select * from tb_brand
        where
            <choose>
                <when test="status != null">
                    status = #{status}
                </when>
                <when test="companyName != null and companyName != ''">
                     company_name like #{companyName}
                </when>
                <when test="brandName != null and brandName != ''">
                    brand_name like #{brand_name}
                </when>
                <otherwise>
                    1 = 1
                </otherwise>
            </choose>
    </select>
默认条件使用otherwise是不太方便的,可以使用上一节的 where标签即可
    <!-- 单条件查询-动态查询 -->
    <select id="selectByConditionSingle" resultMap="brandResultMap">
        select * from tb_brand
        <where>
            <choose>
                <when test="status != null">
                    status = #{status}
                </when>
                <when test="companyName != null and companyName != ''">
                     company_name like #{companyName}
                </when>
                <when test="brandName != null and brandName != ''">
                    brand_name like #{brand_name}
                </when>
            </choose>
        </where>
    </select>

                
            
        
浙公网安备 33010602011771号