xml中批量新增数据或者批量修改数据

批量提交多条语句需要在 mysql 的连接里面开启 allowMultiQueries=true
如:url: jdbc:mysql://127.0.0.1:3306/ku?useUnicode=true&characterEncoding=utf-8&allowMultiQueries=true

<insert id="batchInsert" parameterType="com.Entity">
    insert into pos_parameter ( store_id, pos_id,
    parameter_id, parameter_name, parameter_value,
    parameter_desc, parameter_type, is_show,
    is_modify, create_user_id, memo,
    created_time, updated_time)
    values
  <foreach collection="list"  item="param" index="index" separator=",">
    ( #{param.storeId,jdbcType=VARCHAR}, #{param.posId,jdbcType=VARCHAR},
    #{param.parameterId,jdbcType=VARCHAR}, #{param.parameterName,jdbcType=VARCHAR}, #{param.parameterValue,jdbcType=VARCHAR},
    #{param.parameterDesc,jdbcType=VARCHAR}, #{param.parameterType,jdbcType=INTEGER}, #{param.isShow,jdbcType=CHAR},
    #{param.isModify,jdbcType=CHAR}, #{param.createUserId,jdbcType=VARCHAR}, #{param.memo,jdbcType=VARCHAR},
    #{param.createdTime,jdbcType=TIMESTAMP}, #{param.updatedTime,jdbcType=TIMESTAMP})
  </foreach>
</insert>
<update id="batchUpdate" parameterType="com.Entity">
  <foreach item="param" collection="list" separator=";" index="index">
    update pos_parameter
    <set>
      <if test="param.storeId != null">
        store_id = #{param.storeId,jdbcType=VARCHAR},
      </if>
      <if test="param.posId != null">
        pos_id = #{param.posId,jdbcType=VARCHAR},
      </if>
      <if test="param.parameterId != null">
        parameter_id = #{param.parameterId,jdbcType=VARCHAR},
      </if>
      <if test="param.parameterName != null">
        parameter_name = #{param.parameterName,jdbcType=VARCHAR},
      </if>
      <if test="param.parameterValue != null">
        parameter_value = #{param.parameterValue,jdbcType=VARCHAR},
      </if>
      <if test="param.parameterDesc != null">
        parameter_desc = #{param.parameterDesc,jdbcType=VARCHAR},
      </if>
      <if test="param.parameterType != null">
        parameter_type = #{param.parameterType,jdbcType=INTEGER},
      </if>
      <if test="param.isShow != null">
        is_show = #{param.isShow,jdbcType=CHAR},
      </if>
      <if test="param.isModify != null">
        is_modify = #{param.isModify,jdbcType=CHAR},
      </if>
      <if test="param.createUserId != null">
        create_user_id = #{param.createUserId,jdbcType=VARCHAR},
      </if>
      <if test="param.memo != null">
        memo = #{param.memo,jdbcType=VARCHAR},
      </if>
      <if test="param.createdTime != null">
        created_time = #{param.createdTime,jdbcType=TIMESTAMP},
      </if>
      <if test="param.updatedTime != null">
        updated_time = #{param.updatedTime,jdbcType=TIMESTAMP},
      </if>
    </set>
    where id = #{param.id,jdbcType=BIGINT}
  </foreach>
</update>



原文地址:https://www.cnblogs.com/ding08/p/11686309.html