本文整理汇总了Java中org.jdbi.v3.core.Jdbi.create方法的典型用法代码示例。如果您正苦于以下问题:Java Jdbi.create方法的具体用法?Java Jdbi.create怎么用?Java Jdbi.create使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类org.jdbi.v3.core.Jdbi
的用法示例。
在下文中一共展示了Jdbi.create方法的14个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的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: 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());
}
示例4: 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() );
}
}
示例5: 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);
});
}
示例6: 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);
}
}
示例7: 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);
});
}
示例8: 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");
}
}
示例9: 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");
}
}
示例10: 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);");
}
}
示例11: 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;
}
示例12: testMapToDbObject
import org.jdbi.v3.core.Jdbi; //导入方法依赖的package包/类
@Test
public void testMapToDbObject() throws Exception {
Jdbi dbi = Jdbi.create(DbHelper.getHsqlDataSource());
dbi.installPlugins();
Handle handle = dbi.open();
try {
DbObject dbObject = handle.createQuery(DbHelper.TEST_DB_OBJECT_QUERY).mapTo(DbObject.class).findFirst().get();
DbHelper.assertDbObjectMapping(dbObject);
} finally {
handle.close();
}
}
示例13: dbi
import org.jdbi.v3.core.Jdbi; //导入方法依赖的package包/类
@Provides
@Singleton
Jdbi dbi(DataSource dataSource) {
return Jdbi.create(dataSource);
}
示例14: 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(ContactDao.class, dao -> {
dao.createContactTable();
dao.createPhoneTable();
dao.insertFullContact(Contact.create(1, "Alice",
new Phone(2, WORK, "800-555-1234"),
new Phone(3, MOBILE, "801-555-1212")));
dao.insertFullContact(Contact.create(4, "Bob"));
Contact alice = dao.getFullContactById(1);
assertThat(alice)
.extracting(Contact::getId, Contact::getName)
.containsExactly(1, "Alice");
assertThat(alice.getPhones())
.extracting(Phone::getId, Phone::getType, Phone::getPhone)
.containsExactly(tuple(2, WORK, "800-555-1234"),
tuple(3, MOBILE, "801-555-1212"));
Contact bob = dao.getFullContactById(4);
assertThat(bob)
.extracting(Contact::getId, Contact::getName)
.containsExactly(4, "Bob");
assertThat(bob.getPhones())
.isEmpty();
List<Contact> fullContacts = dao.listFullContacts();
assertThat(fullContacts)
.extracting(Contact::getId, Contact::getName)
.containsExactly(tuple(1, "Alice"),
tuple(4, "Bob"));
assertThat(fullContacts.get(0).getPhones())
.extracting(Phone::getId, Phone::getType, Phone::getPhone)
.containsExactly(tuple(2, WORK, "800-555-1234"),
tuple(3, MOBILE, "801-555-1212"));
assertThat(fullContacts.get(1).getPhones())
.isEmpty();
});
}