【mybatis】mybatis中批量插入 批量更新 batch 进行insert 和 update,或者切割LIst进行批量操作

转载https://www.cnblogs.com/sxdcgaq8080/p/9494336.html

分别展示 mybatis 批量新增 和 批量更新 的操作:

controller层:

goodsService.batchInsert(insertGoodsList);

goodsService.batchUpdate(updateGoodsList);

service层:

切割List的方法【https://www.cnblogs.com/sxdcgaq8080/p/9376947.html】【建议分批次处理,每次处理1000条】【实际根据每条数据的大小,自行划分】

复制代码
  @Override
@Transactional
public int batchInsert(List list) {
int a = 0;

    List<List<Goods>> goodsAllList = ListUtils.splitListBycapacity(list,1000);
    for (List<Goods> goodsList : goodsAllList) {
        a += mapper.batchInsert(goodsList);
    }

    return a;
}

@Override
@Transactional
public int batchUpdate(List<Goods> list) {
    int a = 0;

    List<List<Goods>> goodsAllList = ListUtils.splitListBycapacity(list,1000);
    Map<String,Object> map = new HashMap<>();
    for (List<Goods> goodsList : goodsAllList) {
        map.put("list",goodsList);
        a += mapper.batchUpdate(map);
    }

    return a;
}

复制代码

Mapper.java层

int batchInsert(List<Goods> list);

int batchUpdate(Map<String,Object> map);

Mapper.xml层

【注意,batchUpdate的原理,是循环拼接sql,一次连接数据库,执行多条update语句】

复制代码

INSERT INTO goods

    (create_date,update_date,create_id,update_id,enabled,
    tenement_id,uid,name,py_all,py_head,
    outer_id,outer_code,mnemonic_code,del_flag,enabled_flag,
    goods_type_uid,url,bar_cide,sale_price,integral,
    scan_name,brand_uid,en_name)

    VALUES

    <foreach collection="list" item="item" separator=",">
        (#{item.createDate},#{item.updateDate},#{item.createId},#{item.updateId},#{item.enabled},
        #{item.tenementId},#{item.uid},#{item.name},#{item.pyAll},#{item.pyHead},
        #{item.outerId},#{item.outerCode},#{item.mnemonicCode},#{item.delFlag},#{item.enabledFlag},
        #{item.goodsTypeUid},#{item.url},#{item.barCide},#{item.salePrice},#{item.integral},
        #{item.scanName},#{item.brandUid},#{item.enName})
    </foreach>
</insert>

<update id="batchUpdate" parameterType="java.util.Map">
    <foreach collection="list" separator=";" item="goods">
        update
            goods 
        SET
            update_date = #{goods.updateDate},
            update_id = #{goods.updateId},
            enabled = #{goods.enabled},
            name = #{goods.name},
            py_all = #{goods.pyAll},
            py_head = #{goods.pyHead},
            outer_code = #{goods.outerCode},
            mnemonic_code = #{goods.mnemonicCode},
            del_flag = #{goods.delFlag},
            enabled_flag = #{goods.enabledFlag},
            goods_type_uid = #{goods.goodsTypeUid},
            url = #{goods.url},
            bar_cide = #{goods.barCide},
            sale_price = #{goods.salePrice},
            integral = #{goods.integral},
            scan_name = #{goods.scanName},
            brand_uid = #{goods.brandUid},
            en_name = #{goods.enName}
        where
            outer_id = #{goods.outerId}
        and
            tenement_id = #{goods.tenementId}

    </foreach>
</update>

复制代码

最后如果,批量插入可以成功,但是批量更新失败,可以参考:https://www.cnblogs.com/sxdcgaq8080/p/10565023.html

最后需要注意的是,如果解决了批量更新问题后,还想按照【https://www.cnblogs.com/sxdcgaq8080/p/9100178.html】打印sql,就失效了,sql就不会打印出来了。!!!!

  • 1
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
对于Mybatis和MySQL的批量插入批量更新,可以使用以下方法: 1. 批量插入 使用MyBatis的foreach标签,可以很方便地实现批量插入操作。示例如下: ```xml <insert id="batchInsert" parameterType="java.util.List"> insert into my_table (col1, col2, col3) values <foreach collection="list" item="item" separator=","> (#{item.col1}, #{item.col2}, #{item.col3}) </foreach> </insert> ``` 其,parameterType指定参数类型为List,collection属性指定要插入的List对象,item指定List的元素,separator指定分隔符。 在Java代码,调用该方法时,将List对象作为参数传入即可。 2. 批量更新 对于批量更新,可以使用MySQL的replace into语句来实现。示例如下: ```xml <update id="batchUpdate" parameterType="java.util.List"> <foreach collection="list" item="item" separator=";"> replace into my_table (id, col1, col2, col3) values (#{item.id}, #{item.col1}, #{item.col2}, #{item.col3}) </foreach> </update> ``` 其,replace into语句会先尝试插入记录,如果记录已存在,则更新记录。参数类型和调用方式与批量插入相同。 3. 存在即更新 如果只想更新已存在的记录,可以使用MySQL的on duplicate key update语句。示例如下: ```xml <update id="updateIfExists" parameterType="com.example.MyEntity"> insert into my_table (id, col1, col2, col3) values (#{id}, #{col1}, #{col2}, #{col3}) on duplicate key update col1 = values(col1), col2 = values(col2), col3 = values(col3) </update> ``` 其,id字段为主键。如果该记录已存在,则更新col1、col2、col3字段;否则插入新记录。参数类型为MyEntity,调用方式为传入一个MyEntity对象。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值