1.一個簡單的 Predicate 的示例
構建簡單的Predicate示例: Predicate p1=cb.like(root.get(“name”).as(String.class), “%”+uqm.getName()+“%”); Predicate p2=cb.equal(root.get("uuid").as(Integer.class), uqm.getUuid()); Predicate p3=cb.gt(root.get("age").as(Integer.class), uqm.getAge()); 構建組合的Predicate示例: Predicate p = cb.and(p3,cb.or(p1,p2));
2.in 用法實例
//根據競買人客戶名稱進行查詢 In<Long> in = cb.in(root.get(BailPay_.customerId)); if(customerName != null && !customerName.isEmpty()){ List<Customer> customerList = customerRepository.findAllByCustomerName(customerName); if (customerList != null && customerList.size()>0) { for (Customer customer : customerList) { if (customer != null) { in.value(customer.id()); } } predicates.add(in); }else{ Predicate predicateCustomerId = cb.equal(root.get(BailPay_.customerId), 0L); predicates.add(predicateCustomerId); } }
3.今天總結這種做法的目的是,項目中碰上了匹配不定個數的條件的需求,使用cb.or 存在各種各樣的問題。