码迷,mamicode.com
首页 > 编程语言 > 详细

spring-data-jpa动态条件查询

时间:2017-05-16 23:10:52      阅读:174      评论:0      收藏:0      [点我收藏+]

标签:epo   dsr   osi   group   ret   one   find   utils   code   

//获取动态条件的集合
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动态条件查询

标签:epo   dsr   osi   group   ret   one   find   utils   code   

原文地址:http://www.cnblogs.com/mr-wuxiansheng/p/6863865.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!