0
点赞
收藏
分享

微信扫一扫

Spring JPA 查询的几种方式并处理分页02_李孟_新浪博客

程序员伟杰 2022-03-25 阅读 52

承接Spring JPA 查询的几种方式并处理分页01

 4)页面处理

Java代码  

1. "100%" cellspacing="0" cellpadding="0">  

2.             

3.                 class="Train_Resultlist-item">  

4.                     "50">NO.  

5.                     

6.                     

7.                     

8.                 

9.             

10.             

11.                 "item" items="${itemList}" varStatus="s">  

12.                 

13.                     

14.                     

15.                     

16.                     

22.                 

23.                   

24.                   

25.                 

26.                     "4">  

27.                             class="green-black">  

28.                                 共${totalCount}条数据.  

29.                                 "首页" href="">首页   

30.                                 if test="${currentPage le 1}" var="syy">  

31.                                 "上一页" href="#">上一页  

32.                                 if>  

33.                                 if test="${!syy}">  

34.                                 "上一页" href="">上一页  

35.                                 if>  

36.                                 "pageNo" begin="1" end="${pageNumShown}">  

37.                                         "">  

38.                                         if test="${currentPage eq pageNo}" var="rsFy">  

39.                                             ​${pageNo}​  

40.                                         if>  

41.                                         if test="${!rsFy}">  

42.                                             ${pageNo}  

43.                                         if>  

44.                                           

45.                                   

46.                                 if test="${currentPage ge pageNumShown}" var="xyy">  

47.                                     "下一页" href="#">下一页  

48.                                 if>  

49.                                 if test="${!xyy}">  

50.                                     "下一页" href="">下一页  

51.                                 if>  

52.                                 "尾页" href="">尾页  

53.                               

54.                       

55.                 

56.                                               

57.             

58.           



第二种createNativeQuery(返回方式为实体对象集合)

1)dao层代码

Java代码  

1. @SuppressWarnings("unchecked")  

2.     public PageResult getList(Integer currentPage){  

3.         int pageSize = Constant.DEFAULT_PAGE_SIZE;  

4.         int start = (currentPage - 1) * pageSize;  

5.         String sql="select a.* "  

6.                 +" from train_apply a inner join train_plan b on b.ID=a.PLAN_ID";  

7.         PageResult pageResult = new PageResult();  

8.           

9.         Query query = getEntityManager().createNativeQuery(sql.toString(),TrainApply.class);  

10.         int total = query.getResultList().size();  

11.         // 判断分页  

12.         if (start < total && pageSize > 0) {  

13.             query.setFirstResult(start);  

14.             query.setMaxResults(pageSize);  

15.             pageResult.setFirst(start);  

16.             pageResult.setPageSize(pageSize);  

17.         }  

18.         pageResult.setTotalCount(total);  

19.         pageResult.setPageResultList(query.getResultList());  

20.         return pageResult;  

21.     }  

 2)其他与第一种类似,实体类不需要注释@NamedNativeQueries等。页面读取为item.planId类似方法获取数据,而不是item[0],item[1]。。。只是对于复杂的sql好像不太适合,比如说要count(id)某一列,目前用这种方式还不能实现几个表直接复杂字段展现查询。待研究。



培训计划

报名人数

操作

${s.index + 1}

${item[2]}

${item[3]}


17.                     class="applylist_operate">  

18.                         class="icon icon_train_edit" href="">  

19.                         class="icon icon_train_delete" href="/${item[0]}.htm"  title="确定要删除吗?删除后数据将不可恢复">  

20.                           

21.                     



举报

相关推荐

0 条评论