基於springdata的組合分頁查詢
阿新 • • 發佈:2019-01-03
((root, query, cb) -> {
List<Predicate> predicateList = new ArrayList<>();
Predicate[] predicates;
//判斷orderStatus的狀態,狀態放到查詢條件中
2. orderStatus有三種狀態,對應三種value,查詢待支付,已支付,廢棄中的任意一種或者幾種,我們只需要將選中的引數傳給後臺,並用“,”隔開.例如:orderStatus="2,3,4",後臺解析將引數封裝成條件,用or連線即可
if (StringUtils.isNotBlank(orderCondition.getOrderStatus())){
String [] array = orderCondition.getOrderStatus().trim().split(",");
Predicate[] p=new Predicate[array.length];
for(int i=0;i<array.length;i++){
p[i] = cb.equal(root.get("orderStatus" ), array[i]);
}
predicateList.add(cb.or(p));
predicates = new Predicate[predicateList.size()];
}else{
predicates = new Predicate[0];
}
//前臺傳入的actualConsume的值為1(0-50),2(50-100),3(100以上)
//判斷actualConsume的值
if (StringUtils.isNotBlank(orderCondition.getActualConsume())){
String [] array = orderCondition.getActualConsume().trim().split(",");
Predicate[] p = new Predicate[array.length];
for(int i=0;i<array.length;i++){
if(Integer.parseInt(array[i])==3){
//消費金額100到100以上
p[i] = cb.greaterThanOrEqualTo(root.get("actualConsume"),100);
}else if(Integer.parseInt(array[i])==2){
p[i] = cb.between(root.get("actualConsume"),50,100);
}else{
p[i] = cb.between(root.get("actualConsume"),0,50);
}
}
predicateList.add(cb.or(p));
predicates = new Predicate[predicateList.size()];
}else{
predicates = new Predicate[0];
}
//篩選出符合時間條件的訂單
predicateList.add(cb.between(root.get("startTime"), startDate, endDate));
return cb.and(predicateList.toArray(predicates));
},pageAble);