org.jdbi.v3.core.Handle.prepareBatch()方法的使用及代码示例

x33g5p2x  于2022-01-20 转载在 其他  
字(9.4k)|赞(0)|评价(0)|浏览(223)

本文整理了Java中org.jdbi.v3.core.Handle.prepareBatch()方法的一些代码示例,展示了Handle.prepareBatch()的具体用法。这些代码示例主要来源于Github/Stackoverflow/Maven等平台,是从一些精选项目中提取出来的代码,具有较强的参考意义,能在一定程度帮忙到你。Handle.prepareBatch()方法的具体详情如下:
包路径:org.jdbi.v3.core.Handle
类名称:Handle
方法名:prepareBatch

Handle.prepareBatch介绍

[英]Prepare a batch to execute. This is for efficiently executing more than one of the same statements with different parameters bound.
[中]准备要执行的批处理。这是为了有效地执行具有不同参数绑定的多个相同语句。

代码示例

代码示例来源:origin: jdbi/jdbi

@Override
PreparedBatch createStatement(Handle handle, String locatedSql) {
  return handle.prepareBatch(locatedSql);
}

代码示例来源:origin: jdbi/jdbi

@Test
  public void testEmptyBatchThrows() {
    try (Handle h = dbRule.openHandle()) {
      final PreparedBatch b = h.prepareBatch("insert into something (id, name) values (?, ?)");
      assertThatThrownBy(b::add).isInstanceOf(IllegalStateException.class); // No parameters written yet
    }
  }
}

代码示例来源:origin: jdbi/jdbi

@Test
public void testPreparedBatchException() {
  h.execute(CREATE);
  Throwable e = catchThrowable(h.prepareBatch(INSERT_PREPARED).bindByType(0, null, Integer.class)::execute);
  assertThat(logger.getRawSql()).containsExactly(CREATE, CREATE, INSERT_PREPARED, INSERT_PREPARED);
  assertThat(logger.getTimings()).hasSize(2).allMatch(IS_POSITIVE);
  assertThat(logger.getExceptions()).containsExactly((SQLException) e.getCause());
}

代码示例来源:origin: jdbi/jdbi

@Test
public void testBindMaps() {
  PreparedBatch b = h.prepareBatch("insert into something (id, name) values (:id, :name)");
  b.add(ImmutableMap.of("id", 0, "name", "Keith"));
  b.add(ImmutableMap.of("id", 1, "name", "Eric"));
  b.add(ImmutableMap.of("id", 2, "name", "Brian"));
  b.execute();
  List<Something> r = h.createQuery("select * from something order by id").mapToBean(Something.class).list();
  assertThat(r).hasSize(3);
  assertThat(r.get(2).getName()).isEqualTo("Brian");
}

代码示例来源:origin: jdbi/jdbi

@Test
public void testPreparedBatch() {
  h.execute(CREATE);
  h.prepareBatch(INSERT_PREPARED).bind(0, 1).execute();
  assertThat(logger.getRawSql()).containsExactly(CREATE, CREATE, INSERT_PREPARED, INSERT_PREPARED);
  assertThat(logger.getTimings()).hasSize(2).allMatch(IS_POSITIVE);
  assertThat(logger.getExceptions()).isEmpty();
}

代码示例来源:origin: jdbi/jdbi

@Test
public void testBindProperties() {
  PreparedBatch b = h.prepareBatch("insert into something (id, name) values (?, ?)");
  b.add(0, "Keith");
  b.add(1, "Eric");
  b.add(2, "Brian");
  b.execute();
  List<Something> r = h.createQuery("select * from something order by id").mapToBean(Something.class).list();
  assertThat(r).hasSize(3);
  assertThat(r.get(2).getName()).isEqualTo("Brian");
}

代码示例来源:origin: jdbi/jdbi

@Test
public void testBatchInsertWithKeyGenerationAndExplicitSeveralColumnNames() {
  PreparedBatch batch = h.prepareBatch("insert into something (name) values (?) ");
  batch.add("Brian");
  batch.add("Thom");
  List<IdCreateTime> ids = batch.executeAndReturnGeneratedKeys("id", "create_time")
      .map((r, ctx) -> new IdCreateTime(r.getInt("id"), r.getDate("create_time")))
      .list();
  assertThat(ids).hasSize(2);
  assertThat(ids).extracting(ic -> ic.id).containsExactly(1, 2);
  assertThat(ids).extracting(ic -> ic.createTime).doesNotContainNull();
}

代码示例来源:origin: jdbi/jdbi

@Test
public void testCollectList() {
  h.prepareBatch("insert into something (id, name) values (?, ?)")
   .add(1, "Brian")
   .add(2, "Keith")
   .execute();
  List<String> rs = h.createQuery("select name from something order by id")
      .mapTo(String.class)
      .collect(toList());
  assertThat(rs).containsExactly("Brian", "Keith");
}

代码示例来源:origin: jdbi/jdbi

@Test
public void testFold() {
  h.prepareBatch("insert into something (id, name) values (?, ?)")
   .add(1, "Brian")
   .add(2, "Keith")
   .execute();
  Map<String, Integer> rs = h.createQuery("select id, name from something")
      .<Entry<String, Integer>>map((r, ctx) -> Maps.immutableEntry(r.getString("name"), r.getInt("id")))
      .collect(toMap(Entry::getKey, Entry::getValue));
  assertThat(rs).containsOnly(entry("Brian", 1), entry("Keith", 2));
}

代码示例来源:origin: jdbi/jdbi

@Test
public void testBatch() {
  // tag::batch[]
  PreparedBatch batch = handle.prepareBatch("INSERT INTO user(id, name) VALUES(:id, :name)");
  for (int i = 100; i < 5000; i++) {
    batch.bind("id", i).bind("name", "User:" + i).add();
  }
  int[] counts = batch.execute();
  // end::batch[]
  int[] expected = new int[4900];
  Arrays.fill(expected, 1);
  assertThat(counts).isEqualTo(expected);
}

代码示例来源:origin: jdbi/jdbi

@Test
public void testBatchInsertWithKeyGenerationAndExplicitColumnNames() {
  PreparedBatch batch = h.prepareBatch("insert into something (name) values (?) ");
  batch.add("Brian");
  batch.add("Thom");
  List<Integer> ids = batch.executeAndReturnGeneratedKeys("id").mapTo(Integer.class).list();
  assertThat(ids).containsExactly(1, 2);
  List<Something> somethings = h.createQuery("select id, name from something")
      .mapToBean(Something.class)
      .list();
  assertThat(somethings).containsExactly(new Something(1, "Brian"), new Something(2, "Thom"));
}

代码示例来源:origin: jdbi/jdbi

@Test
public void testBigishBatch() {
  PreparedBatch b = h.prepareBatch("insert into something (id, name) values (:id, :name)");
  int count = 100;
  for (int i = 0; i < count; ++i) {
    b.bind("id", i).bind("name", "A Name").add();
  }
  b.execute();
  int rowCount = h.createQuery("select count(id) from something").mapTo(int.class).findOnly();
  assertThat(rowCount).isEqualTo(count);
}

代码示例来源:origin: jdbi/jdbi

@Test
public void testMixedModeBatch() {
  PreparedBatch b = h.prepareBatch("insert into something (id, name) values (:id, :name)");
  Map<String, Object> one = ImmutableMap.of("id", 0);
  b.bind("name", "Keith").add(one);
  b.execute();
  List<Something> r = h.createQuery("select * from something order by id").mapToBean(Something.class).list();
  assertThat(r).extracting(Something::getName).containsExactly("Keith");
}

代码示例来源:origin: jdbi/jdbi

@Test
public void testBindBatch() {
  PreparedBatch b = h.prepareBatch("insert into something (id, name) values (:id, :name)");
  b.bind("id", 1).bind("name", "Eric").add();
  b.bind("id", 2).bind("name", "Brian").add();
  b.bind("id", 3).bind("name", "Keith").add();
  b.execute();
  List<Something> r = h.createQuery("select * from something order by id").mapToBean(Something.class).list();
  assertThat(r).hasSize(3);
  assertThat(r.get(2).getName()).isEqualTo("Keith");
}

代码示例来源:origin: jdbi/jdbi

@Test
public void testForgotFinalAdd() {
  PreparedBatch b = h.prepareBatch("insert into something (id, name) values (:id, :name)");
  b.bind("id", 1);
  b.bind("name", "Jeff");
  b.add();
  b.bind("id", 2);
  b.bind("name", "Tom");
  // forgot to add() here but we fix it up
  b.execute();
  assertThat(h.createQuery("select name from something order by id").mapTo(String.class).list())
      .containsExactly("Jeff", "Tom");
}

代码示例来源:origin: jdbi/jdbi

@Test
public void testPositionalBinding() {
  PreparedBatch b = h.prepareBatch("insert into something (id, name) values (?, ?)");
  b.bind(0, 0).bind(1, "Keith").add().execute();
  List<Something> r = h.createQuery("select * from something order by id").mapToBean(Something.class).list();
  assertThat(r).extracting(Something::getName).containsExactly("Keith");
}

代码示例来源:origin: jdbi/jdbi

@Test
public void testOnPreparedBatch() {
  Handle h = dbRule.getSharedHandle();
  PreparedBatch batch = h.prepareBatch("insert into something (id, name) values (:id, :name)");
  batch.registerArgument(new NameAF());
  batch.bind("id", 1).bind("name", new Name("Brian", "McCallister")).add();
  batch.bind("id", 2).bind("name", new Name("Henning", "S")).add();
  batch.execute();
  List<String> rs = h.createQuery("select name from something order by id")
            .mapTo(String.class)
            .list();
  assertThat(rs).containsExactly("Brian McCallister", "Henning S");
}

代码示例来源:origin: jdbi/jdbi

@Test
public void testPreparedBatch() {
  PreparedBatch b = dbRule.openHandle().prepareBatch("insert into something (id, name) values (?,?)");
  b.add(0, "a");
  b.add(0, "a");
  assertThatExceptionOfType(UnableToExecuteStatementException.class)
      .isThrownBy(b::execute)
      .satisfies(e -> assertSuppressions(e.getCause()));
}

代码示例来源:origin: jdbi/jdbi

@Before
public void before() {
  db.getSharedHandle().createUpdate("create table foo(bar varchar)").execute();
  db.getSharedHandle().prepareBatch("insert into foo(bar) values(:bar)")
    .bind("bar", "bam").add()
    .bind("bar", "gamma").add()
    .bind("bar", ":hello").add()
    .execute();
}

代码示例来源:origin: jdbi/jdbi

@Test
public void testMultipleExecuteBindBean() {
  final PreparedBatch b = h.prepareBatch("insert into something (id, name) values (:id, :name)");
  b.bindBean(new Something(1, "Eric")).add();
  b.bindBean(new Something(2, "Brian")).add();
  b.execute();
  // bindings should be cleared after execute()
  b.bindBean(new Something(3, "Keith")).add();
  b.execute();
  final List<Something> r = h.createQuery("select * from something order by id").mapToBean(Something.class).list();
  assertThat(r).extracting(Something::getId, Something::getName)
      .containsExactly(tuple(1, "Eric"), tuple(2, "Brian"), tuple(3, "Keith"));
}

相关文章