批量新增
<insert id="insertList">
insert into sea_user (id, username,`time`)
values
<foreach collection="list" item="item" index="index" separator=",">
(replace(uuid(),"-",""), #{item.username}, #{item.time})
</foreach>
</insert>
註釋
list 傳過來的集合對象
item="item" "item" 遍歷的集合中的每一個對象
---------------------------------------------------------------------------------------------------------
新增返回主鍵id (useGeneratedKeys="true" keyProperty="id")
<insert id="insert" parameterType="com.mmall.pojo.Shipping" useGeneratedKeys="true" keyProperty="id">
insert into mmall_shipping (id, user_id, receiver_name,
receiver_phone, receiver_mobile, receiver_province,
receiver_city, receiver_district, receiver_address,
receiver_zip, create_time, update_time
)
values (#{id,jdbcType=INTEGER}, #{userId,jdbcType=INTEGER}, #{receiverName,jdbcType=VARCHAR},
#{receiverPhone,jdbcType=VARCHAR}, #{receiverMobile,jdbcType=VARCHAR}, #{receiverProvince,jdbcType=VARCHAR},
#{receiverCity,jdbcType=VARCHAR}, #{receiverDistrict,jdbcType=VARCHAR}, #{receiverAddress,jdbcType=VARCHAR},
#{receiverZip,jdbcType=VARCHAR}, now(), now()
)
</insert>
---------------------------------------------------------------------------------------------------------
批量修改
list - 傳過來的數據集合,使用註解
goods_id 表中數據
goodsId 對應的實體類屬性
<update id="updateBatchStock">
update
goods
set
goods_stock =
<foreach collection="list" item="item" index="index" separator=" " open="case goods_id" close="end"> when #{item.goodsId} then goods_stock - #{item.quantity} </foreach> ,goods_sales_quantity = <foreach collection="list" item="item" index="index" separator=" " open="case goods_id" close="end"> when #{item.goodsId} then goods_sales_quantity + #{item.quantity} </foreach> where goods_id in <foreach collection="list" index="index" item="item" separator="," open="(" close=")"> #{item.goodsId} </foreach></update>