org.jdbi.v3.core.statement.Query类的使用及代码示例

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

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

Query介绍

[英]Statement providing convenience result handling for SQL queries.
[中]语句,为SQL查询提供方便的结果处理。

代码示例

代码示例来源: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

@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 testBehaviorOnBadBinding2() {
  assertThatThrownBy(() -> h.createQuery("select * from something where id = ?")
    .bind(1, 1)
    .bind(2, "Hi")
    .mapToBean(Something.class)
    .list()).isInstanceOf(UnableToCreateStatementException.class);
}

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

@Test
public void testUnusedBinding() {
  assertThatThrownBy(() -> h.createQuery("select * from something")
    .bind("id", 1)
    .collectRows(Collectors.counting())
  ).isInstanceOf(UnableToCreateStatementException.class);
}

代码示例来源: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

@Override
 public void evaluate() {
  assertThat(
      rule.getHandle()
        .select("select value from custom_migration_location")
        .mapTo(String.class)
        .findOnly())
    .isEqualTo("inserted in migration script in a custom location");
 }
};

代码示例来源: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

public Optional<User> findUserById(long id) {
  RowMapper<User> userMapper =
      (rs, ctx) -> new User(rs.getInt("id"), rs.getString("name"));
  return handle.createQuery("SELECT * FROM user WHERE id=:id")
    .bind("id", id)
    .map(userMapper)
    .findFirst();
}
// end::headlineExample[]

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

@Test
public void testDynamicBindOptionalEmpty() {
  List<Something> result = handle.createQuery(SELECT_BY_NAME)
      .bindByType("name", Optional.empty(), new GenericType<Optional<String>>() {})
      .mapToBean(Something.class)
      .list();
  assertThat(result).containsExactly(new Something(1, "eric"), new Something(2, "brian"));
}

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

default void check() throws Exception {
    Class<StatementContextExtensionMethodDao> extensionMethodDaoClass = StatementContextExtensionMethodDao.class;
    Method checkMethod = extensionMethodDaoClass.getMethod("check");
    ExtensionMethod extensionMethod = getHandle().getExtensionMethod();
    assertThat(extensionMethod.getType()).isEqualTo(extensionMethodDaoClass);
    assertThat(extensionMethod.getMethod()).isEqualTo(checkMethod);
    extensionMethod = getHandle().createQuery("select * from something").getContext().getExtensionMethod();
    assertThat(extensionMethod.getType()).isEqualTo(extensionMethodDaoClass);
    assertThat(extensionMethod.getMethod()).isEqualTo(checkMethod);
  }
}

代码示例来源: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 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 testDynamicBindOptionalOfUnregisteredCustomType() {
  exception.expect(UnsupportedOperationException.class);
  handle.createQuery(SELECT_BY_NAME)
      .bindByType("name", Optional.of(new Name("eric")), new GenericType<Optional<Name>>() {})
      .mapToBean(Something.class)
      .list();
}

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

@Test
public void testWritesViaFluentApi() {
  handle.execute("insert into intervals(id, foo) values(?, ?)", 6, testPeriod);
  final Period p = handle.createQuery("select foo from intervals where id=?")
      .bind(0, 6)
      .mapTo(Period.class)
      .findOnly();
  assertThat(p).isEqualTo(testPeriod);
}

代码示例来源: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 testBehaviorOnBadBinding1() {
  assertThatThrownBy(() -> h.createQuery("select * from something where id = ? and name = ?")
    .bind(0, 1)
    .mapToBean(Something.class)
    .list()).isInstanceOf(UnableToCreateStatementException.class);
}

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

@Test
public void testSimpleUpdate() {
  h.execute("insert into something (id, name) values (1, 'eric')");
  h.execute("update something set name = 'cire' where id = 1");
  Something eric = h.createQuery("select * from something where id = 1").mapToBean(Something.class).list().get(0);
  assertThat(eric.getName()).isEqualTo("cire");
}

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

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

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

@Test
public void testDynamicBindOptionalOfUnregisteredCustomType() {
  exception.expect(UnsupportedOperationException.class);
  handle.createQuery(SELECT_BY_NAME)
      .bindByType("name", Optional.of(new Name("eric")), new GenericType<Optional<Name>>() {})
      .mapToBean(Something.class)
      .list();
}

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

@Test
  public void testNegativePeriod() {
    handle.execute("insert into intervals(id, foo) values(?, interval '-3 years -1 month 2 days')", 7);
    final Period p = handle.createQuery("select foo from intervals where id=?")
      .bind(0, 7)
        .mapTo(Period.class)
        .findOnly();
    assertThat(p).isEqualTo(Period.of(-3, -1, 2));
  }
}

相关文章