zoukankan      html  css  js  c++  java
  • Spring-data-jpa 之Specification in的用法

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

  • 相关阅读:
    周末单车行
    大笨
    随谈个人所得税起征点
    我的新球拍
    谈“家乐福抵制门”事件
    49届世乒赛(团体)电视转播时间表
    上海动物园拍摄记
    一个老外的故事
    请大家帮忙推荐一款名片管理软件
    不要先挂电话
  • 原文地址:https://www.cnblogs.com/mr-wuxiansheng/p/6596603.html
Copyright © 2011-2022 走看看