Five Minute Introduction
2015-11-25 23:33
393 查看
Introduction
JDBI is a SQL convenience library for Java. It attempts to expose relational database access in idiommatic Java, using collections, beans, and so on, while maintaining the same level of detail as JDBC. It exposes two different style APIs, a fluent style and a sql object style.Fluent API
// using in-memory H2 database DataSource ds = JdbcConnectionPool.create("jdbc:h2:mem:test", "username", "password"); DBI dbi = new DBI(ds); Handle h = dbi.open(); h.execute("create table something (id int primary key, name varchar(100))"); h.execute("insert into something (id, name) values (?, ?)", 1, "Brian"); String name = h.createQuery("select name from something where id = :id") .bind("id", 1) .map(StringMapper.FIRST) .first(); assertThat(name, equalTo("Brian")); h.close();
The DBI type is analogous to a JDBC DataSource, and will usually be constructed by passing in a JDBC DataSource. There are alternate constructors which take JDBC URL and credentials, and other means. From the DBI instance you obtain Handle instances. A Handle represents a single connection to the database. Handles rely on an underlying JDBC connection object.
With a handle you may create and execute statements, queries, calls, batches, or prepared batches. In the above example we execute a statement to define a table, execute another statement, this time with two positional arguments to insert a value, and finally construct a query, bind a value to a named argument in the query, map the results to a a String, and take the first result which comes back.
The named argument facility on statements and queries is provided by JDBI – it parses out the SQL and uses positional parameters when actually constructing the prepared statements. The above example uses the default colon-demarcated parser, but an alternative hash delimited parser is included as well for use with databases which use colons in their grammars, such as PostgreSQL.
SQL Object API
The second, SQL object, style API simplifies the common idiom of creating DAO objects where a single method maps to a single statement. A SQL object definition is an annotated interface, such as:public interface MyDAO{ @SqlUpdate("create table something (id int primary key, name varchar(100))") void createSomethingTable(); @SqlUpdate("insert into something (id, name) values (:id, :name)") void insert(@Bind("id") int id, @Bind("name") String name); @SqlQuery("select name from something where id = :id") String findNameById(@Bind("id") int id); /** * close with no args is used to close the connection */ void close(); }
This interface defines two updates, the first to create the same table as in the fluent api example, and the second to do the same insert, the third defines a query. In the second two cases, notice that the arguments to the statements are past to the method, and bound by name.
The final method, close(), is special. When it is invoked it will close the underlying JDBC connection. The method may be declared to raise an exception, such as the close() method does on java.io.Closeable, making it suitable for use with automatic resource management in Java 7.
To use this sql object definition, we use code like so:
/ using in-memory H2 database via a pooled DataSource JdbcConnectionPool ds = JdbcConnectionPool.create("jdbc:h2:mem:test2", "username", "password"); DBI dbi = new DBI(ds); MyDAO dao = dbi.open(MyDAO.class); dao.createSomethingTable(); dao.insert(2, "Aaron"); String name = dao.findNameById(2); assertThat(name, equalTo("Aaron")); dao.close(); ds.dispose();
We obtain an instance of the sql object from the DBI instance, and then call methods on it. There are a couple different ways of creating sql object instances. The one one here binds the object to a specific handle, so we need to make sure to close the object when we are finished with it.
相关文章推荐
- Linux下oracle开机自启动设置
- 通往全栈工程师的捷径 —— react
- ERROR 1044 (42000): Access denied for user ''@'localhost' to database 'mysql'
- IP地址
- 山东理工ACM:1196
- Xml处理——获取Xml格式数据
- 指针 与 数组 以及 a 与 &a的区别
- 开始研究web,mark一下
- Android之ThreadPoolExecutor
- 数据库分类
- 项目总结(一) 手机清空后台后,依旧可以接收到推送的解决方案
- MapReduce编程实例之自定义排序
- Lua学习笔记(1) : 第一个Lua程序 - Lua与C++混合编程
- IOS UIToolbar
- 1.显示和隐藏ActionBar
- 磁盘检验[转自vbird]
- 【C#】匿名方法的终结者——Lambda表达式
- 是什么限制了你的能力和收入
- 我的博客园博客
- C/C++ 笔试、面试题目大汇总2