java~springboot~ibatis數組(zu)in查詢的實現
在ibatis的xml文件里,我們去寫sql語句,對應mapper類的方法,這些sql語句與控制臺上沒什么兩樣,但在有些功能上需要注意,如where in這(zhe)種從數組(zu)里查詢符合條件的集(ji)合里,需要在(zai)xml里進行(xing)特別的處理。
<update id="batchUpdate" parameterType="map">
update customer_info set status=#{status},appoint_time=#{appointTime} where
customer_id in
<foreach collection="customerIdArr" item="customerId"
index="index" open="(" close=")" separator=",">
#{customerId}
</foreach>
</update>
我們可以(yi)看到(dao),在xml里進行(xing)了(le)foreach的(de)遍歷(li),而(er)外部(bu)參數是一個集合或者數組的(de)對象,我們在xml對它(ta)進行(xing)遍歷(li),還是比較方(fang)便的(de)。
技巧(qiao):在xml里,parameterType是(shi)輸入參數類(lei)型,你可(ke)(ke)以使(shi)用(yong)map對象來(lai)代替;而(er)resultType是(shi)返(fan)回類(lei)型,如果你沒(mei)有(you)定義(yi)DTO也可(ke)(ke)以使(shi)用(yong)map代替,雖然map可(ke)(ke)以讓我們(men)的代碼變(bian)簡潔,當(dang)然也有(you)缺陷,就是(shi)會寫很多(duo)弱類(lei)型的屬性名。