org.jooq.impl.DSL是生成所有jOOQ對象的主要類。它作為一個靜態的工廠去生成數據庫表表達式,列表達式,條件表達式和其他查詢部分。jOOQ 2.0以后,為了使客戶端代碼更加的趨近於SQL,引進了靜態工廠方法。當你使用DSL時,你只需要簡單的從DSL class引入所有靜態方法即可。列如:importstatic org.jooq.impl.DSL.*;
DSLContext 和DSL是訪問JOOQ類和功能的主要入口點。
舉例:創建一個常量值的字段, Field<String> field = DSL.val("Hello World")
Condition condition = DSL.exists(DSL.select(DSL.field("username")));相當於SQL [select * from shangfox_user where exists (select username from dual)]
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);
- }
加油!WinterChou。