JOOQ初學-DSL核心


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語句。

列如:

[java] view plain copy
  1. DSLContext dslContext = DSL.using(connection);  
  2.   
  3. Result<Record> fetch = dslContext.select().from(table).where("statu = 0").and("id > 4340").orderBy(DSL.field("time").asc()).fetch();  
  4.         for (Object aResult : fetch) {  
  5.             Record record = (Record) aResult;  
  6.             System.out.println(record);  
  7.         }  

詳細demo

加油!WinterChou。



免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM