MyBatis批量插入|更新数据(MySql)

2017-11-24  本文已影响0人  沉思的老猫

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!=''

参考文献:
https://www.cnblogs.com/Jason-Xiang/p/6558334.html

上一篇下一篇

猜你喜欢

热点阅读