Skip to content
New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

是否支持批量新增、修改 #248

Open
senlu opened this issue May 18, 2022 · 1 comment
Open

是否支持批量新增、修改 #248

senlu opened this issue May 18, 2022 · 1 comment
Assignees

Comments

@senlu
Copy link

senlu commented May 18, 2022

我像使用mybatis的批量插入、更新方式时,发现不能成功(如下),因为batchSqlSession.getMapper(mapperClass);获取不到,有什么方式能获取到或者其他方式实现类似这种的批量操作吗(不使用sql语句拼接)?随便问下新版本有计划什么时候发布吗?
public <T, U, R> int batchUpdateOrInsert(List data, Class mapperClass, BiFunction<T, U, R> function) {
int count = 1;
SqlSession batchSqlSession = sqlSessionFactory.openSession(ExecutorType.BATCH);
try {
U mapper = batchSqlSession.getMapper(mapperClass);

        int size = data.size();
        for (T element : data) {
            function.apply(element, mapper);
            if ((count % BATCH_SIZE == 0) || count == size) {
                batchSqlSession.flushStatements();
            }
            count++;
        }
        // 非事务环境下强制commit,事务情况下该commit相当于无效
        batchSqlSession.commit(!TransactionSynchronizationManager.isSynchronizationActive());
    } catch (Exception e) {
        LogUtils.error(e, log, "MybatisBatchUtils处理异常");
        batchSqlSession.rollback();
        throw BusinessException.newInstance("批量插入错误");
    } finally {
        batchSqlSession.close();
    }
    return count - 1;
}
@easybest easybest self-assigned this May 18, 2022
@KangMz
Copy link

KangMz commented Oct 23, 2024

😥有找到什么解决方案嘛。 我也遇到了这个问题,xml中的namespace值是实体类, addMapper的时候会判断type是不是interface,否则就不add。结果就导致knownMappers是空的。getMapper时就拿不到mapper了

# for free to join this conversation on GitHub. Already have an account? # to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants