Oracle批处理操作方法

批量更新

java 复制代码
public static void batchUpdate(SqlSessionFactory sqlSessionFactory, String mapperStr, List<?> list) {
    if (list == null || list.isEmpty()) {
        return;
    }

    SqlSession batchSqlSession = sqlSessionFactory.openSession(ExecutorType.BATCH, false);
    int batchSize = 1000;

    try {
        int size = list.size();
        for (int i = 0; i < size; i++) {
            batchSqlSession.update(mapperStr, list.get(i));

            // 每 batchSize 条提交一次
            if ((i + 1) % batchSize == 0 || i == size - 1) {
                try {
                    batchSqlSession.commit();
                    batchSqlSession.clearCache();
                } catch (Exception e) {
                    log.error("批次提交失败,开始单条重试: {}", e.getMessage());
                    // 单条重试
                    int start = i - (i % batchSize);
                    int end = i;
                    for (int j = start; j <= end; j++) {
                        try {
                            batchSqlSession.update(mapperStr, list.get(j));
                            batchSqlSession.commit();
                            batchSqlSession.clearCache();
                        } catch (Exception ex) {
                            log.error("单条更新失败: {}", list.get(j), ex);
                        }
                    }
                }
            }
        }
    } catch (Exception e) {
        batchSqlSession.rollback();
        log.error("批量更新异常", e);
        throw e;
    } finally {
        batchSqlSession.close();
    }
}

批量插入

java 复制代码
import lombok.extern.slf4j.Slf4j;
import org.apache.ibatis.session.ExecutorType;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;

@Slf4j
public class BatchInsertUtil {

    /**
     * 通用批量插入(和你原batchUpdate逻辑完全一致)
     *
     * @param sqlSessionFactory sql会话工厂
     * @param mapperStr mapper全路径方法名,如:com.xxx.mapper.UserMapper.insert
     * @param list 要插入的数据集合
     */
    public static void batchInsert(SqlSessionFactory sqlSessionFactory, String mapperStr, List<?> list) {
        SqlSession batchSqlSession = null;
        try {
            // 空集合直接返回
            if (list == null || list.isEmpty()) {
                return;
            }

            // 开启批处理模式,手动提交
            batchSqlSession = sqlSessionFactory.openSession(ExecutorType.BATCH, false);
            int batchCount = 1000; // 每1000条提交一次
            int batchLastIndex = batchCount - 1;
            int size = list.size();
            int lastIndex = size - 1;

            for (int i = 0; i < size; i++) {
                // 执行插入
                batchSqlSession.insert(mapperStr, list.get(i));

                // 到达批次末尾 或 最后一条 → 提交
                if (i == lastIndex || i == batchLastIndex) {
                    try {
                        // 提交批次
                        batchSqlSession.commit();
                        // 清理缓存,防止OOM
                        batchSqlSession.clearCache();
                        // 下一批次
                        batchLastIndex += batchCount;
                    } catch (Exception e) {
                        log.error("插入批次异常,开始单条重试:{}", batchLastIndex, e);

                        // 出错 → 本批次降级单条插入
                        int index = batchLastIndex - batchCount + 1;
                        for (int j = index; j <= i; j++) {
                            try {
                                batchSqlSession.clearCache();
                                batchSqlSession.insert(mapperStr, list.get(j));
                                batchSqlSession.commit();
                                batchSqlSession.clearCache();
                            } catch (Exception es) {
                                log.error("单条插入失败 mapper:{},数据:{}", mapperStr, list.get(j), es);
                            }
                        }
                        batchLastIndex += batchCount;
                    }
                }
            }

        } catch (Exception exception) {
            log.error("batchInsert 全局异常:{}", exception.getMessage(), exception);
            if (batchSqlSession != null) {
                batchSqlSession.rollback();
            }
            throw exception;
        } finally {
            if (batchSqlSession != null) {
                batchSqlSession.close();
            }
        }
    }
}
相关推荐
笃行35044 分钟前
国产化落地避坑 · 干货向|Oracle 迁金仓 KES,我把外连接消除排在隐性陷阱第一位
数据库
老纪的技术唠嗑局1 小时前
花一年时间整理出的:AI 数据库混合搜索入门实践
数据库
川石课堂软件测试2 小时前
安全测试|常见SQL注入攻击方式、实例及预防
服务器·数据库·sql·功能测试·测试工具·安全·单元测试
笃行3502 小时前
国产化落地避坑 · 干货向|迁移后那条「测试能过、一上生产就查空」的 SQL,多半栽在 WHERE 函数顺序上
数据库
蓝田~3 小时前
MySQL慢查询怎么优化?B+Tree索引原理+MVCC读不阻塞写+EXPLAIN执行计划,从5秒到0.05秒
数据库·mysql
sunxunyong3 小时前
doris用户资源组配置
数据库
Cloud云卷云舒3 小时前
云卷云舒:HaishanDB的AI原生能力主要应用场景
数据库·人工智能·ai-native·haishandb·移动云长期记忆
时间的拾荒人3 小时前
MySQL 视图详解
android·数据库·mysql
qq_366086224 小时前
sql 查询中关于 null 值判断的问题
数据库·sql
测试修炼手册4 小时前
[测试技术] JUnit 6 入门与实战:断言、参数化与 Mockito
数据库·junit·sqlserver