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 存在各种各样的问题。