首页 > 代码库 > spring-data-jpa动态条件查询
spring-data-jpa动态条件查询
//获取动态条件的集合
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);
spring-data-jpa动态条件查询
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。