如何在没有域类的querydsl中构造查询 [英] How to construct query in querydsl without domain classes
问题描述
在寻找Java库以数据库不可知的方式构建查询时,我遇到了许多信息,包括iciql,querydsl,jooq,joist,hibernate等.
While looking for java libraries to build queries in a database agnostic way I came across many including iciql, querydsl, jooq, joist, hibernate etc.
我想要一些不需要配置文件并且可以使用动态架构的东西. 对于我的应用程序,我在运行时就了解数据库和架构,因此我没有架构的任何配置文件或域类.
I wanted something that does not require configuration files and can work with dynamic schemas. For my application, I come to know about the database and the schema at runtime so I won't have any configuration files or domain classes for the schema.
这似乎是querydsl的核心目标之一,但仔细阅读querydsl的文档,我看到了很多使用域类构建动态查询的示例,但是我没有遇到任何解释如何构建此类数据库不可知查询的示例.仅使用有关架构的动态信息.
This seems to be one of the core goals of querydsl but going through the documentation for querydsl I see a lot of examples for building dynamic queries using domain classes but I have not come across anything that explains how to build such database agnostic queries using just the dynamic information I have about the schema.
Jooq offers such functionality(See: http://www.jooq.org/doc/3.2/manual/getting-started/use-cases/jooq-as-a-standalone-sql-builder/) but have a restrictive license if I want to expand my focus to Oracle or MS SQL(Which I may not love but need to support).
有querydsl经验的人可以让我知道用querydsl是否可以做到这一点,如果可以,怎么办?
Can someone with experience in querydsl let me know if such a thing is possible with querydsl, and if yes, how.
如果有人也知道任何其他满足我要求的东西,将不胜感激.
If someone know of any other too which can satisfy my requirements, it would be really appreciated.
推荐答案
一个非常简单的SQL查询,例如:
A very simple SQL query such as this:
@Transactional
public User findById(Long id) {
return new SQLQuery(getConnection(), getConfiguration())
.from(user)
.where(user.id.eq(id))
.singleResult(user);
}
...可以像这样动态创建(不添加任何糖):
...can be created dynamically like this (without any sugar added):
@Transactional
public User findById(Long id) {
Path<Object> userPath = new PathImpl<Object>(Object.class, "user");
NumberPath<Long> idPath = Expressions.numberPath(Long.class, userPath, "id");
StringPath usernamePath = Expressions.stringPath(userPath, "username");
Tuple tuple = new SQLQuery(getConnection(), getConfiguration())
.from(userPath)
.where(idPath.eq(id))
.singleResult(idPath, usernamePath);
return new User(tuple.get(idPath), tuple.get(usernamePath));
}
这篇关于如何在没有域类的querydsl中构造查询的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!