本文整理汇总了Java中org.jdbi.v3.core.Jdbi类的典型用法代码示例。如果您正苦于以下问题:Java Jdbi类的具体用法?Java Jdbi怎么用?Java Jdbi使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
Jdbi类属于org.jdbi.v3.core包,在下文中一共展示了Jdbi类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: build
import org.jdbi.v3.core.Jdbi; //导入依赖的package包/类
public Jdbi build(Environment environment,
PooledDataSourceFactory configuration,
String name) {
ManagedDataSource dataSource = configuration.build(environment.metrics(), name);
String validationQuery = configuration.getValidationQuery();
Jdbi jdbi = Jdbi.create(dataSource);
jdbi.setTimingCollector(new InstrumentedTimingCollector(environment.metrics(), nameStrategy));
jdbi.installPlugins();
environment.lifecycle().manage(dataSource);
environment.healthChecks().register(name, new JdbiHealthCheck(
environment.getHealthCheckExecutorService(),
configuration.getValidationQueryTimeout().orElseGet(() -> Duration.seconds(5)),
jdbi, validationQuery));
return jdbi;
}
示例2: test
import org.jdbi.v3.core.Jdbi; //导入依赖的package包/类
@Test
public void test() throws Exception {
Jdbi jdbi = Jdbi.create(ds.getDataSource());
try (Handle h = jdbi.open()) {
h.execute("create table contacts (id int primary key, name varchar(100))");
h.execute("insert into contacts (id, name) values (?, ?)", 1, "Alice");
h.execute("insert into contacts (id, name) values (?, ?)", 2, "Bob");
List<String> names = h.createQuery("select name from contacts order by id")
.mapTo(String.class)
.list();
assertThat(names)
.containsExactly("Alice", "Bob");
String name = h.createQuery("select name from contacts where id = :id")
.bind("id", 1)
.mapTo(String.class)
.findOnly();
assertThat(name)
.isEqualTo("Alice");
}
}
示例3: classBlock
import org.jdbi.v3.core.Jdbi; //导入依赖的package包/类
@Override
protected Statement classBlock(RunNotifier notifier) {
final Statement statement = super.classBlock(notifier);
return new Statement() {
@Override
public void evaluate() throws Throwable {
// Open a new handle for every test
// It affords to avoid creating a static state which makes tests more independent
JditProperties jditProperties = klass.getAnnotation(JditProperties.class);
Jdbi dbi = jditProperties != null ? DBIContextFactory.getDBI(jditProperties.value()) : DBIContextFactory.getDBI();
try (Handle handle = dbi.open()) {
injector = new TestObjectsInjector(dbi, handle);
databaseMaintenance = DatabaseMaintenanceFactory.create(handle);
dataSetInjector = new DataSetInjector(new DataMigration(handle));
statement.evaluate();
}
}
};
}
示例4: configureWithCallback
import org.jdbi.v3.core.Jdbi; //导入依赖的package包/类
@Test
public void configureWithCallback() throws Exception {
new MockUnit(Env.class, Config.class, Binder.class, JdbiPlugin.class)
.expect(configure)
.expect(unit -> {
Jdbi jdbi = unit.get(Jdbi.class);
expect(jdbi.installPlugin(unit.get(JdbiPlugin.class))).andReturn(jdbi);
})
.run(unit -> {
new Jdbi3()
.doWith(jdbi -> {
jdbi.installPlugin(unit.get(JdbiPlugin.class));
})
.configure(unit.get(Env.class), unit.get(Config.class), unit.get(Binder.class));
});
}
示例5: inClause
import org.jdbi.v3.core.Jdbi; //导入依赖的package包/类
@Test
public void inClause() {
Jdbi jdbi = Jdbi.create("jdbc:h2:mem:test");
List<String> names = jdbi.withHandle(h -> {
h.createUpdate("CREATE TABLE USER (id INTEGER PRIMARY KEY, name VARCHAR)")
.execute();
h.createUpdate("INSERT INTO USER(id, name) VALUES (:id, :name)")
.bind("id", 1)
.bind("name", "Pedro Picapiedra")
.execute();
h.createUpdate("INSERT INTO USER(id, name) VALUES (:id, :name)")
.bind("id", 2)
.bind("name", "Pablo Marmol")
.execute();
return h.createQuery("select name from USER where id in (<id>)")
.bindList("id", 1, 2)
.mapTo(String.class)
.list();
});
assertEquals(2, names.size());
}
示例6: load
import org.jdbi.v3.core.Jdbi; //导入依赖的package包/类
@Override
public void load(Undertow.Builder builder, DeploymentContext deploymentContext) throws IOException {
log.info( "Creating users and roles..." );
val script = SystemResource.readFileAsString( "tables.sql", "UTF-8" );
val jdbi = Jdbi.create( dataSource );
try ( val handle = jdbi.open() ) {
handle.useTransaction( h -> h.createScript( script ).execute() );
}
}
示例7: main
import org.jdbi.v3.core.Jdbi; //导入依赖的package包/类
public static void main(String[] args) {
Jdbi dbi = Jdbi.create("jdbc:sqlite:behemoth.db");
String installScript = ClasspathSqlLocator.findSqlOnClasspath(InitDatabase.class.getName() + "_install");
dbi.useHandle(hnd -> {
int totalExecuted = 0;
int[] rowsExecuted = hnd.createScript(installScript).execute();
for (int rows : rowsExecuted) {
totalExecuted += rows;
}
System.out.println("Executed: " + totalExecuted);
});
}
示例8: JdbiDynamicEnumCache
import org.jdbi.v3.core.Jdbi; //导入依赖的package包/类
public JdbiDynamicEnumCache(Jdbi conn, Class<T> clazz, String tableName, BiFunction<Long, String, T> typeCodeFactory) {
super(clazz);
conn.useHandle(handle -> {
Query q = handle.createQuery("SELECT id, name FROM " + tableName);
ResultIterable<T> tcIter = q.map((rs, ctx) -> typeCodeFactory.apply(rs.getLong("id"),
rs.getString("name")));
tcIter.forEach(this::add);
});
}
示例9: test
import org.jdbi.v3.core.Jdbi; //导入依赖的package包/类
@Test
public void test() throws Exception {
Jdbi jdbi = Jdbi.create(ds.getDataSource());
jdbi.registerRowMapper(BeanMapper.factory(Account.class));
jdbi.registerColumnMapper(new MoneyMapper());
jdbi.registerArgument(new MoneyArgumentFactory());
try (Handle h = jdbi.open()) {
Money tenDollars = Money.of(USD, 10);
Money fiveDollars = Money.of(USD, 5);
h.execute("create table accounts (id int primary key, name varchar(100), balance decimal)");
h.execute("insert into accounts (id, name, balance) values (?, ?, ?)", 1, "Alice", tenDollars);
h.execute("insert into accounts (id, name, balance) values (?, ?, ?)", 2, "Bob", fiveDollars);
List<Account> list = h.createQuery("select * from accounts order by id")
.mapTo(Account.class)
.list();
assertThat(list)
.extracting(Account::getId, Account::getName, Account::getBalance)
.containsExactly(tuple(1, "Alice", tenDollars),
tuple(2, "Bob", fiveDollars));
Account bob = h.createQuery("select * from accounts where id = :id")
.bind("id", 2)
.mapTo(Account.class)
.findOnly();
assertThat(bob)
.extracting(Account::getId, Account::getName, Account::getBalance)
.containsExactly(2, "Bob", fiveDollars);
}
}
示例10: test
import org.jdbi.v3.core.Jdbi; //导入依赖的package包/类
@Test
public void test() throws Exception {
Jdbi jdbi = Jdbi.create(ds.getDataSource());
jdbi.installPlugin(new SqlObjectPlugin());
jdbi.useExtension(AccountDao.class, dao -> {
Money tenDollars = Money.of(USD, 10);
Money fiveDollars = Money.of(USD, 5);
dao.createTable();
dao.insert(new Account(1, "Alice", tenDollars));
dao.insert(new Account(2, "Bob", fiveDollars));
assertThat(dao.list())
.extracting(Account::getId, Account::getName, Account::getBalance)
.containsExactly(tuple(1, "Alice", tenDollars),
tuple(2, "Bob", fiveDollars));
assertThat(dao.getById(2))
.extracting(Account::getId, Account::getName, Account::getBalance)
.containsExactly(2, "Bob", fiveDollars);
dao.update(new Account(2, "Robert", tenDollars));
assertThat(dao.getById(2))
.extracting(Account::getId, Account::getName, Account::getBalance)
.containsExactly(2, "Robert", tenDollars);
});
}
示例11: test
import org.jdbi.v3.core.Jdbi; //导入依赖的package包/类
@Test
public void test() throws Exception {
Jdbi jdbi = Jdbi.create(ds.getDataSource());
try (Handle h = jdbi.open()) {
h.execute("create table contacts (id int primary key, name varchar(100))");
h.execute("insert into contacts (id, name) values (?, ?)", 1, "Alice");
h.execute("insert into contacts (id, name) values (?, ?)", 2, "Bob");
List<Contact> list = h.createQuery("select * from contacts order by id")
.map(new ContactMapper())
.list();
assertThat(list)
.extracting(Contact::getId, Contact::getName)
.containsExactly(tuple(1, "Alice"),
tuple(2, "Bob"));
Contact bob = h.createQuery("select * from contacts where id = :id")
.bind("id", 2)
.map(new ContactMapper())
.findOnly();
assertThat(bob)
.extracting(Contact::getId, Contact::getName)
.containsExactly(2, "Bob");
}
}
示例12: test
import org.jdbi.v3.core.Jdbi; //导入依赖的package包/类
@Test
public void test() throws Exception {
Jdbi jdbi = Jdbi.create(ds.getDataSource());
jdbi.registerRowMapper(new ContactMapper());
try (Handle h = jdbi.open()) {
h.execute("create table contacts (id int primary key, name varchar(100))");
h.execute("insert into contacts (id, name) values (?, ?)", 1, "Alice");
h.execute("insert into contacts (id, name) values (?, ?)", 2, "Bob");
List<Contact> list = h.createQuery("select * from contacts order by id")
.mapTo(Contact.class)
.list();
assertThat(list)
.extracting(Contact::getId, Contact::getName)
.containsExactly(tuple(1, "Alice"),
tuple(2, "Bob"));
Contact bob = h.createQuery("select * from contacts where id = :id")
.bind("id", 2)
.mapTo(Contact.class)
.findOnly();
assertThat(bob)
.extracting(Contact::getId, Contact::getName)
.containsExactly(2, "Bob");
}
}
示例13: before
import org.jdbi.v3.core.Jdbi; //导入依赖的package包/类
public void before() throws Exception {
dbi = Jdbi.create(uri);
if (installPlugins) {
dbi.installPlugins();
}
plugins.forEach(dbi::installPlugin);
sharedHandle = dbi.open();
con = sharedHandle.getConnection();
try (Statement s = con.createStatement()) {
s.execute("create table people(id identity primary key, firstName varchar(50), lastName varchar(50), email varchar(255), created timestamp, modified timestamp);");
}
}
示例14: handleDbiInstance
import org.jdbi.v3.core.Jdbi; //导入依赖的package包/类
/**
* Inject a DBI instance to a field with {@link DBIInstance} annotation.
*
* @param test current test
* @param field current field
* @throws IllegalAccessException reflection error
*/
private void handleDbiInstance(Object test, Field field) throws IllegalAccessException {
if (!field.getType().equals(Jdbi.class)) {
throw new IllegalArgumentException("Unable inject a DBI instance to " +
"a field with type " + field.getType());
}
if (Modifier.isStatic(field.getModifiers())) {
throw new IllegalArgumentException("Unable inject a DBI instance to a static field");
}
field.setAccessible(true);
field.set(test, dbi);
}
示例15: createDBI
import org.jdbi.v3.core.Jdbi; //导入依赖的package包/类
@Override
public Jdbi createDBI(Properties properties) {
Jdbi dbi = Jdbi.create(properties.getProperty("db.url"), properties.getProperty("db.username"),
properties.getProperty("db.password"));
dbi.installPlugins();
return dbi;
}