我有办法在QueryDSL中找到示例吗?

时间:2019-04-17 18:43:24

标签: java jpa querydsl

是否可以使用示例实体来创建QueryDSL谓词?例如:

Customer customerExample = new Customer();
customerExample.setName("John");
customerExample.setCountry("EUA");
QCustomer customer = QCustomer.customer;
Customer bob = queryFactory.selectFrom(customer)
  .where(customer.example(customerExample )) // matches customers with name like "John" and country like "EUA"
  .fetch();

如果没有,除了使用SpringJPA之外,还有其他选择吗?

1 个答案:

答案 0 :(得分:0)

您可以尝试

List<Tuple> searchResults = queryFactory.selectFrom(customer)
      .where(customer.name.eq(customerExample.getName()), customer.country.eq(customerExample.getCountry())
      .fetch();

此外,它将返回List<Tuple>而不是“客户”。因此,您之后必须将结果映射到“客户”。