您的位置:首页 > 数据库

mybatis里面xml中SQL语句if语句里嵌套if语句

2016-02-24 11:35 1256 查看
mybatis里面的if语句需要加上(但是有时不需要 但是(又是在if语句中的

案例一  不完善

<select id="selectRechargeInfoChainCount" resultType="int">

      select count(*) from t_group_card_recharge_info  

  <where>

     <if test="cardNo!=null and cardNo!=''">

     and card_no =#{cardNo}

     </if>

      <if test="mobile!=null and mobile!=''">

     and mobile =#{mobile}

     </if>

    <if test="null!=beginDate">

    and back_time  <![CDATA[>=]]> #{beginDate}

    </if>

     <if test="null!=endDate">

    and back_time <![CDATA[<=]]> #{endDate}

    </if>

    <if test="clinicId!=null and clinicId!=''">

    and

    (  //这个括号与下一个括号一一对应  但是下面不满足时会报错,上面的括号是多余的  

     clinic_id  =#{clinicId}

     </if>

     <if test="sysId!=null and sysId!='' and sysId != -1">

     or  sys_id =#{sysId}   )

     </if>

   

    </where>   

  </select>

案例二完善 案例一

<select id="selectRechargeInfoChainCount" resultType="int">

      select count(*) from t_group_card_recharge_info  

  <where>

     <if test="cardNo!=null and cardNo!=''">

     and card_no =#{cardNo}

     </if>

      <if test="mobile!=null and mobile!=''">

     and mobile =#{mobile}

     </if>

    <if test="null!=beginDate">

    and back_time  <![CDATA[>=]]> #{beginDate}

    </if>

     <if test="null!=endDate">

    and back_time <![CDATA[<=]]> #{endDate}

    </if>

    <if test="clinicId!=null and clinicId!=''">

    and
    <if test="sysId!=null and sysId!='' and sysId != -1">

    (

     </if> //这样不会因为下面不满足而多出括号

     clinic_id  =#{clinicId}

     </if>

     <if test="sysId!=null and sysId!='' and sysId != -1">

     or  sys_id =#{sysId} )

     </if>

   

    </where>   

  </select>

注意颜色一样的标记便于理解

关注微信公共号  戒色专家
拥有健康的生活态度。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息