原mapper的sql語句如下:
<select id="countOutFinishedOrdersByDeliverSign" resultType="int" >
select
count(1) cnt
from
order_customer
where
status = #{status}
<if test="sign!=null and sign!='' and sign=='1'">
AND tms_delivery_flag is null
</if>
<if test="sign!=null and sign!='' and sign=='2'">
AND tms_delivery_flag = 1
</if>
and last_modified_date > #{startDate}
and last_modified_date <= #{endDate}
</select>
以上:sign是字符串,判斷等於的條件時,使用了sign=='1' 和sign=='2'
由於項目暫時還沒有集成p6spy,沒辦法打印出sql,所以,自己按照條件拼接的sql語句到Navicat中執行,條件不一樣,執行結果也不一樣。但是程序接口返回的結果集卻是一樣的。
分析原因:
sql沒有問題,查詢結果沒有問題。然後嘗試去掉了其中的判斷條件 AND tms_delivery_flag is null 或者 AND tms_delivery_flag = 1,發現sql查詢結果是一樣的,也就是說這個條件在查詢中就沒有發揮實際的作用。
綜上認爲:mybatis在if判斷時,映射出了問題。
於是搜索mybatis判斷字符串相等:
mybatis 映射文件中,if標籤判斷字符串相等,兩種方式:因爲mybatis映射文件,是使用的ognl表達式,所以在判斷字符串sex變量是否是字符串Y的時候使用
<test="sex=='Y'.toString()">或者<test = 'sex== "Y"'>
修改if條件:
<select id="countOutFinishedOrdersByDeliverSign" resultType="int" >
select
count(1) cnt
from
order_customer
where
status = #{status}
<if test="sign!=null and sign!='' and sign=='1'.toString()">
AND tms_delivery_flag is null
</if>
<if test="sign!=null and sign!='' and sign=='2'.toString()">
AND tms_delivery_flag = 1
</if>
and last_modified_date > #{startDate}
and last_modified_date <= #{endDate}
</select>
結果返回正常。