mybatis裡面xml中SQL語句if語句裡巢狀if語句
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>
注意顏色一樣的標記便於理解
關注微信公共號 戒色專家 擁有健康的生活態度。