---
title: "Ibatis 批量添加修改删除-batch-insert-update-delete"
author: "石头的日记"
date: "2011-04-20T19:04:26Z"
tags: ["HTML", "PHP", "iBATIS"]
categories: ["企业架构"]
slug: "ibatis-batch-insert-update-delete"
draft: false
lastmod: "2011-04-20T19:04:26Z"
id: 1010282
url: "https://shitou521.iteye.com/blog/1010282"
---

转载出处:http://www.javadn.com/read.php?tid-790.html

 

**public** **void** batchUpdate( **final** String statementName, **final** List list) {

       **try** {

           **if** (list != **null** ) {

              **this** .getSqlMapClientTemplate().execute( **new** SqlMapClientCallback() {

                  **public** Object doInSqlMapClient(SqlMapExecutor executor) **throws** SQLException {

                     executor.startBatch();

                     **for** ( **int** i = 0, n = list.size(); i &lt; n; i++) {

                         executor.update(statementName, list.get(i));

                     }

                     executor.executeBatch();

                     **return** **null** ;

                  }

              });

           }

       } **catch** (Exception e) {

           **if** ( log .isDebugEnabled()) {

              e.printStackTrace();

              log .debug( "batchUpdate error: id \[" + statementName + "], parameterObject \[" + list + "].  Cause: " + e.getMessage());

           }

       }

 

    }

    **public** **void** batchInsert( **final** String statementName, **final** List list) {

       **try** {

           **if** (list != **null** ) {

              **this** .getSqlMapClientTemplate().execute( **new** SqlMapClientCallback() {

                  **public** Object doInSqlMapClient(SqlMapExecutor executor) **throws** SQLException {

                     executor.startBatch();

                     **for** ( **int** i = 0, n = list.size(); i &lt; n; i++) {

                         executor.insert(statementName, list.get(i));

                     }

                     executor.executeBatch();

                     **return** **null** ;

                  }

              });

           }

       } **catch** (Exception e) {

           **if** ( log .isDebugEnabled()) {

              e.printStackTrace();

              log .debug( "batchInsert error: id \[" + statementName + "], parameterObject \[" + list + "].  Cause: " + e.getMessage());

           }

       }

 

    }

    **public** **void** batchDelete( **final** String statementName, **final** List list) {

       **try** {

           **if** (list != **null** ) {

              **this** .getSqlMapClientTemplate().execute( **new** SqlMapClientCallback() {

                  **public** Object doInSqlMapClient(SqlMapExecutor executor) **throws** SQLException {

                     executor.startBatch();

                     **for** ( **int** i = 0, n = list.size(); i &lt; n; i++) {

                         executor.delete(statementName, list.get(i));

                     }

                     executor.executeBatch();

                     **return** **null** ;

                  }

              });

           }

       } **catch** (Exception e) {

           **if** ( log .isDebugEnabled()) {

              e.printStackTrace();

              log .debug( "batchDelete error: id \[" + statementName + "], parameterObject \[" + list + "].  Cause: " + e.getMessage());

           }

       }

 

    }