批量更新稍微复杂一些:
写个例子 :
<update id="xxx" parameterType="list">
update
xxx(这个是表名)
<trim prefix="set" prefixOverrides=",">(这一行是拼接set )
<trim prefix="code = case" suffix="end,">(这个里面的code是需要修改的字段)
<foreach collection="list" item="item">(正常的foreach拼接)
<if test="item.code != null and item.code != ''">
when id = #{item.id} then #{item.code}(当id匹配上之后,修改对应的code)
</if>
</foreach>
</trim>
<trim prefix="name = case" suffix="end">(这个里面的name是需要修改的字段,最后的end不要加,号)
<foreach collection="list" item="item">
<if test="item.name != null and item.name != ''">
when id = #{item.id} then #{item.name}(当id匹配上之后,修改对应的name)
</if>
</foreach>
</trim>
</trim>
<where>
<foreach collection="giftPackageDetailList" item="item" separator="or">
id = #{item.id} (条件,根据id更新)
</foreach>
</where>
</update>
基本上一般的批量修改都可以套用.需要修改多少字段,自己定.
要么全部成功,要么全部失败,使用int去接收返回值,会接收到修改的条数的.
我尝试了一下用map<Long,Bean> 进行传参,但是如果不使用@param进行指定的话,即使是一个入参,也会出现映射为空的情况. 当我去具体指定了那个参数的时候,反而可以了.
官方说,当类型为map的时候,index为键,item为值,在我这里也就是bean.一个入参的话,应该不会出现映射为空的情况啊,list和map还是不一样.list简单一些.map复杂一些.如果是Map<String,Obj>的话,即使obj是一个数组,直接输入数组的名字,然后可以当中collection来使用.但是我那种使用,没有指定具体的映射关系,反而还不行了.
具体的规则不清楚.但是目前的来说,够用了.