XML中批量操作添加与更新

1.批量添加

 - Mapper中:
 int insertBatchSelective(@Param(value = "honorPeopleList") List<DatagoHonorPerson> honorPeopleList);
 - XML中:
<!--批量添加荣誉劳模数据-->
    <insert id="insertBatchSelective" parameterType="java.util.List">
        insert into datago_honor_person(id,department_id,department_name,code,`name`,phone,create_time,update_time)
        values
        <foreach collection="honorPeopleList" item="item" index="index" separator=",">
            (
            #{item.id,jdbcType=VARCHAR},
            #{item.departmentId,jdbcType=VARCHAR},
            #{item.departmentName,jdbcType=VARCHAR},
            #{item.code,jdbcType=VARCHAR},
            #{item.name,jdbcType=VARCHAR},
            #{item.phone,jdbcType=VARCHAR},
            #{item.createTime,jdbcType=TIMESTAMP},
            #{item.updateTime,jdbcType=TIMESTAMP}
            )
        </foreach>
    </insert>

2.更新

 - mapper中:
  int  updateByBatch(@Param(value = "updatePersonList")List<DatagoActivityPerson> updatePersonList);
 - XML中:
 <!--批量更新-->
    <update id="updateByBatch" parameterType="java.util.List">
        <foreach collection="updatePersonList" item="itemUpdate" index="index" separator=";">
            update datago_activity_person
            <set>
                <if test="itemUpdate.code != null">
                    `code`=#{itemUpdate.code,jdbcType=VARCHAR},
                </if>
                <if test="itemUpdate.name != null">
                    `name`=#{itemUpdate.name,jdbcType=VARCHAR},
                </if>
                <if test="itemUpdate.phone != null">
                    phone =#{itemUpdate.phone,jdbcType=VARCHAR},
                </if>
                <if test="itemUpdate.createTime != null">
                    create_time=#{itemUpdate.createTime,jdbcType=TIMESTAMP},
                </if>
                <if test="itemUpdate.activityId != null">
                    activity_id=#{itemUpdate.activityId,jdbcType=VARCHAR},
                </if>
                <if test="itemUpdate.activityTime != null">
                    activity_time=#{itemUpdate.activityTime,jdbcType=VARCHAR},
                </if>
                <if test="itemUpdate.departmentId != null">
                    department_id=#{itemUpdate.departmentId,jdbcType=VARCHAR},
                </if>
                <if test="itemUpdate.stepId != null">
                    step_id=#{itemUpdate.stepId,jdbcType=VARCHAR},
                </if>
                <if test="itemUpdate.stepName != null">
                    step_name=#{itemUpdate.stepName,jdbcType=VARCHAR},
                </if>
                <if test="itemUpdate.flowId != null">
                    flow_id=#{itemUpdate.flowId,jdbcType=VARCHAR},
                </if>
                <if test="itemUpdate.stepDetailsId != null">
                    step_details_id=#{itemUpdate.stepDetailsId,jdbcType=VARCHAR},
                </if>
                <if test="itemUpdate.flag != null">
                    flag=#{itemUpdate.flag,jdbcType=INTEGER},
                </if>
                <if test="itemUpdate.flagInfo != null">
                    flag_info=#{itemUpdate.flagInfo,jdbcType=VARCHAR},
                </if>
                <if test="itemUpdate.isHonor != null">
                    is_honor=#{itemUpdate.isHonor,jdbcType=VARCHAR},
                </if>
                <if test="itemUpdate.flagExplain != null">
                    flag_explain=#{itemUpdate.flagExplain,jdbcType=VARCHAR},
                </if>
                <if test="itemUpdate.updateTime != null">
                    update_time=#{itemUpdate.updateTime,jdbcType=TIMESTAMP},
                </if>
            </set>
            where id = #{itemUpdate.id,jdbcType=VARCHAR}
        </foreach>
    </update>

3.这样运行还是会报错,因为再Mysql数据库连接时必须这样设置(允许批量更新allowMultiQueries=true)。

  #mysql8.0
    url: jdbc:mysql://127.0.0.1:3306/db_datago_find?useUnicode=true&characterEncoding=UTF-8&serverTimezone=CTT&zeroDateTimeBehavior=convertToNull&useSSL=false&allowPublicKeyRetrieval=true&serverTimezone=GMT%2B8&allowPublicKeyRetrieval=true&allowMultiQueries=true
    driver-class-name: com.mysql.cj.jdbc.Driver
    username: root
    password: root
上一篇:List插入数据库报错oracle-00926:缺失VALUES关键字


下一篇:Shell for循环(2)- 语法二简介及批量添加删除用户