今天使用mybatis開發公司中台項目踩的一個坑,分享並記錄一下
踩坑前因:因項目中比較多狀態字段,用了大量的Integer 0和1進行判斷
在功能做完后只是粗略的點了下覺得沒多大問題(來自程序員強大的自信),便提交了代碼,很不巧的是剛好領導在做功能測試,發現了功能缺陷,主角來了:
在做牧戶查詢時所有的0判斷均無效,而1有效。查閱資料得知在if語句做如下判斷時intger類型0也視為false
<if test="status != null and status !=''">and status = #{status}</if>
解決方案有二:
1、<if test="status != null ">and status = #{status}</if> 直接判斷!=null即可,只有字符串才需要判斷!=""。
2、或者這樣寫 <if test="status != null and status !='' or status==0 ">and status = #{status}</if>
附上大神詳細解析鏈接:https://www.jianshu.com/p/91ed365c0fdd