controller层
@RequestMapping(value="/search")
@ResponseBody
public String search(HttpServletRequest request,int pageIndex,int size) {
HttpSession session = request.getSession();
User user = (User)session.getAttribute("user");
Page<Message> page = onlineMessageService.pageFind(user,pageIndex, size);
Map<String, Object> tableMap = new HashMap<String, Object>();
tableMap.put("total", page.getTotalElements());
tableMap.put("messages", page.getContent());
return ResponseJSON.instance().setData(tableMap).toJSON(); }
service层
@Transactional
public Page<Message> pageFind(final User user,int pageIndex,int size) {
Specification<Message> messgeSpeci = new Specification<Message>() {
@Override
public Predicate toPredicate(Root<Message> root, CriteriaQuery<?> query, CriteriaBuilder cb) {
List<Predicate> predicates = new ArrayList<Predicate>();
predicates.add(cb.equal(root.get("user").<String>get("status"),"1"));
predicates.add(cb.equal(root.<String> get("isOnlineRead"),"0"));
query.where(predicates.toArray(new Predicate[] {}));
return null;
}
};
return MessageRepository.findAll(messgeSpeci, new PageRequest(pageIndex,size));
}
持久层接口继承
public interface MessageRepository extends JpaRepository<Message, Long> ,JpaSpecificationExecutor<Message>{.....}