<!-- 此处使用if比较是否相等 -->
范例一:
<select id="queryOrderListPage" parameterType="java.util.Map"
resultType="java.util.Map">
SELECT
EDU_ORDERS.ORDER_ID as orderId,
EDU_ORDERS.ORDER_NO as orderNo,
EDU_ORDERS.USER_ID as userId,
EDU_ORDERS.SUM_MONEY as sumMoney,
EDU_ORDERS.STATES as states,
EDU_ORDERS.CREATE_TIME as createTime,
EDU_ORDERS.PAY_TIME as payTime,
EDU_ORDERS.PAY_TYPE as payType,
EDU_ORDERS.out_trade_no as outTradeNo,
EDU_USER.EMAIL as email,
EDU_USER.MOBILE as mobile
FROM EDU_ORDERS
LEFT JOIN EDU_USER ON
EDU_USER.USER_ID = EDU_ORDERS.USER_ID
left join edu_trxorder_detail on
EDU_ORDERS.ORDER_NO = edu_trxorder_detail.request_id
<where>
<if test="e.keyWord!=null and e.keyWord!=''">
AND (EDU_ORDERS.ORDER_NO LIKE
CONCAT('%',#{e.keyWord},'%')
OR EDU_USER.EMAIL LIKE
CONCAT('%',#{e.keyWord},'%')
OR EDU_USER.MOBILE LIKE
CONCAT('%',#{e.keyWord},'%')
OR EDU_ORDERS.out_trade_no LIKE
CONCAT('%',#{e.keyWord},'%')
)
</if>
<if test="e.beginCreateTime!=null and e.beginCreateTime!=''">
AND EDU_ORDERS.CREATE_TIME >= #{e.beginCreateTime}
</if>
<if test="e.endCreateTime!=null and e.endCreateTime!=''">
AND <![CDATA[EDU_ORDERS.CREATE_TIME <= #{e.endCreateTime}]]>
</if>
<if test="e.beginPayTime!=null and e.beginPayTime!=''">
AND EDU_ORDERS.PAY_TIME >= #{e.beginPayTime}
</if>
<if test="e.endPayTime!=null and e.endPayTime!=''">
AND <![CDATA[ EDU_ORDERS.PAY_TIME <= #{e.endPayTime}]]>
</if>
<if test="e.userId>0">
AND EDU_ORDERS.USER_ID=#{e.userId}
</if>
<if test="e.states!=null and e.states!='' and e.states!='all'.toString()">
AND EDU_ORDERS.STATES=#{e.states}
</if>
<if test="e.payType!=null and e.payType!=''and e.payType!='CTOC'.toString()">
AND EDU_ORDERS.PAY_TYPE = #{e.payType}
</if>
<if test="e.payType!=null and e.payType!=''and e.payType=='CTOC'.toString()">
AND (EDU_ORDERS.PAY_TYPE = 'ALIPAY' OR EDU_ORDERS.PAY_TYPE = 'WEIXIN')
</if>
<if test="e.courseId!=null and e.courseId!=0">
AND edu_trxorder_detail.course_id = #{e.courseId}
</if>
</where>
group by EDU_ORDERS.ORDER_ID
ORDER BY EDU_ORDERS.CREATE_TIME DESC
</select>
范例二:
mybatis 映射文件中,if标签判断字符串相等,两种方式:
因为mybatis映射文件,是使用的ognl表达式,所以在判断字符串isComplete变量是否是字符串Y的时候
<if test="isComplete=='Y'.toString()">
或者使用下面的写法
<if test = 'isComplete== "Y"'>
注意:不能使用以下方式
<if test="isComplete=='Y'"> and 1=1 </if>
因为mybatis会把'Y'解析为字符,java是强类型语言,所以不能这样写。