Spring Data JPA 一对一 聚合查询

1: 在 主实体类中加入对应实体类

@Transient //排除数据库对应字段
private Stock stock;
@JsonInclude(JsonInclude.Include.NON_EMPTY)//为空时不显示
public Stock getStock() {
    return stock;
}

2: JPA 聚合查询 添加 关联条件

@Override
@Transactional(readOnly = true)
public Results list(Integer cid, Integer sid, String val, String content, Integer vip, Integer act, Integer type, Integer re, Integer flag, Integer cur, Integer page) {
    Pageable pageable = PageRequest.of(cur, page, Sort.Direction.DESC, "id");
    Page lst = skuRepository.findAll(new Specification() {

        @Override
        public Predicate toPredicate(Root root, CriteriaQuery query, CriteriaBuilder cb) {
            List list = new ArrayList();
            if (StringUtils.notZero(cid)) {
                list.add(cb.equal(root.get("cid").as(Integer.class), cid));
            }
            if (StringUtils.notZero(sid)) {
                list.add(cb.equal(root.get("sid").as(Integer.class), sid));
            }
            if (StringUtils.notEmpty(val)) {
                list.add(cb.like(root.get("val").as(String.class), "%" + val + "%"));
            }
            if (StringUtils.notEmpty(content)) {
                list.add(cb.like(root.get("content").as(String.class), "%" + content + "%"));
            }
            if (StringUtils.notZero(vip)) {
                list.add(cb.equal(root.get("vip").as(Integer.class), vip));
            }
            if (StringUtils.notZero(act)) {
                list.add(cb.equal(root.get("act").as(Integer.class), act));
            }
            if (StringUtils.notZero(type)) {
                list.add(cb.equal(root.get("type").as(Integer.class), type));
            }
            if (StringUtils.notZero(re)) {
                list.add(cb.equal(root.get("re").as(Integer.class), re));
            }
            if (StringUtils.notZero(flag)) {
                list.add(cb.equal(root.get("flag").as(Integer.class), flag));
            }
            Predicate[] p = new Predicate[list.size()];
            return cb.and(list.toArray(p));
        }
    }, pageable);

    List list = lst.getContent();
    Listskus = new ArrayList();
    for (Sku s:list){
      Stock stock =  stockRepository.findBySid(s.getId());
      if(stock!=null){
          s.setStock(stock);
      }
      skus.add(s);
    }
    list = skus;

    return new Results(list, lst.getSize(), lst.getTotalElements(), lst.getNumber(), lst.getTotalPages());
}

3: PostMan 测试结果

[

        {

            "id": ######,

            "cid": ######,

            "sid": ######,

            "val": "######,######",

            "content": "种类:黑,大小:xxl",

            "vip": ######,

            "type": ######,

            "re":######,

            "flag": ######,

            "smon": null,

            "lmon": null,

            "pic": "######",

            "fresh": null,

            "act": ######,

            "stock": {

                "id": ######,

                "sid": ######,

                "istock": ######,

                "astock": null,

                "nstock": null

            }

        },

        {

            "id": ######,

            "cid": ######,

            "sid": ######,

            "val": "######,######",

            "content": "######:######,######:######",

            "vip": ######,

            "type":######,

            "re": ######,

            "flag": ######,

            "smon": null,

            "lmon": null,

            "pic": "######",

            "fresh": null,

            "act": ######,

            "stock": {

                "id": ######,

                "sid": ######,

                "istock": ######,

                "astock": null,

                "nstock": null

            }

        }

    ]

 

 

你可能感兴趣的:(Spring Data JPA 一对一 聚合查询)