Mybatis-XML中批量添加,更新数据

67 篇文章 1 订阅
21 篇文章 4 订阅

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
  • 3
    点赞
  • 25
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值