【问题标题】:What is the use of this spring class BatchPreparedStatementSetter?这个spring类BatchPreparedStatementSetter有什么用?
【发布时间】:2011-05-18 07:58:26
【问题描述】:

谁能给我简要介绍一下他的春季班

org.springframework.jdbc.core.BatchPreparedStatementSetter

(JavaDoc API Link)

【问题讨论】:

    标签: java spring spring-jdbc


    【解决方案1】:

    它用于一次批量插入多行。

    This code 将说明它是如何使用的。

    好好看看importEmployees方法,一切都应该清楚了。

    【讨论】:

      【解决方案2】:

      batchUpdate 可以使用 JdbcTemplate batchUpdate 方法来完成,如下..

      public int[] batchUpdate(final List<Actor> actors) {
      int[] updateCounts = jdbcTemplate.batchUpdate("update t_actor set first_name = ?, " +
      "last_name = ? where id = ?",
      new BatchPreparedStatementSetter() {
      public void setValues(PreparedStatement ps, int i) throws SQLException {
      ps.setString(1, actors.get(i).getFirstName());
      ps.setString(2, actors.get(i).getLastName());
      ps.setLong(3, actors.get(i).getId().longValue());
      }
      public int getBatchSize() {
      return actors.size();
      }
      });
      return updateCounts;
      }
      

      【讨论】:

      • 嗨 Ashok,我很困惑,如果我必须为一个类实现 BatchPreparedStatementSetter 接口,那么我如何才能使用 ps 对象。我知道我们可以为该类创建一个参数化的构造器来传递对象,但是从哪里获取 ps 对象。尝试执行 ps.setString 时,可能是通过该类中的空指针异常。
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多