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