public void create(List<Reply> replyList) { try { // 開始批處理 sqlMapClient.startBatch(); for (Reply reply: replyList) { // 插入操作 sqlMapClient.insert("Reply.create", reply); } // 執(zhí)行批處理 sqlMapClient.executeBatch(); } catch (Exception e) { e.printStackTrace(); } }
public void create(List<Reply> replyList) { try { // 開始事務(wù) sqlMapClient.startTransaction(); // 開始批處理 sqlMapClient.startBatch(); for (Reply reply: replyList) { // 插入操作 sqlMapClient.insert("Reply.create", reply); } // 執(zhí)行批處理 sqlMapClient.executeBatch(); // 提交事務(wù) sqlMapClient.commitTransaction(); } catch (Exception e) { e.printStackTrace(); } finally { try { // 結(jié)束事務(wù) sqlMapClient.endTransaction(); } catch (SQLException e) { e.printStackTrace(); } } }
public void create(List<Reply> replyList) { if (!CollectionUtils.isEmpty(replyList)) { // 注意使用同一個(gè)SqlMapClient會(huì)話 SqlMapClient sqlMapClient = sqlMapClientTemplate.getSqlMapClient(); try { // 開始事務(wù) sqlMapClient.startTransaction(); // 開始批處理 sqlMapClient.startBatch(); for (Reply reply : replyList) { // 插入操作 sqlMapClient.insert("Reply.create", reply); } // 執(zhí)行批處理 sqlMapClient.executeBatch(); // 提交事務(wù) 交給Spring統(tǒng)一控制 // sqlMapClient.commitTransaction(); } catch (Exception e) { e.printStackTrace(); } finally { try { // 結(jié)束事務(wù) sqlMapClient.endTransaction(); } catch (SQLException e) { e.printStackTrace(); } } } }
@SuppressWarnings("unchecked") public void create(final List<Reply> replyList) { // 執(zhí)行回調(diào) sqlMapClientTemplate.execute(new SqlMapClientCallback() { // 實(shí)現(xiàn)回調(diào)接口 public Object doInSqlMapClient(SqlMapExecutor executor) throws SQLException { // 開始批處理 executor.startBatch(); for (Reply reply : replyList) { // 插入操作 executor.insert("Reply.create", reply); } // 執(zhí)行批處理 executor.executeBatch(); return null; } }); }
Copyright @ 飛飛 Powered by: .Text and ASP.NET Theme by: .NET Monster