場景
前端傳遞一個部門id的數組作為查詢條件查詢部門id在這個數組中的數據。
在MyBatis的xml中獲取到了這個數組參數后怎樣進行if-test的判空與長度判斷。
注:
博客:
https://blog.csdn.net/badao_liumang_qizhi
關注公眾號
霸道的程序猿
獲取編程相關電子書、教程推送與免費下載。
實現
在mapper接口層傳遞數組參數
public List<KqDksz> selectKqDkszListBySx(@Param("array") int[] bmids, String xm, String dkzt);
然后在對應的xml中
<select id="selectKqDkszListBySx" resultMap="KqDkszResult"> <include refid="selectKqDkszVoJoinJibenXinXi"/> <where> <if test="array != null and array.length >0"> and j.bmid in <foreach collection="array" item="item" open="(" separator="," close=")"> ${item} </foreach> </if> </where> </select>
這里就可以通過array獲取傳遞的數組並遍歷。
這里的數組參數不是必傳的,所以需要加if-test的判斷
<if test="array != null and array.length >0">
首先判斷其不為空,並且判斷其長度大於0。因為有可能傳遞空數組的情況。
因為這里是傳遞的數組對象,所以這里是使用的array.length屬性
注意length不是方法
但是如果傳遞的參數時list的話,就要使用如下方式
<if test="list != null and list.size() > 0">