JOOQ入门-DSL

1.DSL简介

org.jooq.impl.DSL是生成所有JOOQ对象的主要类。它作为一个静态工厂生成数据库表达式,列表达式,条件表达式和其他查询部分。JOOQ 2.0以后,为了使客户端代码更加趋近于SQL,引进静态工厂方法。当你使用DSL,只需要简单的从DSL类引入静态方法即可。

2.DSL用法

DSLContext 和DSL是访问JOOQ类和功能的主要切入点。

创建一个常量的字段,

Field  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 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 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);
        }


 

你可能感兴趣的:(Java基础,MySQL,JOOQ)