//获取动态条件的集合
List<Long> list = new ArrayList<Long>(); Long sysUserId = currentUser.getSysUserId(); if (sysUserId != null) { SysUser sysUser = sysUserRepository.findOne(sysUserId); if (sysUser != null) { String groupItemIds = sysUser.groupItemIds(); if (groupItemIds != null && !groupItemIds.isEmpty()) { String[] str = StringUtils.split(groupItemIds,","); for (String s : str) { if (s != null && !s.isEmpty() && s != "") { list.add(Long.valueOf(s)); } } } } }
//封装Specification查询条件
Specification<Goods> spec = (root, query, cb) -> { List<Predicate> predicates = new ArrayList<Predicate>(); List<Predicate> preList = new ArrayList<Predicate>(); if (list != null && list.size()>0) { preList.add(root.<Long>get("groupItemId").in(list)); } if (sysUserId != null) { Predicate predicate = cb.equal(root.get(Goods_.crtUserId), sysUserId); preList.add(predicate); } Predicate p = cb.or(preList.toArray(new Predicate[preList.size()])); predicates.add(p); if (!predicates.isEmpty()) { return cb.and(predicates.toArray(new Predicate[0])); } else { return null; } }; Page<Goods> pageresult = goodsRepository.findAll(spec, pageable);