5 回答

TA貢獻1797條經驗 獲得超4個贊
在list和數組中,index是元素的序號,在map中,index是元素的key,該參數可選。
<insert id="batchInsert">
insert into
personal_tag(type, tag, create_time, open_account_id)
values
<foreach collection="list" item="item" index="index" separator=",">
(#{item.type,jdbcType=INTEGER},
#{item.tag,jdbcType=VARCHAR},
#{item.createTme,jdbcType=TIMESTAMP},
#{item.openAccountId,jdbcType=BIGINT})
</foreach>ON DUPLICATE KEY UPDATE tag=tag

TA貢獻2016條經驗 獲得超9個贊
ON DUPLICATE KEY UPDATE tag=tag
把這一句改為
ON DUPLICATE KEY UPDATE tag=#{item.tag}

TA貢獻1801條經驗 獲得超8個贊

TA貢獻2065條經驗 獲得超14個贊
<insert id="batchInsert" parameterType="java.util.List">
insert into
personal_tag(type, tag, create_time, open_account_id)
values
<foreach collection="list" item="item" separator=",">
(#{item.type,jdbcType=INTEGER},
#{item.tag,jdbcType=VARCHAR},
#{item.createTme,jdbcType=TIMESTAMP},
#{item.openAccountId,jdbcType=BIGINT})
</foreach>ON DUPLICATE KEY UPDATE tag=tag
</insert>
在insert標簽加參數類型呢
修改下參數別名呢,還有接口的參數名,下面是我們之前的項目里的
void addRiskItemBatch(@Param("lstItem")List<FastRiskItemInfo> lstItem);
<!-- 批量添加條款 -->
<insert id="addRiskItemBatch" useGeneratedKeys="true" parameterType="java.util.List">
<selectKey resultType="long" keyProperty="id" order="AFTER">
SELECT
LAST_INSERT_ID()
</selectKey>
insert into fast_risk_item_info (query_id, risk_item_id,
item_no, item_name, item_price,
status, create_time)
values
<foreach collection="lstItem" item="item" index="index" separator="," >
(#{item.queryId,jdbcType=INTEGER}, #{item.riskItemId,jdbcType=INTEGER},
#{item.itemNo,jdbcType=VARCHAR}, #{item.itemName,jdbcType=VARCHAR}, # {item.itemPrice,jdbcType=DECIMAL},
#{item.status,jdbcType=INTEGER}, #{item.createTime,jdbcType=VARCHAR})
</foreach>
</insert>
添加回答
舉報