1.說明
sql如下:
SELECT t.contract_id FROM `t_plm_contract_monitor` t WHERE 1=1 ANd (t.whole_id_one in ( 77 ) or t.whole_id_five in ( 77 ) or t.whole_id_six in ( 505602294 ))
說明:
t.whole_id_one,
t.whole_id_two,
t.whole_id_three,
t.whole_id_four,
t.whole_id_five,
t.whole_id_six
這些參數可能存在,也可能不存在,就要動態的平裝語句了。
2.做法【部分代碼進行說明】
<if test="(model.wholeIdOneList != null and model.wholeIdOneList.size()> 0) or (model.wholeIdTwoList != null and model.wholeIdTwoList.size()> 0) or (model.wholeIdThreeList != null and model.wholeIdThreeList.size()> 0) or (model.wholeIdFourList != null and model.wholeIdFourList.size()> 0) or (model.wholeIdFiveList != null and model.wholeIdFiveList.size()> 0) or (model.wholeIdSixList != null and model.wholeIdSixList.size()> 0)"> and ( </if> <if test="model.wholeIdOneList != null and model.wholeIdOneList.size()> 0"> t.whole_id_one in <foreach collection="model.wholeIdOneList" open="(" close=")" item="departmentId" separator=","> #{departmentId,jdbcType=BIGINT} </foreach> </if> <choose> <when test="(model.wholeIdOneList == null or model.wholeIdOneList.size()==0) and (model.wholeIdTwoList != null and model.wholeIdTwoList.size()> 0)"> t.whole_id_two in <foreach collection="model.wholeIdTwoList" open="(" close=")" item="departmentId" separator=","> #{departmentId,jdbcType=BIGINT} </foreach> </when> <when test="(model.wholeIdOneList != null and model.wholeIdOneList.size()> 0) and (model.wholeIdTwoList != null and model.wholeIdTwoList.size()> 0)"> OR t.whole_id_two in <foreach collection="model.wholeIdTwoList" open="(" close=")" item="departmentId" separator=","> #{departmentId,jdbcType=BIGINT} </foreach> </when> </choose>
。。。。。。。
首先:根據值是否存在,將是否有這段邏輯的最外層的括號加進來
然后,使用test進行判斷list,如果前面的都不存在條件,第n個存在,則不需要加上or連接符
如果已經不是第一項了,則需要加上or。
3.對choose的說明
在比較規范的標簽里,存在when和otherwise。
但是有些場景下是不需要的,則可以不寫otherwise。經過驗證是沒有問題的。