---
title: "ibatis 批量操作数据"
author: "alargon"
date: "2012-06-12T15:01:52Z"
tags: []
categories: ["数据库"]
slug: "ibatis"
draft: false
lastmod: "2012-06-06T11:50:19Z"
id: 1553071
url: "https://alargon.iteye.com/blog/1553071"
---

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 , parameterObject .  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 , parameterObject .  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 , parameterObject .  Cause: " + e.getMessage());
           }
       }
 

    }