mysql批量新增和修改示例(foreach 使用)

批量新增:java

in的map中的list查询mysql

<!-- 获取商品分类 -->
<select id="getGoodClass" parameterType="list" resultMap="shopGoodClassRVO">
SELECT T.GC_ID,
       T.GC_NAME,
       T.TYPE_ID,
       T.TYPE_NAME,
       T.STORE_ID,
       T.GC_PARENT_ID,
       T.GC_SORT,
       T.GC_SHOW,
       T.GC_KEYWORD,
       T.CREATORID,
       T.CREATETIME,
       T.UPDATEID,
       T.UPDATETIME,
       T.DELFLAG
  FROM T_SHOP_GOODS_CLASS T
  WHERE T.DELFLAG = '0'
<if test="type == 1" >
AND t.gc_parent_id is null
</if>
<if test ="gc_parent_id != null">
    AND T.GC_PARENT_ID IN 
    <foreach collection ="gc_parent_id" item="item" index= "index" open="(" separator="," close=")">
            #{item.gcId}
        </foreach >
</if>
  ORDER BY T.GC_SORT,T.GC_ID
</select>
sql

<!-- 批量新增接收对象 -->
  <insert id="addRecive" parameterType="java.util.List">
insert into t_news_important_sendee
            (NI_ID, 
        DEPART_ID,
        U_ID,
            CREATORID,
            CITY_NAME)
            values
        <foreach collection ="list" item="item" index= "index" separator =",">
            (
            #{item.niId},
            #{item.departId},
            #{item.uId},
            #{item.creatorid},
            #{item.cityName})
        </foreach >
  </insert>
api

批量修改/删除
spa

批量修改是jdbc的配置必须加个&allowMultiQueries=true;对象

如:jdbc:mysql://192.168.1.85:3307/gz_test?useSSL=false&allowMultiQueries=trueci

 <!-- 批量修改员工车牌号-->
  <update id="updateCar" parameterType="java.util.List">
  <foreach collection="list" item="item" index="index" open="" close="" separator=";">
  update t_user_department t 
  set t.UDT_LICENSE_PLATE = #{item.udt_license_plate},
      t.UPDATEID = #{item.updateId} 
      where t.UDT_SAPID = #{item.udt_sapid} 
      </foreach> 
  </update>

  <!-- 批量删除员工-->
  <update id="delStaffs" parameterType="java.util.List">
  <foreach collection="list" item="item" index="index" open="" close="" separator=";">
  update t_user t set t.DELFLAG = '1',t.UPDATEID = #{item.updateid} where t.U_ID = #{item.uId} 
      </foreach> 
  </update>
get