1、批量插入
<insert id ="insertBatch" parameterType="java.util.List" >
insert into tableName
values
<foreach collection ="list" item="item" index= "index" separator =",">
(
#{item.name},
#{item.logo}
)
</foreach >
</insert>
2、批量更新
<update id="updateBatch" parameterType="java.util.List">
update tableName
set
name =
<foreach collection="list" item="item" index="index" separator=" ">
case id when #{item.id} then #{item.name} end
</foreach> ,
logo =
<foreach collection="list" item="item" index="index" separator=" ">
case id when #{item.id} then #{item.logo } end
</foreach>
where id in
<foreach collection="list" item="item" index="index" separator="," open="(" close=")">
#{item.id}
</foreach>
3、MyBatis Integer类型更新写法
正确:
<if test="name!= null">name= #{name},</if>
错误:
<if test="name != null and name != '' ">name= #{name},</if>
原因:Integer类型为空时默认为0, 0!=''