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

spring data jpa分页

时间:2015-03-09 12:16:29      阅读:181      评论:0      收藏:0      [点我收藏+]

标签:

controller层

 1 @RequestMapping(value="/search")
 2     @ResponseBody
 3     public String search(HttpServletRequest request,int pageIndex,int size) {
 4         HttpSession session = request.getSession();
 5         User user = (User)session.getAttribute("user");
 6         Page<Message> page = onlineMessageService.pageFind(user,pageIndex, size);
 7         Map<String, Object> tableMap = new HashMap<String, Object>();
 8         tableMap.put("total", page.getTotalElements());
 9         tableMap.put("messages", page.getContent());
10         return ResponseJSON.instance().setData(tableMap).toJSON();
11 
12     }

service层

 1 @Transactional
 2     public Page<Message> pageFind(final User user,int pageIndex,int size) {
 3         Specification<Message> messgeSpeci = new Specification<Message>() {
 4             @Override
 5             public Predicate toPredicate(Root<Message> root, CriteriaQuery<?> query, CriteriaBuilder cb) {
 6                 List<Predicate> predicates = new ArrayList<Predicate>();
 7                 predicates.add(cb.equal(root.get("user").<String>get("status"),"1"));
 8                 predicates.add(cb.equal(root.<String> get("isOnlineRead"),"0"));
 9                 query.where(predicates.toArray(new Predicate[] {}));
10                 return null;
11             }
12         };
13         return MessageRepository.findAll(messgeSpeci, new PageRequest(pageIndex,size));
14     }

 持久层接口继承

1 public interface MessageRepository extends JpaRepository<Message, Long> ,JpaSpecificationExecutor<Message>{.....}

 

  

spring data jpa分页

标签:

原文地址:http://www.cnblogs.com/cora/p/4323025.html

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