mysql 批量新增,修改

批量新增
<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>





原文地址:https://www.cnblogs.com/SeaWxx/p/8461895.html