您的位置:首页 > 编程语言 > Java开发

Spring-data-jpa 之Specification in的用法

2017-03-21 20:51 381 查看
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 存在各种各样的问题。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: