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

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

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

Handle.createQuery介绍

[英]Return a Query instance that executes a statement with bound parameters and maps the result set into Java types.
[中]返回一个查询实例,该实例执行带有绑定参数的语句,并将结果集映射为Java类型。

代码示例

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

@Test
public void inlineRowMapper() {
  // tag::inlineRowMapper[]
  List<User> users = handle.createQuery("SELECT id, name FROM user ORDER BY id ASC")
      .map((rs, ctx) -> new User(rs.getInt("id"), rs.getString("name")))
      .list();
  // end::inlineRowMapper[]
  assertThat(users).hasSize(4);
  assertThat(users.get(3).name).isEqualTo("Data");
}

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

@Test
public void testExceptionAbortsTransaction() {
  assertThatThrownBy(() ->
      h.inTransaction(handle -> {
        handle.execute("insert into something (id, name) values (?, ?)", 0, "Keith");
        throw new IOException();
      }))
      .isInstanceOf(IOException.class);
  List<Something> r = h.createQuery("select * from something").mapToBean(Something.class).list();
  assertThat(r).isEmpty();
}

代码示例来源: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 zoneId() {
    final ZoneId zone = ZoneId.systemDefault();
    h.execute("insert into stuff(z) values (?)", zone);
    assertThat(h.createQuery("select z from stuff").mapTo(ZoneId.class).findOnly()).isEqualTo(zone);
  }
}

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

@Test
  public void testEnumCaseInsensitive() {
    assertThat(dbRule.getSharedHandle().createQuery("select 'BrIaN'").mapTo(SomethingElse.Name.class).findOnly())
      .isEqualTo(SomethingElse.Name.brian);
  }
}

代码示例来源: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 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 testZipping() {
  UsesBatching b = handle.attach(UsesBatching.class);
  List<Integer> ids = Arrays.asList(1, 2, 3, 4, 5);
  List<String> names = Arrays.asList("David", "Tim", "Mike");
  b.zipArgumentsTogether(ids, names);
  assertThat(b.size()).isEqualTo(3);
  List<String> insNames = handle.createQuery("select distinct name from something order by name")
                  .mapTo(String.class)
                  .list();
  assertThat(insNames).containsExactly("David", "Mike", "Tim");
}

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

@Test
public void testBindOptionalInt() {
  assertThat(handle.createQuery("SELECT :value")
      .bind("value", OptionalInt.empty())
      .collectInto(OptionalInt.class))
      .isEmpty();
  assertThat(handle.createQuery("SELECT :value")
      .bind("value", OptionalInt.of(123))
      .collectInto(OptionalInt.class))
      .hasValue(123);
}

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

@Test
public void immutableSet() {
  ImmutableSet<Integer> set = dbRule.getSharedHandle().createQuery("select intValue from something")
      .collectInto(new GenericType<ImmutableSet<Integer>>(){});
  assertThat(set).containsOnlyElementsOf(expected);
}

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

@Test
public void testBindOptionalPresent() {
  Something result = handle.createQuery(SELECT_BY_NAME)
      .bind("name", Optional.of("brian"))
      .mapToBean(Something.class)
      .findOnly();
  assertThat(result).isEqualTo(new Something(2, "brian"));
}

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

@Test
public void immutableList() {
  ImmutableList<Integer> list = dbRule.getSharedHandle().createQuery("select intValue from something")
      .collectInto(new GenericType<ImmutableList<Integer>>(){});
  assertThat(list).containsOnlyElementsOf(expected);
}

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

@Test
public void testBindOptionalPresent() {
  Something result = handle.createQuery(SELECT_BY_NAME)
      .bind("name", Optional.of("brian"))
      .mapToBean(Something.class)
      .findOnly();
  assertThat(result).isEqualTo(new Something(2, "brian"));
}

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

@Test
public void immutableSortedSet() {
  ImmutableSortedSet<Integer> set = dbRule.getSharedHandle().createQuery("select intValue from something")
      .collectInto(new GenericType<ImmutableSortedSet<Integer>>(){});
  assertThat(set).containsExactlyElementsOf(expected);
}

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

@Test
public void testCreateQueryObject() {
  h.createUpdate("insert into something (id, name) values (1, 'eric')").execute();
  h.createUpdate("insert into something (id, name) values (2, 'brian')").execute();
  List<Map<String, Object>> results = h.createQuery("select * from something order by id").mapToMap().list();
  assertThat(results).hasSize(2);
  assertThat(results.get(0).get("name")).isEqualTo("eric");
}

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

@Test
public void canFluentMapToMapWithGenericTypeForValue() {
  jdbi.useHandle(h -> {
    Map<String, BigDecimal> map = h.createQuery(QUERY)
      .mapToMap(new GenericType<BigDecimal>() {})
      .findOnly();
    assertThat(map)
      .containsOnlyKeys("one", "two", "three")
      .containsValues(new BigDecimal("1.0"), new BigDecimal("2.0"), new BigDecimal("3.0"));
  });
}

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

@Test
public void testColumnNameAnnotation() {
  Handle handle = dbRule.getSharedHandle();
  handle.execute("insert into something (id, name) values (1, 'foo')");
  ColumnNameThing thing = handle.createQuery("select * from something")
      .map(FieldMapper.of(ColumnNameThing.class))
      .findOnly();
  assertThat(thing.i).isEqualTo(1);
  assertThat(thing.s).isEqualTo("foo");
}

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

@Test
public void zonedDateTimeLosesZone() {
  ZonedDateTime dt = ZonedDateTime.now().withZoneSameInstant(ZoneId.of("America/Denver"));
  h.execute("insert into stuff(ts) values (?)", dt);
  assertThat(h.createQuery("select ts from stuff").mapTo(ZonedDateTime.class).findOnly().isEqual(dt)).isTrue();
}

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

@Test
public void canFluentMapToGenericTypeOfMap() {
  jdbi.useHandle(h -> {
    Map<String, BigDecimal> map = h.createQuery(QUERY)
      .mapTo(new GenericType<Map<String, BigDecimal>>() {})
      .findOnly();
    assertThat(map)
      .containsOnlyKeys("one", "two", "three")
      .containsValues(new BigDecimal("1.0"), new BigDecimal("2.0"), new BigDecimal("3.0"));
  });
}

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

@Test
public void localDateTime() {
  LocalDateTime d = LocalDateTime.now();
  h.execute("insert into stuff(ts) values (?)", d);
  assertThat(h.createQuery("select ts from stuff").mapTo(LocalDateTime.class).findOnly()).isEqualTo(d);
}

相关文章