mybatis-plus批量处理遇到的坑

starlin 3,214 2021-04-21

最近项目中有用到mybatis-plus插件,遇到了一些坑,发现saveBatch中的批量方法效率太慢了,看了下源码

public boolean saveBatch(Collection<T> entityList, int batchSize) {
    String sqlStatement = sqlStatement(SqlMethod.INSERT_ONE);
    try (SqlSession batchSqlSession = sqlSessionBatch()) {
        int i = 0;
        for (T anEntityList : entityList) {
            batchSqlSession.insert(sqlStatement, anEntityList);
            if (i >= 1 && i % batchSize == 0) {
                batchSqlSession.flushStatements();
            }
            i++;
        }
        batchSqlSession.flushStatements();
    }
    return true;
}

看到这里,惊不惊喜,意不意外,这样不慢才怪

后面在看了mybatis-plus的官方文档中有提出一个选装插件InsertBatchSomeColumn,和拼接sql比效率还是慢,后面有测试对比,这里重点说下拼接sql的处理方法:


# Mybatis