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


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM