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();
            }
        }
    }
}
相关推荐
睡不醒男孩0308232 小时前
第二篇:深入探索开源数据库高可用:构建基于CLup的PostgreSQL生产级高可用与读写分离架构
数据库·postgresql·开源·clup
Micro麦可乐4 小时前
Spring Boot 实战:从零设计一个短链系统(含完整代码与数据库设计)
数据库·spring boot·后端·哈希算法·雪花算法·短链系统
码农阿豪4 小时前
从零到一:Spring Boot快速接入金仓数据库实战
数据库·spring boot·后端
鼎讯信通4 小时前
风电光缆运维提质增效:G-4000A 光缆故障追踪仪破解风场巡检难题
运维·网络·数据库
三十..5 小时前
MySQL 从入门到高可用架构实战精要
运维·数据库·mysql
cfm_29145 小时前
Redis五大基本数据结构底层了解
数据结构·数据库·redis
真实的菜6 小时前
Redis 从入门到精通(十二):典型业务场景实战 —— 排行榜、限流器、秒杀系统、Session 共享
数据库·redis·python
你想考研啊6 小时前
mysql数据库导出导入
数据库·mysql·oracle
十年编程老舅7 小时前
Linux DRM:底层逻辑与实践架构
数据库·mysql
The Sheep 20237 小时前
Vue复习
linux·服务器·数据库