MyBatis查询sql写法

1.多条件,可全写,可空缺

<select id="selectByCondition" resultMap="brandResultMap">
        select *
        from tb_brand
        <where>
        <if test="status != null">status = #{status}</if>
        <if test="companyName != null and companyName != '' ">and company_name like #{companyName}</if>
        <if test="brandName != null and brandName != '' ">and brand_name like #{brandName}</if>
        </where>
    </select>

2.单条件,但可以选择传入的字段名

<select id="selectByConditionSingle" resultMap="brandResultMap">
        select *
        from tb_brand
        <where>
            <choose>
            <when test="status != null">status = #{status}</when>
            <when test="companyName != null and companyName != '' ">and company_name like #{companyName}</when>
            <when test="brandName != null and brandName != '' ">and brand_name like #{brandName}</when>
            </choose>
        </where>
    </select>

 

posted @ 2022-06-03 13:41  椅念琼  阅读(198)  评论(0编辑  收藏  举报