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

x33g5p2x  于2022-01-21 转载在 其他  
字(6.7k)|赞(0)|评价(0)|浏览(194)

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

Jdbi.useHandle介绍

[英]A convenience function which manages the lifecycle of a handle and yields it to a callback for use by clients.
[中]一种方便的函数,用于管理句柄的生命周期,并将其生成回调以供客户端使用。

代码示例

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

@Test
public void mapToMapFailsOnUnmappableClass() {
  jdbi.useHandle(h -> {
    Query query = h.createQuery(QUERY);
    assertThatThrownBy(() -> query.mapToMap(Alien.class))
      .hasMessage("no column mapper found for type " + Alien.class);
  });
}

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

@Test
public void mapToMapFailsOnUnmappableGenericType() {
  jdbi.useHandle(h -> {
    Query query = h.createQuery(QUERY);
    GenericType<Alien> type = new GenericType<Alien>() {};
    assertThatThrownBy(() -> query.mapToMap(type))
      .hasMessage("no column mapper found for type " + type);
  });
}

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

@Test
public void testObtainHandleInCallback() {
  Jdbi db = Jdbi.create("jdbc:h2:mem:" + UUID.randomUUID());
  db.useHandle(handle -> handle.execute("create table silly (id int)"));
}

代码示例来源: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 canFluentMapToMapWithClassForValue() {
  jdbi.useHandle(h -> {
    Map<String, BigDecimal> map = h.createQuery(QUERY)
      .mapToMap(BigDecimal.class)
      .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 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

@Before
public void createTable() {
  dbRule.getJdbi().useHandle(h -> {
    h.execute("CREATE TABLE test (id BIGINT PRIMARY KEY, value TEXT)");
  });
}

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

private void insert(String binding, Object bean) {
  dbRule.getJdbi().useHandle(h -> {
    String insert = String.format("INSERT INTO test VALUES(:id, :%s)", binding);
    h.createUpdate(insert).bindBean(bean).execute();
  });
}

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

@Before
public void setUp() {
  dbRule.getJdbi().useHandle(handle ->
    handle.execute("create table nvarchars (id int primary key, name nvarchar not null)"));
}

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

@After
public void tearDown() {
  dbRule.getJdbi().useHandle(handle ->
    handle.execute("drop table nvarchars"));
}

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

@Test
public void duplicateColumnsWithoutCaseChangeCauseException() {
  jdbi.useHandle(h -> {
    h.getConfig(MapMappers.class).setCaseChange(CaseStrategy.NOP);
    ResultIterable<Map<String, BigDecimal>> query = h.createQuery(QUERY.replace("two", "one")).mapToMap(BigDecimal.class);
    assertThatThrownBy(query::findOnly)
      .hasMessageContaining("map key \"one\" (from column \"one\") appears twice");
  });
}

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

@Test
public void duplicateKeysAfterCaseChangeCauseException() {
  jdbi.useHandle(h -> {
    h.getConfig(MapMappers.class).setCaseChange(CaseStrategy.LOWER);
    // one and ONE
    ResultIterable<Map<String, BigDecimal>> query = h.createQuery(QUERY.replace("two", "ONE")).mapToMap(BigDecimal.class);
    assertThatThrownBy(query::findOnly)
      .hasMessageContaining("map key \"one\" (from column \"ONE\") appears twice");
  });
}

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

@Before
public void setUp() {
  db = Jdbi.create("jdbc:hsqldb:mem:" + UUID.randomUUID(), "username", "password")
      .installPlugin(new SqlObjectPlugin());
  db.useHandle(handle -> handle.execute("create table something (id identity primary key, name varchar(32))"));
}

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

@Test
public void namesAreMappedCorrectly() {
  db.getJdbi().useHandle(h -> {
    Foobar name = h.createQuery("select :name")
      .bind("name", Foobar.FOO.name())
      .mapTo(Foobar.class)
      .findOnly();
    assertThat(name)
      .isEqualTo(Foobar.FOO);
  });
}

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

@Before
public void setUp() {
  db.useHandle(h -> h.execute("CREATE TABLE users (id SERIAL PRIMARY KEY, name VARCHAR)"));
  db.registerRowMapper(ConstructorMapper.factory(User.class));
}
// end::setup[]

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

@Test
public void byNameOverridesDefaultInBindingAndMapping() {
  db.getJdbi().useHandle(h -> {
    h.getConfig(Enums.class).setEnumStrategy(EnumStrategy.BY_ORDINAL);
    h.execute("create table enums(name varchar)");
    FooByNameDao dao = h.attach(FooByNameDao.class);
    dao.insert(Foo.BAR);
    assertThat(h.createQuery("select name from enums").mapTo(String.class).findOnly()).isEqualTo("BAR");
    Foo value = dao.select();
    assertThat(value).isEqualTo(Foo.BAR);
  });
}

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

@Test
public void useEnumStrategyOrdinalAnnotation() {
  db.getJdbi().useHandle(h -> {
    h.getConfig(Enums.class).setEnumStrategy(EnumStrategy.BY_NAME); // dao annotations will override
    h.execute("create table enums(ordinal int)");
    UseEnumStrategyOrdinalDao dao = h.attach(UseEnumStrategyOrdinalDao.class);
    dao.insert(Foo.BAR);
    assertThat(h.createQuery("select ordinal from enums").mapTo(Integer.class).findOnly()).isEqualTo(0);
    Foo value = dao.select();
    assertThat(value).isEqualTo(Foo.BAR);
  });
}

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

@Before
public void setUp() {
  db = dbRule.getJdbi();
  db.useHandle(h -> h.execute("CREATE ALIAS custom_insert FOR "
    + "\"org.jdbi.v3.sqlobject.TestTimingCollector.customInsert\";"));
  db.setTimingCollector(timingCollector);
  dao = db.onDemand(DAO.class);
}

相关文章