当前位置:网站首页>数据库中间件-jdbi
数据库中间件-jdbi
2022-08-09 06:21:00 【ggaofeng】
数据库中间件-jdbi https://www.jianshu.com/p/1ee34c858cb9
Jdbi3官方教程 https://www.jianshu.com/p/2d8e9550f650
如何使用连接池呢?
有两种方式创建JDBI对象
//用jdbc url创建jdbi对象
// H2 in-memory database
Jdbi jdbi = Jdbi.create("jdbc:h2:mem:test");
//用数据源创建jdbi对象
DataSource ds = ...
Jdbi jdbi = Jdbi.create(ds);
//数据源是专门提供连接的,连接池就是数据源的一个具体实现
public interface DataSource extends CommonDataSource, Wrapper {
Connection getConnection() throws SQLException;
Connection getConnection(String username, String password)throws SQLException;
}
Jdbi jdbi = Jdbi.create("jdbc:h2:file:./test");
Handle h = jdbi.open();
ResultIterator<Map<String, Object>> it = h.createQuery("select * from user").mapToMap().iterator();
while(it.hasNext()) {
System.out.println(it.next());
}
it.close(); --这个别漏了
h.close();
<dependency>
<groupId>com.github.ben-manes.caffeine</groupId>
<artifactId>caffeine</artifactId>
<version>2.9.0</version>
</dependency>
<dependency>
<groupId>org.jdbi</groupId>
<artifactId>jdbi3-core</artifactId>
<version>3.14.1</version>
</dependency>
<dependency>
<groupId>com.h2database</groupId>
<artifactId>h2</artifactId>
<version>1.4.200</version>
</dependency>
HikariCP连接池 ---最快的连接池 https://www.jianshu.com/p/15b846107a7c
import java.util.List;
import java.util.Map;
import org.jdbi.v3.core.Handle;
import org.jdbi.v3.core.Jdbi;
import org.jdbi.v3.core.result.ResultIterator;
public class App {
private static void f1() {
// Jdbi jdbi = Jdbi.create("jdbc:h2:mem:test");
Jdbi jdbi = Jdbi.create("jdbc:h2:~/test.gaofeng");
jdbi.useHandle(h -> {
h.execute("drop table contacts");
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");
});
// jdbi.useHandle(h -> h.execute("insert into contacts (id, name) values (?, ?)", 1, "Alice"));
String name = jdbi.withHandle(h-> h.select("select name from contacts where id = ?", 1).mapTo(String.class).one());
System.out.println(name);
List<Map<String, Object>> result = jdbi.withHandle(h-> h.select("select name from contacts ").mapToMap().list());
for (Map<String, Object> map : result) {
System.out.println(map);
}
jdbi.useHandle(h-> {
ResultIterator<Map<String, Object>> it= h.select("select name from contacts ").mapToMap().iterator();
while(it.hasNext()) {
System.out.println(it.next());
}
it.close();
});
}
private static void f2() {
// Handle jdbi = Jdbi.open("jdbc:h2:mem:test");
Handle jdbi = Jdbi.open("jdbc:h2:~/test.gaofeng");
int i= jdbi.execute("drop table contacts");
i= jdbi.execute("create table contacts (id int primary key, name varchar(100))");
i = jdbi.execute("insert into contacts (id, name) values (?, ?)", 1, "Alice");
System.out.println(i);
jdbi.execute("insert into contacts (id, name) values (?, ?)", 2, "Bob");
String name = jdbi.select("select name from contacts where id = ?", 1).mapTo(String.class).one();
System.out.println(name);
String name2 = jdbi.select("select name from contacts where id = ?", 2).mapTo(String.class).one();
System.out.println(name2);
jdbi.close();
}
}
边栏推荐
猜你喜欢
二硫化钼/二氧化铪的复合纳米材料(MoS2/HfO2)|钽掺杂二氧化铪纳米颗粒(齐岳bio)
[GO], arrays and slices
关于如何查找NXP S32K1xx系列单片机的封装信息和引脚定义
获取开发版安全码SHA1时遇到的报错
Deep Learning - Principles of Neural Networks 2
sql problem solving statement to create a table
05 多线程与高并发 - ThreadPoolExecutor 源码解析
Polyamide-amine (PAMAM) dendrimer-bismuth sulfide composite nanoparticles | bismuth sulfide modified Gd‑DTPA‑OA ligand | for scientific research
AIOT介绍
RT201 Domestic PA RF Power Amplifier Compatible with RFX2401C
随机推荐
kubernetes apparmor 简介
kubernetes security
【微信小程序】5分钟实现微信小程序海报
Xilinx Zynq ZynqMP DNA
Excel受保护的工作表怎么操作?
untiy 倒计时
phpstudy install flarum forum
年薪35W的测试工程师被裁亲身经验:不得不听的忠告
Getting Started with MATLAB Image Processing
Chapter 3 Search and Graph Theory (1)
盒子模型中垂直分布的基本内容
字符串的方法
中英文说明书丨TRC D-阿卓糖(D-Altrose)
A test engineer with an annual salary of 35W was laid off. Personal experience: advice that you have to listen to
深度学习-神经网络原理2
sqlserver导入数据类型问题
Deep Learning - Principles of Neural Networks 2
mongo+ycsb性能测试及线程数分析
Harbor Enterprise Mirror Warehouse Construction
Cysteine/Galactose/Perylenediimide Functionalized Fe3O4 Fe3O4 Nanomaterials | Scientific Research Reagents