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。