mybatis批量修改

单个修改会消耗mysql性能,使用批量修改功能

<resultMap type="FanGoods" id="FanGoodsResult">
        <result property="id" column="id"    />
        <result property="goodsName" column="goodsName"    />
        <result property="companyId" column="companyId"    />
        <result property="itemNo" column="itemNo"    />
        <result property="image" column="image"    />
        <result property="num" column="num"    />
        <result property="price" column="price"    />
        <result property="isRecycle" column="isRecycle"    />
        <result property="supplierId" column="supplierId"    />
        <result property="supplierName" column="supplierName"    />
        <result property="createTime" column="createTime"    />
        <result property="createId" column="createId"    />
        <result property="updateTime" column="updateTime"    />
        <result property="updateId" column="updateId"    />
    </resultMap>

<update id="updateFanGoodsList" parameterType="FanGoods">
        <foreach collection="list" item="item" index="index" separator=";">
            update fan_goods
            <set>
                <if test="item.goodsName != null and item.goodsName != ''">goodsName = #{item.goodsName}, </if>
                <if test="item.companyId != null and item.companyId != ''">companyId = #{item.companyId}, </if>
                <if test="item.itemNo != null and item.itemNo != ''">itemNo = #{item.itemNo}, </if>
                <if test="item.image != null and item.image != ''">image = #{item.image}, </if>
                <if test="item.num != null and item.num != ''">num = #{item.num}, </if>
                <if test="item.price != null and item.price != ''">price = #{item.price}, </if>
                <if test="item.isRecycle != null and item.isRecycle != ''">isRecycle = #{item.isRecycle}, </if>
                <if test="item.supplierId != null and item.supplierId != ''">supplierId = #{item.supplierId}, </if>
                <if test="item.supplierName != null and item.supplierName != ''">supplierName = #{item.supplierName}, </if>
                <if test="updateTime != null ">updateTime = #{updateTime}, </if>
                <if test="item.updateId != null and item.updateId != ''">updateId = #{item.updateId}</if>
            </set>
            where id = #{item.id}
        </foreach>
    </update>

 

然后是service类

Map<String,Object> map = new HashMap<>();
map.put("updateTime",new Date());
map.put("list",list);
//先修改仓库数据 仓库数据修改主要是数量和总价
fanGoodsMapper.updateFanGoodsList(map);

执行之后可能会报错

 Cause: java.sql.SQLSyntaxErrorException: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'update fan_goods
             SET goodsName = '会场话筒', 
                c' at line 15

原因是mybatis中的批量修改携带了分号;

需要配置mysql的url,添加allowMultiQueries=true

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值