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

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

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

Jdbi.withExtension介绍

[英]A convenience method which opens an extension of the given type, yields it to a callback, and returns the result of the callback. A handle is opened if needed by the extension, and closed before returning to the caller.
[中]

代码示例

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

/**
 * A convenience method which opens an extension of the given type, and yields it to a callback. A handle is opened
 * if needed by the extention, and closed before returning to the caller.
 *
 * @param extensionType the type of extension
 * @param callback      a callback which will receive the extension
 * @param <E>           the extension type
 * @param <X>           the exception type optionally thrown by the callback
 * @throws NoSuchExtensionException if no {@link ExtensionFactory} is registered which supports the given extension type.
 * @throws X                        if thrown by the callback.
 */
public <E, X extends Exception> void useExtension(Class<E> extensionType, ExtensionConsumer<E, X> callback)
    throws NoSuchExtensionException, X {
  withExtension(extensionType, extension -> {
    callback.useExtension(extension);
    return null;
  });
}

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

static <E> E create(Jdbi db, Class<E> extensionType) {
  ThreadLocal<E> threadExtension = new ThreadLocal<>();
  InvocationHandler handler = (proxy, method, args) -> {
    if (EQUALS_METHOD.equals(method)) {
      return proxy == args[0];
    }
    if (HASHCODE_METHOD.equals(method)) {
      return System.identityHashCode(proxy);
    }
    if (TOSTRING_METHOD.equals(method)) {
      return extensionType + "@" + Integer.toHexString(System.identityHashCode(proxy));
    }
    if (threadExtension.get() != null) {
      return invoke(threadExtension.get(), method, args);
    }
    return db.withExtension(extensionType, extension ->
        JdbiThreadLocals.invokeInContext(threadExtension, extension,
            () -> invoke(extension, method, args)));
  };
  return extensionType.cast(
      Proxy.newProxyInstance(
          extensionType.getClassLoader(),
          new Class[]{extensionType}, handler));
}

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

@Test
public void testUseTemplateEngine() {
  String selected = jdbi.withExtension(Queries1.class, q -> q.select("foo"));
  assertThat(selected).isEqualTo("foo");
}

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

@Test
public void testUseMessageFormat() {
  String selected = jdbi.withExtension(QueriesForMessageFormatTE.class, q -> q.select("foo"));
  assertThat(selected).isEqualTo("foo");
}

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

@Test
public void testUseDefinedAttributes() {
  String selected = jdbi.withExtension(QueriesForDefinedAttributeTE.class, q -> q.select("foo"));
  assertThat(selected).isEqualTo("foo");
}

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

@Test
public void testCustomAnnotation() {
  String selected = jdbi.withExtension(Queries2.class, q -> q.select("foo"));
  assertThat(selected).isEqualTo("foo");
}

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

@Test
public void testLocateConfigDriven() throws Exception {
  Jdbi jdbi = dbRule.getJdbi();
  jdbi.useHandle(h -> {
    h.execute("create table something (id int, name text)");
    h.execute("insert into something (id, name) values (?, ?)", 2, "Alice");
    h.execute("insert into something (id, name) values (?, ?)", 1, "Bob");
  });
  jdbi.getConfig(SqlObjects.class).setSqlLocator(
    (type, method, config) -> config.get(TestConfig.class).sql);
  jdbi.getConfig(TestConfig.class).sql = "select * from something order by id";
  assertThat(jdbi.withExtension(TestDao.class, TestDao::list))
    .containsExactly(new Something(1, "Bob"), new Something(2, "Alice"));
  jdbi.getConfig(TestConfig.class).sql = "select * from something order by name";
  assertThat(jdbi.withExtension(TestDao.class, TestDao::list))
    .containsExactly(new Something(2, "Alice"), new Something(1, "Bob"));
}

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

@Test
public void sqlObject() {
  // tag::sqlobject-usage[]
  Jdbi jdbi = Jdbi.create("jdbc:h2:mem:test");
  jdbi.installPlugin(new SqlObjectPlugin());
  // Jdbi implements your interface based on annotations
  List<User> userNames = jdbi.withExtension(UserDao.class, dao -> {
    dao.createTable();
    dao.insertPositional(0, "Alice");
    dao.insertPositional(1, "Bob");
    dao.insertNamed(2, "Clarice");
    dao.insertBean(new User(3, "David"));
    return dao.listUsers();
  });
  assertThat(userNames).containsExactly(
      new User(0, "Alice"),
      new User(1, "Bob"),
      new User(2, "Clarice"),
      new User(3, "David"));
  // end::sqlobject-usage[]
}

代码示例来源:origin: org.jdbi/jdbi3

/**
 * A convenience method which opens an extension of the given type, and yields it to a callback. A handle is opened
 * if needed by the extention, and closed before returning to the caller.
 *
 * @param extensionType the type of extension
 * @param callback      a callback which will receive the extension
 * @param <E>           the extension type
 * @param <X>           the exception type optionally thrown by the callback
 * @throws NoSuchExtensionException if no {@link ExtensionFactory} is registered which supports the given extension type.
 * @throws X                        if thrown by the callback.
 */
public <E, X extends Exception> void useExtension(Class<E> extensionType, ExtensionConsumer<E, X> callback)
    throws NoSuchExtensionException, X {
  withExtension(extensionType, extension -> {
    callback.useExtension(extension);
    return null;
  });
}

代码示例来源:origin: org.jdbi/jdbi3

return method.invoke(threadExtension.get(), args);
return db.withExtension(extensionType, extension -> {
  threadExtension.set(extension);
  try {

代码示例来源:origin: org.jdbi/jdbi3-sqlobject

@Test
public void testUseDefinedAttributes() {
  String selected = jdbi.withExtension(QueriesForDefinedAttributeTE.class, q -> q.select("foo"));
  assertThat(selected).isEqualTo("foo");
}

代码示例来源:origin: org.jdbi/jdbi3-sqlobject

@Test
public void testUseMessageFormat() {
  String selected = jdbi.withExtension(QueriesForMessageFormatTE.class, q -> q.select("foo"));
  assertThat(selected).isEqualTo("foo");
}

代码示例来源:origin: org.jdbi/jdbi3-sqlobject

@Test
public void testLocateConfigDriven() throws Exception {
  Jdbi jdbi = dbRule.getJdbi();
  jdbi.useHandle(h -> {
    h.execute("create table something (id int, name text)");
    h.execute("insert into something (id, name) values (?, ?)", 2, "Alice");
    h.execute("insert into something (id, name) values (?, ?)", 1, "Bob");
  });
  jdbi.getConfig(SqlObjects.class).setSqlLocator(
    (type, method, config) -> config.get(TestConfig.class).sql);
  jdbi.getConfig(TestConfig.class).sql = "select * from something order by id";
  assertThat(jdbi.withExtension(TestDao.class, TestDao::list))
    .containsExactly(new Something(1, "Bob"), new Something(2, "Alice"));
  jdbi.getConfig(TestConfig.class).sql = "select * from something order by name";
  assertThat(jdbi.withExtension(TestDao.class, TestDao::list))
    .containsExactly(new Something(2, "Alice"), new Something(1, "Bob"));
}

相关文章