SpringBootlist查询⽅法SpringBoot中根据不同查询条件,获取list列表。
springboot原理pdf
@Component
public class QuerySpecifications {}
(1)根据商户,查询商品全部列表
public Specification<Goods> fieldOwnerGoods(User owner) {
return new Specification<Goods>() {
@Override
public Predicate toPredicate(Root<Goods> root, CriteriaQuery<?> criteriaQuery, CriteriaBuilder criteriaBuilder) {
return criteriaBuilder.("owner"), owner);
}
};
}
@Service 中使⽤⽅法
public DataTablesOutput<Goods> list(DataTablesInput input, User owner) {
DataTablesOutput<Goods> output = goodsRepository.findAll(input, dsForOwner(owner));
log.debug("{}", output);
return output;
}
(2)根据不同的⽀付状态进⾏查询,获取list列表
PayState ⽀付状态-- 0:未⽀付,1:已⽀付,2:已取消
way 订单来源--0:后台  1:⼿机端
查询条件:(1)⽀付状态为1的(2)⽀付状态为1,订单状态来源为0 (3)⽀付状态为2  (4)商户名称下的订单public Specification<com.del.Order> orderForOwner(User owner) {
return new Specification<Order>() {
@Override
public Predicate toPredicate(Root<Order> root, CriteriaQuery<?> criteriaQuery, CriteriaBuilder criteriaBuilder) {
List<Predicate> predicates = wArrayList();
Predicate predicate = criteriaBuilder.("payState"), 1);
Predicate predicate6 = criteriaBuilder.("payState"), 2);
Predicate predicate1 = criteriaBuilder.("payState"), 0);
Predicate predicate2 = criteriaBuilder.("way"), 1);
Predicate predicate3 = criteriaBuilder.and(predicate1, predicate2);
Predicate predicate4 = (predicate, predicate3, predicate6);
Predicate predicate5 = criteriaBuilder.("fieldSchedule").get("field").get("owner"), owner);
return criteriaBuilder.and(predicate4, predicate5);
}
};
}
@Service 中使⽤⽅法
public DataTablesOutput<Order> list(DataTablesInput input, User user) {
input.addOrder("fieldSchedule.start",false);//排序⽅法,按照场地开始时间排序,false 倒叙,true 正序
DataTablesOutput<Order> output = orderRepository.findAll(input, derForOwner(user));
log.debug("{}", output);
return output;
}
(3)多个列表,查询条件不同,放到⼀个⽅法⾥
public Specification<com.del.Order> orderForOwnerAndPayState(User owner, int single) {
return new Specification<Order>() {
@Override
public Predicate toPredicate(Root<Order> root, CriteriaQuery<?> criteriaQuery, CriteriaBuilder criteriaBuilder) {
List<Predicate> predicates = wArrayList();
//single:0 待校验,1:已付款,2:已取消,3:待付款
Predicate predicate = criteriaBuilder.("fieldSchedule").get("field").get("owner"), owner);
if(single == 0){
Predicate predicate1 = criteriaBuilder.("state"), 0);
Predicate predicate2 = criteriaBuilder.("payState"), 1);
Predicate predicate3 = criteriaBuilder.("way"), 0);
Predicate predicate4 = criteriaBuilder.and(predicate1, predicate2,predicate3);
return criteriaBuilder.and(predicate, predicate4 );
}else if(single == 1){
Predicate predicate1 = criteriaBuilder.("payState"), 1);
return criteriaBuilder.and(predicate, predicate1 );
}else if(single == 2){
Predicate predicate1 = criteriaBuilder.("payState"), 2);
return criteriaBuilder.and(predicate, predicate1 );
}else if(single == 3){
Predicate predicate1 = criteriaBuilder.("payState"), 0);
Predicate predicate2 = criteriaBuilder.("way"), 1);
Predicate predicate3 = criteriaBuilder.and(predicate1, predicate2);
return criteriaBuilder.and(predicate, predicate3 );
}else{
return null;
}
/*return criteriaBuilder.Array(new Predicate[predicates.size()]));*/
}
};
}
@Service 中使⽤⽅法
public DataTablesOutput<Order> listByPayState(DataTablesInput input, User owner, final int single) {
input.addOrder("fieldSchedule.start",false);
DataTablesOutput<Order> output = orderRepository.findAll(input, derForOwnerAndPayState(owner, single));    log.debug("{}", output);
return output;
}

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。