当前位置:网站首页>数据库中间件-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();
}
}边栏推荐
猜你喜欢
随机推荐
22年下高项论文题目预测
.NET高级技术
SiO2/KH550修饰四氧化三铁纳米磁性颗粒|PDA包裹四氧化三铁磁性纳米颗粒(科研级)
excel表格如何不需鼠标往下拖动而自动往下填
抗菌药物丨Toronto Research Chemicals 天冬酰胺D
S7-200SMART PLC Modbus TCP communication
A test engineer with an annual salary of 35W was laid off. Personal experience: advice that you have to listen to
qt发送邮件程序
【R语言】对文件进行归一化整理到各文件类型文件夹
2022-08-08:给定一个数组arr,表示从早到晚,依次会出现的导弹的高度。 大炮打导弹的时候,如果一旦大炮定了某个高度去打,那么这个大炮每次打的高度都必须下降一点。 1) 如果只有一个大炮,返回
MYSQL高级篇-----查询截取分析,锁机制,主从复制
[R language] Normalize and organize files into folders of various file types
力扣刷题180
超顺磁四氧化三铁@二氧化硅@硫化镉纳米核壳结构材料|表面接枝mPEG的Fe3O4磁性纳米颗粒(f-Fe3O4)|相关产品
pdf加密、找回密码
GNNExplainer应用于节点分类任务
22 high mid term paper topics forecast
Text String Length Sorting - Online Tool
深度学习-神经网络原理2
二硫化钼/二氧化铪的复合纳米材料(MoS2/HfO2)|钽掺杂二氧化铪纳米颗粒(齐岳bio)

![[GO], arrays and slices](/img/71/86126c41d0f43aa8b9f232b219f5d7.png)







