org.jdbi.v3.core.statement.Query.mapTo()方法的使用及代码示例

x33g5p2x  于2022-01-28 转载在 其他  
字(7.0k)|赞(0)|评价(0)|浏览(217)

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

Query.mapTo介绍

暂无

代码示例

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

@Test
public void testReadsViaFluentAPI() {
  List<Map<String, String>> caps = handle.createQuery("select caps from campaigns order by id")
      .mapTo(STRING_MAP)
      .list();
  assertThat(caps).isEqualTo(ImmutableList.of(
      ImmutableMap.of("yearly", "10000", "monthly", "5000", "daily", "200"),
      ImmutableMap.of("yearly", "1000", "monthly", "200", "daily", "20")
  ));
}

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

@Test
public void immutableSortedSetWithComparator() {
  Comparator<Integer> comparator = Comparator.<Integer>naturalOrder().reversed();
  ImmutableSortedSet<Integer> set = dbRule.getSharedHandle().createQuery("select intValue from something")
      .mapTo(int.class)
      .collect(ImmutableSortedSet.toImmutableSortedSet(comparator));
  assertThat(set).containsExactlyElementsOf(expected.stream()
      .sorted(comparator)
      .collect(Collectors.toList()));
}

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

@Test
public void testInvalidDuration() {
  assertThatThrownBy(() -> handle.createQuery("select foo from intervals where id=?")
    .bind(0, 3) // The bad one.
    .mapTo(Duration.class)
    .findOnly()).isInstanceOf(IllegalArgumentException.class);
}

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

@Benchmark
public List<Tribble> mapByExactName() {
  return jdbi.withHandle(h ->
    h.select("select name from exact_name")
      .mapTo(QualifiedType.of(Tribble.class).with(EnumByName.class))
      .list());
}

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

@Benchmark
public List<Tribble> mapByRandomCaseName() {
  return jdbi.withHandle(h ->
    h.select("select name from random_case")
      .mapTo(QualifiedType.of(Tribble.class).with(EnumByName.class))
      .list());
}

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

@Test
public void testReduceBiFunction() {
  assertThat(
    dbRule.getSharedHandle().createQuery("SELECT * FROM reduce")
      .mapTo(Integer.class)
      .reduce(0, TestResultBearing::add))
    .isEqualTo(10);
}

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

@Test
public void badNameThrows() {
  db.getJdbi().useHandle(h -> {
    assertThatThrownBy(h.createQuery("select 'xxx'").mapTo(Foobar.class)::findOnly)
      .isInstanceOf(UnableToProduceResultException.class)
      .hasMessageContaining("no Foobar value could be matched to the name xxx");
  });
}

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

@Test
  public void testTimeout() {
    h.getConfig(SqlStatements.class).setQueryTimeout(2);

    assertThatCode(h.createQuery("select pg_sleep(1)").mapTo(String.class)::findOnly)
      .doesNotThrowAnyException();

    assertThatThrownBy(h.createQuery("select pg_sleep(3)").mapTo(String.class)::findOnly)
      .isInstanceOf(UnableToExecuteStatementException.class)
      .hasMessageContaining("canceling statement due to user request");
  }
}

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

@Test
public void methodCallCanBeAnnotatedAsByOrdinal() {
  Object byOrdinal = h.createQuery("select :ordinal")
    .bind("ordinal", Foobar.FOO.ordinal())
    .mapTo(QualifiedType.of(Foobar.class).with(EnumByOrdinal.class))
    .findOnly();
  assertThat(byOrdinal)
    .isEqualTo(Foobar.FOO);
}

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

@Test
public void testReadNegativeDuration() {
  handle.execute("insert into intervals(id, foo) values(?, interval '-2 days -3 hours')", 7);
  final Duration d = handle.createQuery("select foo from intervals where id=?")
      .bind(0, 7)
      .mapTo(Duration.class)
      .findOnly();
  assertThat(d).isEqualTo(Duration.ofDays(-2).plusHours(-3));
}

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

@Test
public void testHandlesNulls() {
  handle.execute("insert into campaigns(id, caps) values (?,?)", 4, null);
  Map<String, String> newCaps = handle.createQuery("select caps from campaigns where id=?")
      .bind(0, 4)
      .mapTo(STRING_MAP)
      .findOnly();
  assertThat(newCaps).isNull();
}

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

@Test
public void testTrivialDuration() {
  handle.execute("insert into intervals(id, foo) values(?, ?)", 4, Duration.ZERO);
  Duration d = handle.createQuery("select foo from intervals where id=?")
      .bind(0, 4)
      .mapTo(Duration.class)
      .findOnly();
  assertThat(d.isZero());
}

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

@Test
public void testDefineStrings() {
  assertThat(
    db.getSharedHandle().createQuery("select <a> from values(:a) union all select <b> from values(:b)")
      .defineNamedBindings()
      .bindBean(new DefinedBean())
      .mapTo(boolean.class)
      .list())
  .isEqualTo(Arrays.asList(true, false));
}

代码示例来源: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 testSillyNumberOfCallbacks() throws Exception {
  try (Handle h = dbRule.openHandle()) {
    h.execute("insert into something (id, name) values (1, 'Keith')");
  }
  // strangely enough, the compiler can't infer this and thinks the throws is redundant
  String value = dbRule.getJdbi().<String, Exception>withHandle(handle ->
      handle.inTransaction(handle1 ->
          handle1.createQuery("select name from something where id = 1").mapTo(String.class).findOnly()));
  assertThat(value).isEqualTo("Keith");
}

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

@Test
public void ordinalsAreMappedCorrectly() {
  db.getJdbi().useHandle(h -> {
    h.getConfig(Enums.class).setEnumStrategy(EnumStrategy.BY_ORDINAL);
    Foobar name = h.createQuery("select :ordinal")
      .bind("ordinal", Foobar.FOO.ordinal())
      .mapTo(Foobar.class)
      .findOnly();
    assertThat(name)
      .isEqualTo(Foobar.FOO);
  });
}

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

@Test
public void testWriteReadNegativeDuration() {
  handle.execute("insert into intervals(id, foo) values(?, ?)",
      8, Duration.ofDays(-3).plusMinutes(2));
  final Duration d = handle.createQuery("select foo from intervals where id=?")
      .bind(0, 8)
      .mapTo(Duration.class)
      .findOnly();
  assertThat(d).isEqualTo(Duration.ofDays(-3).plusMinutes(2));
}

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

@Test
public void testNestedHalfPresent() {
  Handle handle = dbRule.getSharedHandle();
  assertThat(handle
    .registerRowMapper(FieldMapper.factory(NullableNestedThing.class))
    .select("SELECT 42 as testValue, '3' as s")
    .mapTo(NullableNestedThing.class)
    .findOnly())
    .extracting("testValue", "nested.i", "nested.s")
    .containsExactly(42, null, "3");
}

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

@Test
public void nestedParameters() {
  assertThat(dbRule.getSharedHandle()
    .registerRowMapper(ConstructorMapper.factory(NestedBean.class))
    .select("select s, i from bean")
    .mapTo(NestedBean.class)
    .findOnly())
    .extracting("nested.s", "nested.i")
    .containsExactly("3", 2);
}

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

@Test
  public void testUri() {
    Handle h = dbRule.openHandle();
    h.createUpdate("insert into something (id, name) values (1, :uri)")
      .bind("uri", TEST_URI).execute();

    assertThat(h.createQuery("SELECT name FROM something")
      .mapTo(URI.class)
      .findOnly()).isEqualTo(TEST_URI);
  }
}

相关文章