IBatis批量处理那些事

IBatis批量处理之Sql样例

批量更新:

id="Update" resultMap="Select" parameterClass="list">
       begin
      "">
        update SYS_TABLE set
        Category=#[].Category#,
        Name =#[].Name#,
        Code =#[].Code#,               
        Status =#[].Status#
        where id = #[].Id#;     
      
      end;    

批量删除:

<delete id="Del" resultMap="Select" parameterClass="list">
      delete      
      from SYS_TABLE
      where id in    
      "," open="(" close=")">
        #[]#
      
delete>

批量插入:

<insert id="Add" resultMap="Select" parameterClass="list">
    insert all
    <iterate conjunction="">
        into SYS_TABLE
            (id,Category,Name,Code,Status)
        values(#[].Id#,#[].Category#,#[].Name#,
        #[].Code#,#[].Status#)
    iterate>
    
    select * from dual
insert>

注释一定要注意!select * from dual 这句必须加

IBatis批量处理之Java样例

总体思路都一样,一条的执行,批量提交

批量更新

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

批量插入

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

Merge into的用法 – 待完善

你可能感兴趣的:(IBatis学习笔记)