网上有很多使用 QueryDSL 的例子,如下所示:
public class CustomerRepositoryImpl
extends QueryDslRepositorySupport
implements CustomerRepositoryCustom {
public Iterable<Customer> findAllLongtermCustomersWithBirthday() {
QCustomer customer = QCustomer.customer;
return from(customer)
.where(hasBirthday().and(isLongTermCustomer()))
.list(customer);
}
}
这段代码是有道理的,但我想知道为什么 customer
在对 list()
的方法调用中是“重复的”。
从from(customer)
中的引用中,类型不应该是显而易见的吗?
最佳答案
from
定义源并list
投影。 Querydsl 查询类没有任何返回类型的泛型类型参数,投影(选择部分)在查询构造链的最后一部分中定义。
需要与源不同的投影的情况示例
- 仅限特定列:
query.list(customer.firstName, customer.lastName)
- 构造函数调用:
Projections.constructor(...)
- Bean 数量:
Projections.bean(...)
- 使用了多个 from 调用
关于spring - 为什么 Spring Data JPA 和 QueryDSL 中会使用重复的变量?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26533491/