第一种 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. 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.setFirstResult(start);
11. query.setMaxResults(pageSize);
12. pageResult.setFirst(start);
13. pageResult.setPageSize(pageSize);
14. }
15.
16. pageResult.setTotalCount(total);
17. pageResult.setPageResultList(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. Integer pageNum = getIntParameter(request, "pageNum");
6. if (pageNum != null) {
7. currentPage = getIntParameter(request, "pageNum");
8. }
9. PageResult a = trainApplyService.findContentResult(currentPage);
10. addPageResultModel2(a, currentPage, model);
11. return "common/train/admin/applyList";
12. }
处理分页参数
Java代码
1. protected extends VO> void addPageResultModel2(PageResult pct,Integer currentPage, Model model) {
2. model.addAttribute("totalCount", pct.getTotalCount());
3. 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. model.addAttribute("currentPage", currentPage);
7. model.addAttribute("itemList", pct.getPageResultList());
8. }
3)实体类
Java代码
1. @NamedNativeQueries
2. (
3. {
4. @NamedNativeQuery(
5. name="ReturnTrainAppyUser",
6. query=" select a.id as apply_id,b.id as plan_id,b.title as plan_title,(select count(c.id) from train_apply_user c where c.APPLY_ID=a.ID) as 'apply_user_num',a.company as 'apply_company' from train_apply a inner join train_plan b on b.ID=a.PLAN_ID",
7. resultSetMapping="ReturnTrainAppyUser"),
8. }
9. )
10. @SqlResultSetMappings(
11. {
12. @SqlResultSetMapping
13. (
14. name="ReturnTrainAppyUser",
15. entities={},
16. columns=
17. {
18. @ColumnResult(name="apply_id"),
19. @ColumnResult(name="plan_id"),
20. @ColumnResult(name="plan_title"),
21. @ColumnResult(name="apply_user_num"),
22. @ColumnResult(name="apply_company")
23. }
24. )
25. })
26. @Entity
27. @Table(name = "train_apply")
28. public class TrainApply extends VO {
29. private static final long serialVersionUID = -6530604520661376764L;
30. @Id
31. @GeneratedValue(strategy = GenerationType.IDENTITY)
32. private Long id;// ID
33. private Long planId;//计划ID
34. private String orgName;//单位名称
35. private String roomType;//客房类型
36. private String roomNumber;//客房数
37. private String invoiceType;//发票类型
38. private String status;//状态
39.
40.
41.
42. public Long getId() {
43. return id;
44. }
45. public void setId(Long id) {
46. this.id = id;
47. }
48. public Long getPlanId() {
49. return planId;
50. }
51. public void setPlanId(Long planId) {
52. this.planId = planId;
53. }
54. public String getOrgName() {
55. return orgName;
56. }
57. public void setOrgName(String orgName) {
58. this.orgName = orgName;
59. }
60. public String getRoomType() {
61. return roomType;
62. }
63. public void setRoomType(String roomType) {
64. this.roomType = roomType;
65. }
66. public String getRoomNumber() {
67. return roomNumber;
68. }
69. public void setRoomNumber(String roomNumber) {
70. this.roomNumber = roomNumber;
71. }
72. public String getInvoiceType() {
73. return invoiceType;
74. }
75. public void setInvoiceType(String invoiceType) {
76. this.invoiceType = invoiceType;
77. }
78. public String getStatus() {
79. return status;
80. }
81. public void setStatus(String status) {
82. this.status = status;
83. }
84.
85. }
4)页面处理下一文章
未完待续!-------------------------------------------------