当前位置: 首页 > 工具软件 > JDBI > 使用案例 >

数据库中间件-jdbi

萧阳波
2023-12-01

数据库中间件-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();
    }
}

 类似资料: