Java多线程 结合 Mybatis 批量添加、修改实现批量数据优化

博客介绍了使用MyBatis的foreach进行批量添加和批量修改操作。在批量修改时需注意判断时加item以及逗号问题,涉及SQL和MySQL相关知识。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

注:本文章项目背景为使用excel文档批量导入Java数据。
原404条数据导入时间为30秒+,加入多线程后为12秒+,加入Mybatis 批量处理后优化至1.8秒。

Java多线程配置

                /**
                 * 创建定长线程池
                 */
                ExecutorService fixedThreadPool = Executors.newFixedThreadPool(5);
                /**
                 * 存放多线程submit数据
                 */
                List<Future<Void>> futures = new ArrayList<>();
                List<CsWordGroup> csWordGroups = new ArrayList<>();
                /**
                 * 取Excel表传入信息循环
                 */
                for (int index = 0; index < csWordBook.getFeedbackData().size(); index++) {
                    /**
                     * 使用利用匿名内部类实现多线程 用submit执行任务 用Callable返回值 Callable返回值固定为Future类型
                     */
                    int finalIndex = index;
                    futures.add(fixedThreadPool.submit(new Callable<Void>() {
                        @Override
                        public Void call() throws Exception {
                            try {
                                Map<String, Object> item = csWordBook.getFeedbackData().get(finalIndex);
                                /**
                                 * 赋值单词/词组信息表并循环添加
                                 */
                                CsWordGroup csWordGroup = new CsWordGroup();
                                csWordGroup.setWgWbId(csWordBook.getWbId());
                                csWordGroup.setWgWord(item.get("单词/词组").toString());
                                csWordGroup.setWgAbbr(item.get("单词缩写").toString());
                                csWordGroup.setWgInterpretation(item.get("单词释义(多个意思以英文,隔开)").toString());
                                csWordGroup.setWgCreateBy(csWordBook.getWbCreateBy());
                                csWordGroups.add(csWordGroup);
                            } catch (Exception e) {
                                // 处理异常
                                e.printStackTrace();
                            }
                            return null;
                        }
                    }));
                }
                /**
                 * 批量执行添加 缩减添加时间
                 */
                rows += csWordGroupMapper.insertCsWordGroupForeach(csWordGroups);
                /**
                 * 关闭线程池执行,子线程不结束线程池不会关闭
                 * 执行后退出方法执行回收掉线程
                 */
                fixedThreadPool.shutdown();

使用foreach 批量添加:

mapper.java

public int insertCsWordGroupForeach(List<CsWordGroup> csWordGroups);

由于mapper.java中未指定参数,所以默认xml中的collectionlist

    <insert id="insertCsWordGroupForeach">
        insert into cs_word_group
        (
            wg_wb_id,
            wg_word,
            wg_abbr,
            wg_interpretation,
            wg_create_by,
            wg_create_time,
            wg_is_del
        )
        <foreach collection="list" item="item" open="values" separator=",">
            (
                #{item.wgWbId},
                #{item.wgWord},
                #{item.wgAbbr},
                #{item.wgInterpretation},
                #{item.wgCreateBy},
                now(),
                0
            )
        </foreach>
    </insert>

批量修改:(此处注意判断时加item,以及注意逗号问题)

<update id="batchUpdate">
    <foreach collection="list" item="item" separator=";">
        update table
        set
        pdjlid = #{item.pdjlid}
        <if test="item.pmggnm != null">,pmggnm = #{item.pmggnm}</if>
        <if test="item.pdsl != null">,pdsl = #{item.pdsl}</if>
        <if test="item.sysl != null">,sysl = #{item.sysl}</if>
        <if test="item.zwsl != null">,zwsl = #{item.zwsl}</if>
        <if test="item.zzsl != null">,zzsl = #{item.zzsl}</if>
        <if test="item.pysl != null">,pysl = #{item.pysl}</if>
        <if test="item.pksl != null">,pksl = #{item.pksl}</if>
        <if test="item.qmkc != null">,qmkc = #{item.qmkc}</if>
        <if test="item.qcsl != null">,qcsl = #{item.qcsl}</if>
        <if test="item.idx != null">,idx = #{item.idx}</if>
        <if test="item.updateBy != null">,update_by = #{item.updateBy}</if>
        <if test="item.updateTime != null">,update_time = #{item.updateTime}</if>
        <if test="item.updateIp != null">,update_ip = #{item.updateIp}</if>
        <if test="item.remark != null and item.remark != ''">,remark = #{item.remark}</if>
        <if test="item.version != null">,version = #{item.version}</if>
        <if test="item.delFlag != null and item.delFlag != ''">,del_flag = #{item.delFlag}</if>
        <if test="item.ljPksl != null">,lj_pksl = #{item.ljPksl}</if>
        where
        pdjlmxid = #{item.pdjlmxid}
    </foreach>
</update>
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

程似锦吖

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值