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 存在各種各樣的問題。

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM