不嚴謹的寫法,可能會報錯:in (),這種情況不符合SQL的語法,導致程序報錯。
如果簡單只做非空判斷,這樣也有可能會有問題:本來in一個空列表,應該是沒有數據才對,卻變成了獲取全部數據!
所以一個比較周全的方法是:
<select id="findLastPoolTaskIdsForMo" resultMap="poolTaskResult">
SELECT MIN(p.pool_task_id) AS pool_task_id
FROM pool_task p
WHERE r_type != 2
<if test="moCodeList != null and moCodeList.size>0">
AND p.mo_code IN
<foreach collection="moCodeList" item="item" index="index" open="(" close=")" separator=",">
#{item}
</foreach>
</if>
<if test="moCodeList==null or moCodeList.size==0">and 1=0</if>
GROUP BY p.mo_code
</select>
如上,加2個if判斷,如果為空則永遠讓語句查詢不到數據
