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

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

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

Jdbi介绍

[英]Main entry point; configurable wrapper around a JDBC DataSource. Use it to obtain Handle instances and provide configuration for all handles obtained from it.
[中]

代码示例

代码示例来源:origin: prestodb/presto

@Inject
public H2DaoProvider(DbResourceGroupConfig config)
{
  JdbcDataSource ds = new JdbcDataSource();
  ds.setURL(requireNonNull(config.getConfigDbUrl(), "resource-groups.config-db-url is null"));
  // TODO: this should use onDemand()
  this.dao = Jdbi.create(ds)
      .installPlugin(new SqlObjectPlugin())
      .open()
      .attach(H2ResourceGroupsDao.class);
}

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

@Override
  public void customizeJdbi(Jdbi jdbi) {
    jdbi.installPlugin(new JsonPlugin());
    jdbi.getConfig(JsonConfig.class).setJsonMapper(new JacksonJsonMapper());
  }
}

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

/**
 * Obtain a handle with just a JDBC URL
 *
 * @param url   JDBC Url
 * @param props JDBC properties
 *
 * @return newly opened Handle
 */
public static Handle open(final String url, final Properties props) {
  return create(url, props).open();
}

代码示例来源:origin: prestodb/presto

@Inject
public MysqlDaoProvider(DbResourceGroupConfig config)
{
  requireNonNull(config, "DbResourceGroupConfig is null");
  MysqlDataSource dataSource = new MysqlDataSource();
  dataSource.setURL(requireNonNull(config.getConfigDbUrl(), "resource-groups.config-db-url is null"));
  this.dao = Jdbi.create(dataSource)
      .installPlugin(new SqlObjectPlugin())
      .onDemand(ResourceGroupsDao.class);
}

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

@Override
  public void customizeJdbi(Jdbi db) {
    db.registerArgument(new DateTimeArgumentFactory());
    db.registerColumnMapper(new DateTimeMapper());
  }
}

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

@Before
public void setUp() {
  JdbcDataSource ds = new JdbcDataSource();
  // in MVCC mode h2 doesn't shut down immediately on all connections closed, so need random db name
  ds.setURL(String.format("jdbc:h2:mem:%s;MVCC=TRUE", UUID.randomUUID()));
  db = Jdbi.create(ds);
  db.installPlugin(new SqlObjectPlugin());
  db.registerRowMapper(new SomethingMapper());
  handle = db.open();
  handle.execute("create table something (id int primary key, name varchar(100))");
}

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

@Before
public void before() {
  final Jdbi db = dbRule.getJdbi();
  db.installPlugin(new SqlObjectPlugin());
  db.registerRowMapper(new SomethingMapper());
  handle = db.open();
  handle.execute("insert into something(id, name) values(1, '1')");
  handle.execute("insert into something(id, name) values(2, '2')");
  // "control group" element that should *not* be returned by the queries
  handle.execute("insert into something(id, name) values(3, '3')");
  expectedSomethings = Arrays.asList(new Something(1, "1"), new Something(2, "2"));
}

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

@Before
public void setUp() {
  db = Jdbi.create("jdbc:h2:mem:" + UUID.randomUUID());
  db.installPlugin(new SqlObjectPlugin());
  handle = db.open();
  handle.createUpdate(
      "create table foo (id int, bar varchar(100) default null);")
      .execute();
  dao = db.onDemand(MyDAO.class);
}

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

protected Handle openHandle() {
  tc = new TTC();
  dbRule.getJdbi().getConfig(SqlStatements.class).setTimingCollector(tc);
  return dbRule.getJdbi().open();
}

代码示例来源: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 bindMultipleQualifiers() {
  dbRule.getJdbi()
    // should use this one - register first so it's consulted last
    .registerArgument(new ReversedUpperCaseStringArgumentFactory())
    .registerArgument(new ReversedStringArgumentFactory())
    .registerArgument(new UpperCaseArgumentFactory())
    .useHandle(handle -> {
      handle.createUpdate("INSERT INTO something (id, name) VALUES (1, :name)")
        .bindByType("name", "abc", QualifiedType.of(String.class).with(Reversed.class, UpperCase.class))
        .execute();
      assertThat(handle.select("SELECT name FROM something")
        .mapTo(String.class)
        .findOnly())
        .isEqualTo("CBA");
    });
}

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

@Test
public void mapBeanQualifiedSetter() {
  dbRule.getJdbi()
    .registerColumnMapper(new ReversedStringMapper())
    .registerRowMapper(BeanMapper.factory(QualifiedSetterThing.class))
    .useHandle(handle -> {
      handle.execute("INSERT INTO something (id, name) VALUES (1, 'abc')");
      assertThat(handle.select("SELECT * FROM something")
        .mapTo(QualifiedSetterThing.class)
        .findOnly())
        .isEqualTo(new QualifiedSetterThing(1, "cba"));
    });
}

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

@Override
protected void before() {
  db = Jdbi.create(uri);
  if (installPlugins) {
    db.installPlugins();
  }
  plugins.forEach(db::installPlugin);
  sharedHandle = db.open();
  con = sharedHandle.getConnection();
}

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

@Benchmark
public UnqualifiedBean mapUnqualifiedBean() {
  return jdbi.withHandle(h -> h.createQuery("select 'a' as a, 'b' as b, 'c' as c").mapTo(UnqualifiedBean.class).findOnly());
}

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

@Test
public void testWithHandle() {
  Jdbi db = Jdbi.create(this.dbRule.getConnectionString());
  String value = db.withHandle(handle -> {
    handle.execute("insert into something (id, name) values (1, 'Brian')");
    return handle.createQuery("select name from something where id = 1").mapToBean(Something.class).findOnly().getName();
  });
  assertThat(value).isEqualTo("Brian");
}

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

@Override
  public void customizeJdbi(Jdbi jdbi) {
    jdbi.registerCollector(new VavrCollectorFactory());
    jdbi.registerRowMapper(new VavrTupleRowMapperFactory());
    jdbi.registerArgument(new VavrValueArgumentFactory());
    jdbi.registerColumnMapper(VavrOptionMapper.factory());
  }
}

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

@Before
public void before() {
  handle = h2.getJdbi()
    .registerRowMapper(new SomethingMapper())
    .open();
  handle.execute("insert into something(id, name) values(1, '1')");
  handle.execute("insert into something(id, name) values(2, '2')");
  // "control group" element that should *not* be returned by the queries
  handle.execute("insert into something(id, name) values(3, '3')");
  expectedSomethings = Arrays.asList(new Something(1, "1"), new Something(2, "2"));
}

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

@Test
public void testAPIWorks() {
  Spiffy s = db.onDemand(Spiffy.class);
  s.insert(7, "Bill");
  String bill = db.open().createQuery("select name from something where id = 7").mapTo(String.class).findOnly();
  assertThat(bill).isEqualTo("Bill");
}

相关文章