Notes for batch updates

Keywords: Programming Mybatis xml Java

One of my younger brother's needs yesterday was to say that a list set update was put into a table, but it was different from insert. List needs to be encapsulated as map for foreach in incoming xml

<update id="updateYesterdayAmountBatch" parameterType="java.util.Map">
        update debt_current_user_holding_temp dcu set
        dcu.yesterday_amount =
        <foreach collection="tt" item="debtCurrentUserHoldingTemp" index="index" separator=" " open="case id" close="end,">
            when #{debtCurrentUserHoldingTemp.id} then
            #{debtCurrentUserHoldingTemp.yesterdayAmount}
        </foreach>
        dcu.amount =
        <foreach collection="tt" item="debtCurrentUserHoldingTemp" index="index" separator=" " open="case id" close="end,">
            when #{debtCurrentUserHoldingTemp.id} then
            #{debtCurrentUserHoldingTemp.amount}
        </foreach>
        dcu.updated_at =
        <foreach collection="tt" item="debtCurrentUserHoldingTemp" index="index" separator=" " open="case id" close="end">
            when #{debtCurrentUserHoldingTemp.id} then
            #{debtCurrentUserHoldingTemp.updatedAt}
        </foreach>
        where dcu.id in
        <foreach collection="tt" index="index" item="debtCurrentUserHoldingTemp" separator="," open="(" close=")">
            #{debtCurrentUserHoldingTemp.id}
        </foreach>
    </update>

Note here that the field following the set sets sets sets open s to case id close = end, and where conditions use in because mybatis parses its condition in (1, 2, 3) and so on. The field after set is resolved to case id when x x then x when x x x then x x end, the next field, and the last field is end without comma.

int updateYesterdayAmountBatch(Map<String, Object> tt);

These are the contents of mapper. This allows for batch updates of mybatis. The reason is that mybatis automatically encapsulates list-type parameters into a map by default and key= list, where vaue is a collection. Sometimes it's okay to pass in a list directly. Do not know why? According to my writing. If you pass in the list parameter directly, an error will be reported, parameter xxx not found. I don't know what the problem is. Follow-up research comes out and then supplements.

Inserts can also be stored in batches in the same way.

Posted by TheStalker on Thu, 24 Jan 2019 21:30:14 -0800