1. 程式人生 > >java~springboot~ibatis陣列in查詢的實現

java~springboot~ibatis陣列in查詢的實現

在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可以讓我們的程式碼變簡潔,當然也有缺陷,就是會寫很多弱型別的屬性名。