在ibatis的xml文件里,我們去寫sql語句,對應mapper類的方法,這些sql語句與控制台上沒什么兩樣,但在有些功能上需要注意,如where in
這種從數組里查詢符合條件的集合里,需要在xml里進行特別的處理。
<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>
我們可以看到,在xml里進行了foreach的遍歷,而外部參數是一個集合或者數組的對象,我們在xml對它進行遍歷,還是比較方便的。
技巧:在xml里,parameterType是輸入參數類型,你可以使用map對象來代替;而resultType是返回類型,如果你沒有定義DTO也可以使用map代替,雖然map可以讓我們的代碼變簡潔,當然也有缺陷,就是會寫很多弱類型的屬性名。