天天看點

ibatis批量修改、批量增加、批量删除

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

 1、修改:傳入修改對象的List類型

<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-->
select * from dual
</insert>      

 2、插入:傳入對象的List類型

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

 3、删除:傳入ID的List類型

4、擷取清單跟删除一樣,周遊ID的list

Ibatis批量添加修改删除方法

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

           } 

       } 

 

    }
           

繼續閱讀