ibatis 批量 insert,update,delete

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 < 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 < 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 < 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());

           }

       }

    }

posted @ 2012-02-10 10:50  lovecd  阅读(1339)  评论(0编辑  收藏  举报