欢迎来到天天文库
浏览记录
ID:40567409
大小:103.00 KB
页数:7页
时间:2019-08-04
《Spring JPA 查询的几种方式并处理分页》由会员上传分享,免费在线阅读,更多相关内容在教育资源-天天文库。
1、SpringJPA查询的几种方式并处理分页第一种NamedQuery(返回方式为列模式[原生态sql的复杂查询])1)dao层处理查询并分页Java代码 1.@SuppressWarnings("unchecked") 2. public PageResult getList(Integer currentPage){ 3. PageResult pageResult = new PageResult(); 4. int pageSize = Constant.DEFAULT_PAGE_SIZE; 5.
2、 int start = (currentPage - 1) * pageSize; 6. Query query = getEntityManager().createNamedQuery("ReturnTrainAppyUser"); 7. int total = query.getResultList().size(); 8. // 判断分页 9. if (start < total && pageSize > 0) { 10. query.
3、setFirstResult(start); 11. query.setMaxResults(pageSize); 12. pageResult.setFirst(start); 13. pageResult.setPageSize(pageSize); 14. } 15. 16. pageResult.setTotalCount(total); 17. pageResult.setPageR
4、esultList(query.getResultList()); 18. return pageResult; 19. } 2)控制层代码Java代码 1.@RequestMapping("/applyList") 2. public String applyList(HttpServletRequest request, 3. HttpServletResponse response, Model model) throws Exception { 4. Integer currentPage = 1;
5、 5. Integer pageNum = getIntParameter(request, "pageNum"); 6. if (pageNum != null) { 7. currentPage = getIntParameter(request, "pageNum"); 8. } 将夜www.jiangyea.com1. PageResult a = trainApplyService.findContentResult(currentPage); 2.
6、 addPageResultModel2(a, currentPage, model); 3. return "common/train/admin/applyList"; 4. } 处理分页参数 Java代码 1.protected void addPageResultModel2(PageResult pct,Integer currentPage, Model model) { 2. model.addAttribute("totalCount", pct.getTotalCount()); 3.
7、 model.addAttribute("numPerPage", Constant.DEFAULT_PAGE_SIZE); 4. model.addAttribute("pageNum", currentPage); 5. model.addAttribute("pageNumShown", pct.getPageCount(pct.getTotalCount(), Constant.DEFAULT_PAGE_SIZE)); 6.
此文档下载收益归作者所有