Mybatis 之批量处理

该内容参考于 MybatisPlus 的批量操作方法

通过继承 MybatisPlus 的 ServiceImpl 来获取 SqlSession (实际上可以通过其他方式获取,因为使用了 MybatisPlus,所以就拿来直接用了),然后获取 Mapper 执行对应的方法,然后执行 flushStatements() 发送到数据库吗,代码如下

public void invalidProcessBatch(List<HcPassRecords> list) {
    Assert.notEmpty(list, "error: entityList must not be empty", new Object[0]);
    SqlSession batchSqlSession = this.sqlSessionBatch();
    Throwable throwable = null;

    try {
        int i = 0;

        for(Iterator<HcPassRecords> iteratored = list.iterator(); iteratored.hasNext(); ++i) {
            HcPassRecords hcPassRecords = iteratored.next();
            // 获取 Mapper
            HcPassRecordsProcessMapper sqlSessionMapper = batchSqlSession.getMapper(HcPassRecordsProcessMapper.class);
            // 执行方法
            sqlSessionMapper.invalidProcess(hcPassRecords.getPassRecordsId(), TrucksNodeType.PARK_OUT.getName(), PassRecordConstant.PROCESS_INVALID);
            if (i >= 1 && i % 1000 == 0) {
            	// 这里是没循环 1000 次,发送到数据库一次
                batchSqlSession.flushStatements();
            }
        }

        batchSqlSession.flushStatements();
    } catch (Throwable e) {
        throwable = e;
        throw e;
    } finally {
        if (batchSqlSession != null) {
            if (throwable != null) {
                try {
                    batchSqlSession.close();
                } catch (Throwable var16) {
                    throwable.addSuppressed(var16);
                }
            } else {
                batchSqlSession.close();
            }
        }

    }
}

相关推荐

  1. Mybatis 批量处理

    2024-07-17 00:36:02       21 阅读
  2. redis优化场景批量处理

    2024-07-17 00:36:02       19 阅读
  3. mybatis批量新增数据

    2024-07-17 00:36:02       35 阅读
  4. mybatis 批量添加数据

    2024-07-17 00:36:02       26 阅读
  5. 带你学习MybatisParameterHandler参数处理

    2024-07-17 00:36:02       30 阅读
  6. 带你学习MybatisResultSetHandler结果集处理

    2024-07-17 00:36:02       23 阅读

最近更新

  1. docker php8.1+nginx base 镜像 dockerfile 配置

    2024-07-17 00:36:02       66 阅读
  2. Could not load dynamic library ‘cudart64_100.dll‘

    2024-07-17 00:36:02       70 阅读
  3. 在Django里面运行非项目文件

    2024-07-17 00:36:02       57 阅读
  4. Python语言-面向对象

    2024-07-17 00:36:02       68 阅读

热门阅读

  1. Spring Boot 面试题及答案整理,最新面试题

    2024-07-17 00:36:02       21 阅读
  2. 【python基础】学习路线

    2024-07-17 00:36:02       20 阅读
  3. HTTP基本原理

    2024-07-17 00:36:02       23 阅读
  4. Git 的基本命令和使用方式

    2024-07-17 00:36:02       21 阅读
  5. 1.3Zygote

    2024-07-17 00:36:02       20 阅读
  6. 精准打击:Conda中conda remove命令的高效使用指南

    2024-07-17 00:36:02       22 阅读
  7. react项目使用EventBus实现登录拦截

    2024-07-17 00:36:02       20 阅读
  8. MySQL 关键字 IN 与 EXISTS 的使用与区别

    2024-07-17 00:36:02       22 阅读
  9. 关于ARP欺骗

    2024-07-17 00:36:02       20 阅读