Mysql Mybatis批量更新寫法
阿新 • • 發佈:2018-12-21
Mapper介面
/**
* 批量同步更新資料
* @param collectionBids
*/
void updateSyncs(@Param("collectionBids") List<CollectionBid> collectionBids);
表字段
`user_id` bigint(20) unsigned NOT NULL DEFAULT '0' COMMENT '使用者id'
`name` varchar(50) DEFAULT NULL COMMENT '使用者暱稱'
mybatis更新語句
<update id="updateSyncs" parameterType="list"> UPDATE `t_collection_bid` <trim prefix="set" suffixOverrides=","> <trim prefix="user_id=case" suffix="end,"> <foreach collection="collectionBids" item="c" index="index"> <if test="c.userId!=null"> when order_number=#{c.orderNumber} then #{c.userId} </if> </foreach> </trim> <trim prefix="name =case" suffix="end,"> <foreach collection="collectionBids" item="c" index="index"> <if test="c.name!=null"> when order_number=#{c.orderNumber} then #{c.name} </if> </foreach> </trim> </trim> WHERE <foreach collection="collectionBids" separator="or" item="c" index="index"> order_number=#{c.orderNumber} </foreach> </update>