使用ssm編寫的一個功能輸入機器編號,開始時間,結束時間,查找實現過程中,當機器編號輸入一個不存在的值,或者開始時間空著,都會報錯,錯誤顯示是sql語句錯誤mybatis的xml文件中,sql語句如下:<select id="findByRowBounds" resultMap="BaseResultMap">?? ??? ?select?? ??? ?<include refid="Base_Column_List" />?? ??? ?from yujing_record?? ??? ?<where>?? ??? ??? ?<if test="macid != null? and macid !=''">?? ??? ??? ??? ?and macid like concat('%',#{macid},'%')?? ??? ??? ?</if>?? ??? ??? ?<if test="start != null and start !=''">?? ??? ??? ??? ?and time <![CDATA[>]]>?? ??? ??? ??? ?#{start}?? ??? ??? ?</if>?? ??? ??? ?<if test="end != null and end !=''">?? ??? ??? ??? ?and time <![CDATA[<]]>?? ??? ??? ??? ?#{end}?? ??? ??? ?</if>?? ??? ?</where>?? ??? ?order by time desc limit #{sindex},#{size}?? ?</select>
2 回答

sunbohan00
TA貢獻44條經驗 獲得超73個贊
if判斷語句的問題,可能是我見得少吧,你這么寫的我確實沒見過,
當空值的時候,if判斷沒有生效,所以空值進行搜索會報錯。
試試這樣
?<if end != null and end !=“”>
?? ??? ??? ??? ?and time?? #{end}
?</if>
添加回答
舉報
0/150
提交
取消