JOOQ入門-DSL

1.DSL簡介

org.jooq.impl.DSL是生成所有JOOQ對象的主要類。它作爲一個靜態工廠生成數據庫表達式,列表達式,條件表達式和其他查詢部分。JOOQ 2.0以後,爲了使客戶端代碼更加趨近於SQL,引進靜態工廠方法。當你使用DSL,只需要簡單的從DSL類引入靜態方法即可。

2.DSL用法

DSLContext 和DSL是訪問JOOQ類和功能的主要切入點。

創建一個常量的字段,

Field <String> field=DSL.val("Hello World")

 SQL語句:select * from shangfox_user where exists (select username from dual) 相當於

Condition condition = DSL.exists(DSL.select(DSL.field("username")));

Table<Record> table = DSL.table("shangfox_user");獲取表記錄對象

DSLContext dslContext = DSL.using(connection); 獲取數據庫連接

DSLContext引用了org.jooq.Configuration。Configuration配置了jOOQ的行爲,當jOOQ執行查詢時。DSLContext和DSL不同,DSLContext允許創建已經配置的和準備執行的sql語句。

例如:

DSLContext dslContext = DSL.using(connection);
 
Result<Record> fetch = dslContext.select().from(table).where("statu = 0").and("id > 4340").orderBy(DSL.field("time").asc()).fetch();
        for (Object aResult : fetch) {
            Record record = (Record) aResult;
            System.out.println(record);
        }


 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章