2013年1月11日 星期五

Apache Commons DbUtils

這邊我要介紹Apache Commons DbUtils,在官網的介紹如下:

DbUtils is designed to be:
  • Small - you should be able to understand the whole package in a short amount of time.
  • Transparent - DbUtils doesn't do any magic behind the scenes. You give it a query, it executes it and cleans up for you.
  • Fast - You don't need to create a million temporary objects to work with DbUtils.
DbUtils is not:
  • An Object/Relational bridge - there are plenty of good O/R tools already. DbUtils is for developers looking to use JDBC without all the mundane pieces.
  • A Data Access Object (DAO) framework - DbUtils can be used to build a DAO framework though.
  • An object oriented abstraction of general database objects like a Table, Column, or PrimaryKey.
  • A heavyweight framework of any kind - the goal here is to be a straightforward and easy to use JDBC helper library.
通常我們會使用像JPA或Hibernate這類ORM framework來進行database的系統開發,但如果要extend Hibernate的功能,勢必還是需要直接寫JDBC相關的程式,這時候我們就可以用DbUtils來加快開發速度,不需要操作繁瑣的JDBC API,使用簡單的org.apache.commons.dbutils.QueryRunner就可以進行資料庫的操作,而且搭配多樣org.apache.commons.dbutils.ResultSetHandler<T>類別提供type safe,和Spring的JDBCTemplate相較之下更簡潔一些,可以試試看。