当前位置:网站首页>数据库中间件-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();
}
}
边栏推荐
- untiy countdown
- 05 多线程与高并发 - ThreadPoolExecutor 源码解析
- Fe3O4/SiO2 Composite Magnetic Nanoparticles Aminated on SiO2-NH2/Fe3O4 Surface (Qiyue Reagent)
- 二硫化钼/二氧化铪的复合纳米材料(MoS2/HfO2)|钽掺杂二氧化铪纳米颗粒(齐岳bio)
- 字符串的方法
- untiy 倒计时
- kubernetes security
- GNNExplainer应用于节点分类任务
- CalBioreagents超全Id 蛋白兔单克隆抗体,助力科研
- Xilinx Zynq ZynqMP DNA
猜你喜欢
excel表格如何不需鼠标往下拖动而自动往下填
Unity 五子棋游戏设计和简单AI(2)
[Deep Learning] Let's talk about what is convolutional neural network and what does convolution mean?
深度学习-神经网络原理2
Unity Gobang Game Design and Simple AI (2)
[email protected]@BSABiS nanoparticles) | dendrimer-stabilized bismuth sulfide nanop"/>
Ferric oxide/bismuth sulfide nanocomposites ([email protected]@BSABiS nanoparticles) | dendrimer-stabilized bismuth sulfide nanop
Likou Brush Question 180
sql问题解答创建表的语句
Molybdenum disulfide/hafnium dioxide composite nanomaterials (MoS2/HfO2) | tantalum-doped hafnium dioxide nanoparticles (Qi Yue bio)
牛客每日刷题之链表
随机推荐
Selection of MOS tube
域名批量查询在线工具
5年测试开发工程师感悟——写给还在迷茫中的朋友
【JMeter】jmeter测试 - 上传多个图片/批量上传图片接口 CSV文件参数化方法
2022-08-08:给定一个数组arr,表示从早到晚,依次会出现的导弹的高度。 大炮打导弹的时候,如果一旦大炮定了某个高度去打,那么这个大炮每次打的高度都必须下降一点。 1) 如果只有一个大炮,返回
RNN-T
Data center project preliminary summary
力扣刷题180
VB.net程序关闭后后台还在与SQL连接
Unity Gobang Game Design and Simple AI (2)
[GO], arrays and slices
Getting Started with MATLAB Image Processing
正则表达式-判断字符串是否匹配“AABB”模式
Invalid argument(s) appears when redis runs lua script
tidb crash test
Bismuth sulfide nanorods with CT imaging function | Bismuth sulfide-zinc protoporphyrin composites (PAMAM/Bi2S3 composite nanoparticles)
Regular Expression - Determine if a string matches the "AABB" pattern
sqlserver导入数据类型问题
[R language] Extract all files under a folder to a specific folder
22年下高项论文题目预测