jooq多表查询_动态创建JOOQ查询

I need to create a JOOQ SELECT query dynamically based on the set of parameters. I dont know how to append it dynamically.

Please help

Thanks in advance.

解决方案

jOOQ has two types of APIs to construct queries.

The DSL API that allows for creating inline SQL statements in your Java code, e.g.

create.select(T.A, T.B).from(T).where(T.X.eq(3).and(T.Y.eq(5)));

The "model" API that allows for incremental SQL building. At any time, you can access the "model" API through the getQuery() method on a DSL query object

An example of what you want to do is given in the manual here:

For instance, optionally adding a join:

DSLContext create = DSL.using(configuration);

SelectQuery query = create.selectQuery();

query.addFrom(AUTHOR);

// Join books only under certain circumstances

if (join)

query.addJoin(BOOK, BOOK.AUTHOR_ID.equal(AUTHOR.ID));

Result> result = query.fetch();

Or, optinally adding conditions / predicates:

query.addConditions(BOOK.TITLE.like("%Java%"));

query.addConditions(BOOK.LANGUAGE_CD.eq("en"));

UPDATE: Given your comments, that's what you're looking for:

// Retrieve search strings from your user input (just an example)

String titleSearchString = userInput.get("TITLE");

String languageSearchString = userInput.get("LANGUAGE");

boolean lookingForTitles = titleSearchString != null;

boolean lookingForLanguages = languageSearchString != null;

// Add only those conditions that the user actually provided:

if (lookingForTitles)

query.addConditions(BOOK.TITLE.like("%" + titleSearchString + "%"));

else if (lookingForLanguages)

query.addConditions(BOOK.LANGUAGE_CD.eq(languageSearchString));

Note, you can also use the Field.compare(Comparator, Object) methods:

// Initialise your dynamic arguments

Field field = BOOK.TITLE;

Comparator comparator = Comparator.LIKE;

String value = "%" + titleSearchString + "%";

// Pass them to the field.compare() method

query.addConditions(field.compare(comparator, value));

For more info, consider the org.jooq.SelectQuery Javadoc

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值