扫码关注官方订阅号
ringa_lee
这是基于ibatis batch insert的代码,从项目中抠出来的,希望能对你有所帮助
protected <E> boolean executeBatch(List<E> elist, String sqlId) throws SQLException { boolean isAutoCommit = true; SqlMapClient sqlMapClient = this.getSqlMapClient(); try { sqlMapClient.startTransaction(); sqlMapClient.startBatch(); isAutoCommit = sqlMapClient.getCurrentConnection().getAutoCommit(); sqlMapClient.getCurrentConnection().setAutoCommit(false); int flag = 0; for (int i = 0, size = elist.size(); i < size; ++i) { sqlMapClient.insert(sqlId, elist.get(i)); if (++flag == 200) { flag = 0; sqlMapClient.executeBatch(); } } sqlMapClient.executeBatch(); sqlMapClient.commitTransaction(); } catch (SQLException e) { sqlMapClient.getCurrentConnection().rollback();// 事务回滚 throw new SQLException(e.getCause()); } finally { sqlMapClient.getCurrentConnection().setAutoCommit(isAutoCommit);// 恢复原来的状态 sqlMapClient.endTransaction(); } return true; }
使用mybaits的foreach标签
mybaits
foreach
微信扫码关注PHP中文网服务号
QQ扫码加入技术交流群
Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号
PHP学习
技术支持
返回顶部
这是基于ibatis batch insert的代码,从项目中抠出来的,希望能对你有所帮助