我是新手,想学学batchUpdate()的用法,可是在spring的API中找不到这个用法,只能看到了org.springframework.jdbc.object.BatchSqlUpdate 这个类。请问batchUpdate()这个方法在哪看?怎么用这个方法

解决方案 »

  1.   

    我只用过JDBC中Statement的addBatch和executeBatch()方法。SPRING中有
    jdbcTemplate.batchUpdate(String sql, final BatchPreparedStatementSetter pss),类似于JDBC的PreparedStatement,性能较上着有所提高。
    示例如下:final int count = 2000;    
        final List<String> firstNames = new ArrayList<String>(count);    
        final List<String> lastNames = new ArrayList<String>(count);    
        for (int i = 0; i < count; i++) {    
          firstNames.add("First Name " + i);    
          lastNames.add("Last Name " + i);    
        }    
        jdbcTemplate.batchUpdate(    
                "insert into customer (id, first_name, last_name, last_login, comments) values (?, ?, ?, ?, ?)",    
                new BatchPreparedStatementSetter() {    
               //为prepared statement设置参数。这个方法将在整个过程中被调用的次数    
            public void setValues(PreparedStatement ps, int i) throws SQLException {    
                    ps.setLong(1, i + 10);    
                    ps.setString(2, firstNames.get(i));    
                    ps.setString(3, lastNames.get(i));    
                    ps.setNull(4, Types.TIMESTAMP);    
                    ps.setNull(5, Types.CLOB);    
                  }    
                  //返回更新的结果集条数    
              public int getBatchSize() {    
                       return count;    
                  }    
                });    
      }  
      

  2.   

    还有jdbcTemplate.batchUpdate(final String[] sql),参数是直接传入一组SQL查询语句。其实使用JDBC自己写一类似的批量操作类也很简单。