有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java I在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) 个答案

  1. # 1 楼答案

    你可以试试

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

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