• SpringBoot list查询方法


    SpringBoot中根据不同查询条件,获取list列表。
    @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.equal(root.get("owner"), owner);
    }
    };
    }
    @Service 中使用方法
    public DataTablesOutput<Goods> list(DataTablesInput input, User owner) {
    DataTablesOutput<Goods> output = goodsRepository.findAll(input, querySpecifications.goodsForOwner(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.ishare.football.model.Order> orderForOwner(User owner) {
    return new Specification<Order>() {
    @Override
    public Predicate toPredicate(Root<Order> root, CriteriaQuery<?> criteriaQuery, CriteriaBuilder criteriaBuilder) {
    List<Predicate> predicates = Lists.newArrayList();
    Predicate predicate = criteriaBuilder.equal(root.get("payState"), 1);
    Predicate predicate6 = criteriaBuilder.equal(root.get("payState"), 2);
    Predicate predicate1 = criteriaBuilder.equal(root.get("payState"), 0);
    Predicate predicate2 = criteriaBuilder.equal(root.get("way"), 1);
    Predicate predicate3 = criteriaBuilder.and(predicate1, predicate2);
    Predicate predicate4 = criteriaBuilder.or(predicate, predicate3, predicate6);
    Predicate predicate5 = criteriaBuilder.equal(root.get("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, querySpecifications.orderForOwner(user));
    log.debug("{}", output);

    return output;
    }
    (3)多个列表,查询条件不同,放到一个方法里

    public Specification<com.ishare.football.model.Order> orderForOwnerAndPayState(User owner, int single) {
    return new Specification<Order>() {
    @Override
    public Predicate toPredicate(Root<Order> root, CriteriaQuery<?> criteriaQuery, CriteriaBuilder criteriaBuilder) {
    List<Predicate> predicates = Lists.newArrayList();
    //single:0 待校验,1:已付款,2:已取消,3:待付款
    Predicate predicate = criteriaBuilder.equal(root.get("fieldSchedule").get("field").get("owner"), owner);
    if(single == 0){
    Predicate predicate1 = criteriaBuilder.equal(root.get("state"), 0);
    Predicate predicate2 = criteriaBuilder.equal(root.get("payState"), 1);
    Predicate predicate3 = criteriaBuilder.equal(root.get("way"), 0);
    Predicate predicate4 = criteriaBuilder.and(predicate1, predicate2,predicate3);
    return criteriaBuilder.and(predicate, predicate4 );
    }else if(single == 1){
    Predicate predicate1 = criteriaBuilder.equal(root.get("payState"), 1);
    return criteriaBuilder.and(predicate, predicate1 );

    }else if(single == 2){
    Predicate predicate1 = criteriaBuilder.equal(root.get("payState"), 2);
    return criteriaBuilder.and(predicate, predicate1 );

    }else if(single == 3){
    Predicate predicate1 = criteriaBuilder.equal(root.get("payState"), 0);
    Predicate predicate2 = criteriaBuilder.equal(root.get("way"), 1);
    Predicate predicate3 = criteriaBuilder.and(predicate1, predicate2);
    return criteriaBuilder.and(predicate, predicate3 );

    }else{
    return null;
    }

    /*return criteriaBuilder.and(predicates.toArray(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, querySpecifications.orderForOwnerAndPayState(owner, single));
    log.debug("{}", output);
    return output;
    }
     
  • 相关阅读:
    asp.net连接数据库,在web.config中配置数据库连接
    在处理向该请求提供服务所需的配置文件时出错。请检查下面的特定错误详细信息并适当地修改配置文件如何解决
    centos7查看CPU的利用率
    阿里云云盘在线扩容
    微信和支付宝付款码条码规则(官方)
    微信和支付宝付款码条码规则
    IPFS与般若文海
    Moira果老星宗七政四余排盘软件下载
    Odoo 电子公章/印章模块
    playtoearn
  • 原文地址:https://www.cnblogs.com/aliy-pan/p/5919808.html
Copyright © 2020-2023  润新知